Mike
Mike

Reputation: 832

What is the proper way to acces JBPM6 Process Variables

I started off with this example JBPM webapp as the basis for my code. I made enough modifications to go the code to deploy in JBoss EAP 6.3 using JBPM6.1.0.Final (I could not get it to deploy in WildFly 8.1 or 8.2). I wanted to modify the webapp to actually perform some custom basic human task interaction. I created a POJO called PurchaseRequest and created a process variable of type PurchaseRequest named request. The following code is how I start the process

PurchaseRequest purchaseRequest = new PurchaseRequest(item, user, cost);
Map<String, Object> params = new HashMap<String, Object>();
params.put("request", purchaseRequest);
processInstanceId = processService.startProcess(processId, params);

I added the following Script Task to show that the process variable is being set.

if (request == null) {
    System.out.println("Output1: request is NULL");
} else {
    System.out.println("Output1: request is VALID");
    System.out.println("\trequester: "+request.getRequester());
    System.out.println("\titem: "+request.getItem());
    System.out.println("\tcost: "+request.getCost());
}

Inside the TaskServlet I added the following instance variables

@Inject
@Singleton
private RuntimeEnvironment runtimeEnvironment;

I also added the following private method to the servlet

private void printTaskInfo(TaskSummary summary) {
    RuntimeEngine runtime = processService.getRuntimeManager().getRuntimeEngine(EmptyContext
            .get());
    KieSession ksession = runtime.getKieSession();
    ProcessInstance pi = ksession.getProcessInstance(summary.getProcessInstanceId());
    //Exception is occuring on this line
    org.kie.api.definition.process.Process procsess = pi.getProcess();
}

When I retrieve the active tasks through the taskService.retrieveTaskList(user) method, I cycle through the collection of TaskSummary instances and call the private method above. When I call the method, I get the following error

java.lang.RuntimeException: Process instance 1[com.sample.bpmn] is disconnected.

I feel that I may have strayed off target since I could not find any examples of how to access Process Variables. Can anyone point me towards an example of how to properly access Process Variables from within a Java EE/CDI container?

EDIT: Here is the code I came up with for JBPM 6.1.

public Object getProcessVariable(Long processInstanceId, String variable) {
    RuntimeEngine runtime = singletonManager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    ProcessInstance pi = ksession.getProcessInstance(processInstanceId);
    RuleFlowProcessInstance rfpi = (RuleFlowProcessInstance)pi;
    if (rfpi == null) {
        return null;
    }
    return rfpi.getVariable(variable);
}

Upvotes: 2

Views: 3010

Answers (1)

salaboy
salaboy

Reputation: 4143

Notice that you are accessing to the Process Definition in there, do you really need to do that? if you already know the process variables that you have defined you can just get the process variables from the ProcessInstance instead.

Upvotes: 2

Related Questions