Advertise with us

Our eBooks

jBPM handlers code examples

Actions are a mechanism to bind your custom java code into a jBPM process. Actions can be associated with its own nodes (if they are relevant in the graphical representation of the process). Or actions can be placed on events like e.g. taking a transition, leaving a node or entering a node. In that case, the actions are not part of the graphical representation, but they are executed when execution fires the events in a runtime process execution.

ActionHandler example

    <state name="GetFromStock">
        <event type="node-enter">
            <action class="com.sample.action.GetFromStockActionHandler"
                name="recharge"></action>
        </event>
        <transition name="checkAvailability" to="CheckAvailability"></transition>
    </state>

 

package com.sample.action;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

public class GetFromStockActionHandler implements ActionHandler {

    public void execute(ExecutionContext context) throws Exception {
        System.out.println("Getting goods from factory!");

        context.getContextInstance().
        setVariable("goodsInStock", new Integer(300));

        context.leaveNode();
    }

}


AssignmentHandler example


An assignment handler implementation is called when a task instance is created. At that time, the task instance can be assigned to one or more actors.

<task-node name="In Carico">
        <task name="InizioLavorazione">
        <assignment class="it.wind.mobile.process.handler.LavorazioneAssignmentHandler" />
        </task>
        <transition to="In Lavorazione" name="to In Lavorazione"></transition>
    </task-node>

    


public class LavorazioneAssignmentHandler implements AssignmentHandler {
    private final static Logger logger = LoggerFactory.getLogger( MobileProcessManagerImpl.class );
    @Override
    public void assign(Assignable assignable, ExecutionContext executionContext)
            throws Exception {
        
        Node task = executionContext.getToken().getNode();
         
        String actor = (String) executionContext.getVariable("actor");
        assignable.setActorId(actor);
        logger.info(task.getName()+" assigned to "+actor);
        
    }

}

DecisionHandler example

jBPM decision nodes are very useful to create some alternative path along the process.
You can implement the decision condition either in the jPDL file:

    <decision name="WO Accettato">
            <transition to="In Carico">
             <condition>#{workorderAccepted == 0}</condition>
            </transition>
            <transition to="Chiusa" name="to Chiusa">
            <condition>#{workorderAccepted == 1}</condition>
            </transition>
            
    </decision>

    
Or you can leave the path decision to an handler which implements org.jbpm.graph.node.DecisionHandler

    
<decision name="Someone approved?">
   <handler class="com.sample.DecisionSample">
     <leaveReject>applicant rejected</leaveReject>
     <leaveOk>applicant approved</leaveOk>
   </handler>
 
   <transition name="applicant approved" to="HP approve"></transition>
   <transition name="applicant rejected" to="rejected"></transition>
    
 </decision>
 
 public  class DecisionSample implements org.jbpm.graph.node.DecisionHandler {
 
             
   //will be set with the name of the transition to go if user rejects
   private String leaveReject;
   //will be set with the name of the transition to go if user approves
   private String leaveOk;
              
   public String decide(ExecutionContext executionContext) throws Exception {
     log.debug(logPrefix +"DecisionOnEnd: leaveReject: "+ leaveReject);
     log.debug(logPrefix +"DecisionOnEnd: leaveOk:     "+ leaveOk);
 
     //which way to go, as default we reject
      String exitTransition = leaveReject;
 
     try {
       //fetch input data from the processInstance variables (see WorkManagerBean for more info)    
       String varName="inputdataActivity";
       ContextInstance contextInstance = executionContext.getContextInstance();
 
       Activity myActivity = (Activity) contextInstance.getVariable(varName);
 
       if(myActivity != null) {
         log.debug(logPrefix +"DecisionOnEnd: Reading processInstance variable to determine if approved or not. \""+ varName
                   + "\", value="+ myActivity.bApproved);          
 
         //if approved we go to the transition defined in leaveOk
         if(myActivity.bApproved)
             exitTransition = leaveOk;
 
       }else{
         log.error(logPrefix +"DecisionOnEnd: there data regarding if the task was approved or rejected is null! (i.e. "+ varName
                   + "=null). The task is rejected since we have to do something");
       }
                 
     } catch (Exception e) {
       log.error(logPrefix +"DecisionOnEnd: Catched exception when trying to read variable, " +
               "the task is rejected since we have to do something  e="+e);
     }                  
                 
     log.debug(logPrefix +"DecisionOnEnd: Attempt to leave on the transition: " + exitTransition );
     return exitTransition;
   }
 }    
© 2021 mastertheboss.com. All Rights Reserved.

Please publish modules in offcanvas position.