Resolve expression string in custom Java state

Hello friends,

I have a unique requirement about expression resolution. My understanding is that Camunda core / process engine takes care of resolving expressions, e.g. while starting state, resolving expressions to input / output variables.

Now, I have written a java state, which waits for a specific signal. Here I need to resolve a expression string AFTER state is started and signal is received. This is because my process variables would have changed after state start and before signal receive.

Is it possible to resolve a expression string in such custom java code?

Hi,

I’m not quite sure if I understand you correctly.
Do you have an example for this?

Greets
Chris

Following is my sample java state code.

public class TestWaitState extends AbstractBpmnActivityBehavior {

    //called at 11:00
    public void execute(final ActivityExecution execution) throws Exception {
        // this is executed when BPMN process instance reaches to this state
                
        //prints 11:00
        System.out.println( execution.getVariable("currentTime"));      // in BPMN definition, "currentTime" input variable is mapped to ${now()}
        System.out.println( execution.getVariable("isWorktimeOver"));   // in BPMN definition, "currentTime" input variable is mapped to ${now().hourOfDay > officeEndTimeVariable}
        
        //after this state goes into wait mode until leave() is called
    }

    //called at 12:30
    public void signal(ActivityExecution execution, String signalName, Object signalData) throws Exception {      
        // this is executed when a signal is received 
        
        //prints 11:00, and not 12:30
        System.out.println( execution.getVariable("currentTime"));      // in BPMN definition, "currentTime" input variable is mapped to ${now()}
        System.out.println( execution.getVariable("isWorktimeOver"));   // in BPMN definition, "currentTime" input variable is mapped to ${now().hourOfDay > officeEndTimeVariable}
        
        // exit state and proceed bpmn process execution to next state..
        leave(execution);  
      
    }
}

This state waits and stops execution until a signal is received and leave() function is called. Execution() function is called when state execution starts, and signal() function is called when a signal is received by this execution.

I have assigned few input variables to such java state, they are expressions containing few dynamic variables. When execute() is called, camunda resolves these variable expressions and resultant variable values are available in ‘execution’ argument. After some time, signal() function is triggered, but ‘execution’ argument of signal function still contains same resolved variable values, camunda does not re-resolve variable expressions upon signal() call. E.g. in above code snippet, both execute() and signal() functions see same time (time when execute() was called)

My requirement is to re-resolve input variable expressions in signal(). Expected result is - ‘currentTime’ variable value in above code snippet should show current system time in both execute() and signal() functions

Does anyone know the solution to this problem?

Hi,

you can take a look in the CallableElementActivityBehavior, where the input and output mapping for CallAcitivties is done, perhaps this helps. You could overwrite the signal method and do the same what is done in the CallableElementActivityBehavior execute method.

Please note that you use internal API, there is no guarantee about backwards compatibility, see the docs for more information about the difference between public and internal API.

Greets,
Chris