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;
   }
 }    

Recommended Articles

Mastering BPMN Elements: Activities and Gateways | Enterprise Java Tutorials

Learn about the core BPMN elements: Activities, Gateways, and Sub-Processes in this comprehensive tutorial for Enterprise Java developers.

How to Use a REST WorkItem Handler in jBPM | Master the Boss

Learn how to use the REST WorkItem Handler in jBPM to execute tasks during a process and send POST callbacks to your web application.

Mastering Enterprise Java Rule Engines with WildFly 31 - A Comprehensive Guide

Learn how to leverage WildFly 31 for advanced enterprise rule engine implementation. #Java #Middleware #CloudNative

Configure jBPM to use MySQL as Database | Enterprise Java Tutorials

Learn how to configure jBPM to use MySQL as database for storing process data. A step-by-step guide on setting up jBPM with MySQL.