How to end a process instance

Hello all,

My bpmn file is as follows:

add_mul.bpmn (4.2 KB)

I am using receive task so that i can use RuntimeService, I am starting my process in sayHello class as follows:

‘’’
public void sayHello(ProcessEngine processEngine) {

	try {
		System.out.println("inside postdeploy  ");
		variables.put("a", 2);
		variables.put("b", 5);
		variables.put("c", 0);
			
ProcessInstance instance=  processEngine.getRuntimeService().startProcessInstanceByKey("Process_2", variables);


variables.put("c",processEngine.getRuntimeService().getVariable(instance.getId(), "c"));
		
		Execution execution = processEngine.getRuntimeService().createExecutionQuery()
				  .processInstanceId(instance.getId())
				  .activityId("ReceiveTask_16nulbx")
				  .singleResult();

		processEngine.getRuntimeService().signal(execution.getId());

}
‘’’
I set my c variable in Calculator class which is implementing by my service task as follows:

‘’’
public class Calculator implements JavaDelegate {

public void execute(DelegateExecution exe) throws Exception {
	
	System.out.println("Inside calculator again");

    Integer x = (Integer) exe.getVariable("a");
    Integer y = (Integer) exe.getVariable("b");
    int add = x+y;
	System.out.println("Addition is"+add);
	exe.setVariable("c", add);
    
}

‘’’
The problem is my process instance is not ended after this.
My question is how can i end my Process instance after getting my c variable??

Any help is appreciated .

Thank you,
Keshav Taparia

Hey,

the problem is that your Receive Task is still active.
You send a signal not a message. The signal can’t be recieved by the receive task.
You have to add a message to your process and a messageRef to your receive task.
Then you can correlate a message in your sayHello method.

So for example your process definition can look like this add_mul.bpmn (4.3 KB)

Then replace the line

 processEngine.getRuntimeService().signal(execution.getId());

with

processEngine.getRuntimeService().correlateMessage("Message_20ddbko");

Hope it helps.

Kind regards,

Chris

1 Like

Dear Zelldon sir,

i truly appreciate your time and effort.I changed my bpmn file as well as sayHello class code as suggested by you.Now my sayHello class looks as follows:

‘’’
public void sayHello(ProcessEngine processEngine) {

	try {
		System.out.println("inside postdeploy  ");
		variables.put("a", 2);
		variables.put("b", 5);
		variables.put("c", 0);

ProcessInstance instance= processEngine.getRuntimeService().startProcessInstanceByKey(“Process_2”, variables);
variables.put(“c”,processEngine.getRuntimeService().getVariable(instance.getId(), “c”));
Execution execution = processEngine.getRuntimeService().createExecutionQuery()
.processInstanceId(instance.getId())
.activityId(“ReceiveTask_16nulbx”)
.singleResult();
processEngine.getRuntimeService().correlateMessage(“Message_20ddbko”);

        int i=1;
        for(i=1;i>0;i--)
		{
			System.out.println("waiting"+i);
			Thread.sleep(20000);
		}
		if(instance.isEnded())
		{
			System.out.println("Successfully completed");
		}

‘’’

The problem is same it is not printing Successfully completed.

Is there anything i need to change?

Awaiting your reply.

Thanking you,
Keshav Taparia

Hey Keshav,

the problem is that your process instance object will not be updated after the receive task is completed.
You have to query the process instance if you want to check whether the instance is ended or not.
Use the following statement to check if the instance still exists:

ProcessInstance processInstance = processEngine
  .getRuntimeService()
  .createProcessInstanceQuery()
  .processInstanceId(processInstanceId)
  .singleResult();

If the processInstance is null then the instance was completed.

Short note to my previous reply.
I was wrong that the receive task can’t receive signals. If the receive task has no message reference you can use
the signal method. Sorry for that.
For further information see the docs.

Greetings,
Chris

1 Like

Dear Zeldon Sir,

Thanks for your prompt reply.Yes that’s absolutely correct,now my processInstance is null.

I have one more question for you regarding HistoryService, In this case basically we are pausing our process and then retrieve process variable from RuntimeService so I tried to retrieve my process variables(without using recieve task) from HistoryService query as suggested by you(Stackoverflow),in this case my sayHello class looks as follows:

‘’’
public void sayHello(ProcessEngine processEngine) {

	try {
		System.out.println("inside postdeploy  ");
		variables.put("a", 2);
		variables.put("b", 5);
		variables.put("c", 0);
			
ProcessInstance instance=  processEngine.getRuntimeService().startProcessInstanceByKey("Process_2", variables);

List variables = processEngine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceIdIn(processInstance.getId()).variableName(“c”).list();

‘’’
Error message in my eclipse is as follows:

Multiple markers at this line
- The method processInstanceIdIn(String) is undefined for the type
HistoricVariableInstanceQuery
- The type List is not generic; it cannot be parameterized with arguments

Can you tell me how can i retrieve my process variable c in my sayHello class by using HistoryService?

Thanking you,
Keshav Taparia

Hi Keshav,

replace the processInstanceIdIn with processInstanceId. That should work for your case.
The method processInstanceIdIn is added in the 7.6.2-alpha2 version.

Also use List<HistoricVariableInstance> instead of simple List.

Best regards,
Chris

1 Like

Dear Zeldon Sir,

I am Sorry for dumb question.Now it worked.

Thanks a lot :slight_smile: You saved my time and project.

Keshav Taparia

Hi,

I wondered why there is no attribute processInstanceIdIn in GET /history/variable-instance like in GET /variable-instance and just stumbled over the following statement:

In 7.5, there is no chance to query all historic variable instances corresponding to a list of process instances? So I need to make a REST call for every single process instance to get all variables?

Thanks for any hints!

Cheers,
Kerstin

Hi Kerstin,

it seems that the documentation misses the query parameter processInstanceIdIn
for the resource /history/variable-instance.

I tried to use the parameter but it seems that there is also a bug, because all variables are returned.
I created a bug ticket for this. In the meantime as workaround you have to create a request for each process instance,
with the processInstanceId Queryparameter

Thanks for your hint.

Best regards,
Chris

Hi Chris,

if you spell a query parameter wrong, it is omitted by the query without any message. Normally the result is not the expected and normally it’s too large, as an and-condition is missing.

An improvement could be to answer the request with the error message from the database:
Column "PROC_INST_IDD_" not found; SQL statement: SELECT * FROM ACT_HI_VARINST where PROC_INST_IDd_ in ('1', '2') [42122-168] 42S22/42122

What do you think?

Cheers, Ingo

Hey Ingo,

you’re right from the user perspective it is more convenient, if we return an error response for example
HTTP status code 400 - Bad request with an appropriate message.

I can’t estimate the effort to change this, since the default behavior of JAX-RS is setting the parameter to null if they’re not given. The question will be how we can decide that the user has a typo on his request parameters or the parameter wasn’t used. For sure we can parse the request and the UriInfo context, but i don’t think that this is the better approach.

Greetings,
Chris