How to inject variables into a JBPM Action handler ?

This tutorial has been written for an old version of jBPM which is now deprecated.
The current version of jBPM does not include Action Handlers but you can define WorkItem handlers to run Java code in your process.

The following tutorial covers this topic: How to create a custom WorkItem Handler in jBPM

You can inject data into any Handler (ActionHandler, AssignmentHandler, DecisionHandler) by simply adding the properties as XML elements. In the handler class you have to create the corresponding field with getters/setters methods.

Example:

    
<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;
   }
 }    
Found the article helpful? if so please follow us on Socials