How to set Process Variables at startup in JBPM

In jBPM, you can conveniently set process variables at start-up by utilizing start parameters. Start parameters allow you to provide initial values for process variables when starting a new process instance. This tutorial will guide you through the steps to achieve this.


Step 1: Define Process Variables

Ensure that your BPMN process definition file (with the .bpmn extension) contains the process variables you want to set at start-up. For this example, let’s assume we have a process variable called “traveller”:

how to set process variables at start up in jbpm

Step 2: Start a Process Instance with Start Parameters

  1. Obtain the KieServices instance in your Java code:
KieServices kieServices = KieServices.Factory.get();

Load the process definition from the classpath:

KieContainer kieContainer = kieServices.getKieClasspathContainer();
KieSession kieSession = kieContainer.newKieSession();

Prepare the start parameters:

Map<String, Object> parameters = new HashMap<>();
parameters.put("traveller", "John Doe"); // Set the initial value for "traveller" variable

Start a new process instance with the provided start parameters (replace in the code with your Process instance id):

ProcessInstance processInstance = kieSession.startProcess("yourProcessId", parameters);

Step 3: Access Process Variables within the Process

Once the process instance is started, you can access the initial values of the process variables within your BPMN process using expressions or service tasks.

For example, you can use an expression language (e.g., Drools Expression Language or MVEL) to access the “traveller” variable within a script task or gateway:


Alternatively, you can use it within a service task to perform business logic or integrate with external systems.

By setting process variables at start-up using start parameters, you can initialize process instances with specific values, enabling more dynamic and adaptable process flows.

That’s it! You have successfully learned how to set process variables at start-up using jBPM 7. This feature empowers you to customize process instances according to specific requirements and achieve greater flexibility in your BPMN workflows.

Found the article helpful? if so please follow us on Socials