Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:3500:0300:0314 [2023/04/05 05:57] – removed - external edit (Unknown date) 127.0.0.1 | docs:3500:0300:0314 [2023/04/05 07:40] (current) – [Examples of custom process components] sean | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{tag> | ||
| + | [< | ||
| + | ==== Examples of custom process components ==== | ||
| + | |||
| + | The following example shows the code for the simple process component that changes the attribute of the process input. | ||
| + | |||
| + | <code java> | ||
| + | { | ||
| + | public SimpleProcess() | ||
| + | { | ||
| + | } | ||
| + | /** | ||
| + | * @see com.bas.basserver.executionengine.IProcess# | ||
| + | */ | ||
| + | @Override | ||
| + | public boolean cancel() | ||
| + | { | ||
| + | return true; | ||
| + | } | ||
| + | /** | ||
| + | * @see com.bas.basserver.executionengine.IProcess# | ||
| + | */ | ||
| + | @Override | ||
| + | public Object execute(IExecutionEngine engine, Object[] parameters) | ||
| + | throws SuspendProcessException, | ||
| + | { | ||
| + | // one entity is declared as process input | ||
| + | if (parameters== null || parameters.length!=1 || !(parameters[0] instanceof IEntity)) | ||
| + | { | ||
| + | return null; | ||
| + | } | ||
| + | // get the parameter | ||
| + | IEntityentity=(IEntity)parameters[0]; | ||
| + | Object oldValue = null; | ||
| + | try | ||
| + | { | ||
| + | // change " | ||
| + | oldValue = entity.getAttributeValue (" | ||
| + | entity.setAttributeValue(" | ||
| + | } | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | } | ||
| + | try | ||
| + | { | ||
| + | // update entity in persistence | ||
| + | engine.updateEntity(this, | ||
| + | new String[]{" | ||
| + | new Object [] {oldValue} ); | ||
| + | } | ||
| + | catch (ServerTimeOutExceptionste) | ||
| + | { | ||
| + | throw new SuspendProcessException(ste); | ||
| + | } | ||
| + | return null; | ||
| + | } | ||
| + | /** | ||
| + | * @see com.bas.basserver.executionengine.IProcess# | ||
| + | */ | ||
| + | @Override | ||
| + | public Objectresume(IExecutionEngineengine, | ||
| + | throws SuspendProcessException, | ||
| + | { | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The next example shows the code of the process that performs some lengthy calculation and throws SuspendProcessException every now and again: | ||
| + | |||
| + | <code java> | ||
| + | { | ||
| + | private boolean m_cancelled = false; | ||
| + | private long m_count; | ||
| + | public LongProcess() | ||
| + | { | ||
| + | } | ||
| + | /** @see IProcess# | ||
| + | @Override | ||
| + | public boolean cancel() | ||
| + | { | ||
| + | // set m_cancelled flag. This flag will be checked by execute // and resume methods | ||
| + | synchronized (this) | ||
| + | { | ||
| + | m_cancelled = true; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | /** | ||
| + | * @see IProcess# | ||
| + | */ | ||
| + | @Override | ||
| + | public Object execute(IExecutionEngine engine, Object[] parameters) | ||
| + | throws SuspendProcessException, | ||
| + | { | ||
| + | m_count=0; | ||
| + | execute(engine); | ||
| + | return null; | ||
| + | } | ||
| + | private void execute(IExecutionEngine engine) | ||
| + | throws SuspendProcessException | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | // get the context in which the process is running | ||
| + | IExecutionContextmyContext = engine.getExecutionContext(this); | ||
| + | while (true) | ||
| + | { | ||
| + | Thread.sleep(500); | ||
| + | // finish after 400 iterations | ||
| + | if (m_count++ >=400) | ||
| + | break; | ||
| + | // every 4 iterations update our progress | ||
| + | if (m_count % 4==0) | ||
| + | { | ||
| + | int progress = myContext.getProgress (); | ||
| + | if (progress< | ||
| + | myContext.setProgress (progress + 1); | ||
| + | } | ||
| + | // check if we have been cancelled | ||
| + | synchronized (this) | ||
| + | { | ||
| + | if (m_cancelled) | ||
| + | break; | ||
| + | } | ||
| + | // every 120 iterations suspend ourselves so that | ||
| + | // transaction can be committed (though this is | ||
| + | // only for illustrational purposes as we are not | ||
| + | // doing any transactional operations here such as | ||
| + | // changing values of attributes etc) | ||
| + | if (m_count % 120 == 0) | ||
| + | { | ||
| + | throw new SuspendProcessException (true); | ||
| + | // we will be called soon – see resume | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | catch (InterruptedException ie) | ||
| + | { | ||
| + | } | ||
| + | } | ||
| + | /** | ||
| + | * @see IProcess# | ||
| + | */ | ||
| + | @Override | ||
| + | public Object resume (IExecutionEngine engine, Object reply) | ||
| + | throws SuspendProcessException, | ||
| + | { | ||
| + | // this is called every 120 iterations | ||
| + | execute (engine); | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | {{simplenavi>: | ||
| + | |||
| + | |||