2

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);
}
Mike
  • 820
  • 7
  • 19
  • I would say that this is the most correctly way of doing it right now: WorkflowProcessInstance wpi = (WorkflowProcessInstance)processInstance; and you should check if the pi variable is null before casting – salaboy Sep 28 '15 at 08:29
  • Hi Mike, I am trying to set/update the process variable but getting same error. Here is my question - https://stackoverflow.com/questions/50613721/proper-way-to-set-or-update-jbpm6-process-variables – AniSaw May 30 '18 at 21:41

1 Answers1

2

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.

salaboy
  • 4,123
  • 1
  • 14
  • 15
  • 1
    Thank you for pointing that out @salaboy . I forgot that ProcessInstance was an interface and not class. The implementation class was really RuleFlowProcessInstance, which contains the method public String getVariable(String variableName); – Mike Feb 18 '15 at 13:20
  • 1
    Good to know that I can still help in here :) Enjoy! – salaboy Feb 19 '15 at 20:08
  • 1
    @Mike, can you provide an updated example on how to access the process instance variables? I'm still unsure / confused on the explanation above. I'm also getting that `is disconnected` error. – LifeAndHope Sep 11 '15 at 15:08
  • 1
    @jl987, I added an edit at the bottom to show the code I wound up using in my project. – Mike Sep 11 '15 at 15:46