Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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.1docs:3500:0300:0314 [2023/04/05 07:40] (current) – [Examples of custom process components] sean
Line 1: Line 1:
 +{{tag>Programmers_Reference Example Custom_Process}}
 +[<10>]
 +==== 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 class SimpleProcess implements IProcess
 +{
 +    public SimpleProcess()
 +    {
 +    }
 +    /**
 +     * @see com.bas.basserver.executionengine.IProcess#cancel()
 +     */
 +    @Override
 +    public boolean cancel()
 +    {
 +        return true;
 +    }
 +    /**
 +     * @see com.bas.basserver.executionengine.IProcess#execute(com.bas.basserver.executionengine.IExecutionEngine,java.lang.Object[])
 +     */
 +    @Override
 +    public Object execute(IExecutionEngine engine, Object[] parameters)
 +    throws SuspendProcessException, ExecutionException, AccessDeniedException
 +    {
 +        // 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 "attrName" attribute to "attrValue" in memory
 +            oldValue = entity.getAttributeValue ("attrName");
 +            entity.setAttributeValue("attrName","attrValue");
 +        }
 +        catch (Exception e)
 +        {   
 +        }
 +        try
 +        {
 +        // update entity in persistence
 +            engine.updateEntity(this, entity,
 +                new String[]{"attrName"}, 
 +                new Object [] {oldValue} );
 +        }
 +        catch (ServerTimeOutExceptionste)
 +        {
 +            throw new SuspendProcessException(ste);
 +        }
 +        return null;
 +    }
 +    /**
 +     * @see com.bas.basserver.executionengine.IProcess#resume(com.bas.basserver.executionengine.IExecutionEngine,java.lang.Object)
 +     */
 +    @Override
 +    public Objectresume(IExecutionEngineengine,Objectreply)
 +    throws SuspendProcessException, ExecutionException, AccessDeniedException
 +    {
 +        return null;
 +    }
 +}
 +</code>
 +
 +The next example shows the code of the process that performs some lengthy calculation and throws SuspendProcessException every now and again:
 +
 +<code java>public class LongProcess implements IProcess
 +{
 +    private boolean m_cancelled = false;
 +    private long m_count;
 +    public LongProcess()
 +    {
 +    }
 +    /** @see IProcess#cancel()*/
 +    @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#execute(IExecutionEngine, Object[])
 +     */
 +    @Override
 +    public Object execute(IExecutionEngine engine, Object[] parameters)
 +    throws SuspendProcessException, ExecutionException
 +    {
 +        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<100)
 +                        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#resume(IExecutionEngine,Object)
 +     */
 +    @Override
 +    public Object resume (IExecutionEngine engine, Object reply)
 +    throws SuspendProcessException, ExecutionException
 +    {
 +        // this is called every 120 iterations
 +        execute (engine);
 +        return null;
 +    }
 +}
 +</code>
 +
 +{{simplenavi>:docs:3500:3000:0314:}}
 +
 +