cancel
Showing results for 
Search instead for 
Did you mean: 

Suspend Functionality in Activiti

chandanmb1
Champ on-the-rise
Champ on-the-rise
Hi,

First of all i need to understand how suspend action works in activiti.
Assume, i have a scenario

Start -> Service Task1 -> Service Task2 ->End
Service Task 1 - counts from 1 to 1000
Service Task 2 - counts from 1001 to 2000

I am trying to suspend after the execution of Service Task 1
   runtimeService = processEngine.getRuntimeService();
        processInstance = runtimeService.startProcessInstanceByKey("suspendDelUser");
      
        When process starts, Control directly goes to Service Task 1 and then to Service Task 2 and then Ends
        AFAIK, to suspend process,
        String ProcInstanceId = processInstance.getProcessInstanceId();
        runtimeService.suspendProcessInstanceById(ProcInstanceId);
        which i am not getting.

How to suspend the process, when we are in the middle of execution of Service Task 1 (Or after completion of Service Task 1)
But it should not go to Service Task 2?
         
         


12 REPLIES 12

vasile_dirla
Star Contributor
Star Contributor
Try to mark the ServiceTask1 and ServiceTask2 as asynchronous and then put an execution listener on ServiceTask1 (listening the end event of this task).
inside this execution listener you could suspend the process instance (exactly as you wrote in the prev. post)

This way the suspendProcessInstanceById will be called between these 2 service tasks in separate transactions.
see: http://www.activiti.org/userguide/#asyncContinuations

I didn't test it but I think it will work.

Thanks for helping.
I added activiti:async="true" to my bpmn process xml.
But the Service Task 1 didnt executed, but directly it printed the ProcessInstanceID in my console.

I also didnt understand what is execution listener ? and how to add it ?

This is my sample code. I have added activiti:async=true
MainClass

processEngine = ProcessEngines.getDefaultProcessEngine();

   repositoryService = processEngine.getRepositoryService();
   Deployment deploymentId = repositoryService.createDeployment().addClasspathResource("chanprocess.bpmn20.xml")
     .deploy();

runtimeService = processEngine.getRuntimeService();
processInstance = runtimeService.startProcessInstanceByKey("development");
System.out.println("————–process instance————– : ");
   String procDefId = processInstance.getProcessDefinitionId();
   System.out.println("ProcessDefinitionId : " + procDefId);
   String ProcInstanceId = processInstance.getProcessInstanceId();
   System.out.println("Process Instance ID :::::::::::::::" + ProcInstanceId);

Service Task 1
public void execute(DelegateExecution delegate) throws Exception {
 
 
  for(int i=0;i<10;i++){
   System.out.println("Task 1 ::::::::Welcome to Bosch");
   Thread.sleep(100);
  }
 
}
@Override
public void notify(DelegateExecution delegate) throws Exception {
  String id = delegate.getId();
  System.out.println("ID is :::::::::::::"+id);
 
}

It didnt executed the Service Task 1 class, but directly printed the Process Instance ID in the main class.
Is service Task1 somewhere running ?

Hello vasile.dirla /Martin,

Please verify my code uploaded in github.
Let me know your opinions.

Regards,
Chandan

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

Could you create jUnit test please?
https://forums.activiti.org/content/sticky-how-write-unit-test

Regards
Martin

Hi,
I have created the Junit code , but how to upload the project and where ?

Hello Martin,

Here is the code.
https://github.com/chandanmb/activitiinaction

Please verify and let me know how to achieve asynchronous action as well as suspend in activiti

Regards,
Chandan

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Chandan,

Pull request with jUnit test example fix
https://github.com/chandanmb/activitiinaction/pull/9

Regards
Martin

Thanks martin for the code.
But the code is not working as expected i guess.

It is executing the Task 1 and i should get the control in the notify method to stop the further execution of workflow.
Currently , what is happening is
1. It is executing Task1
2. It is executing Task2
3. It is not going for Notify

To achieve is
1. Task 1 to be executed
2. Notify method to be triggered to suspend the further execution of workflow
3. Start the Suspended workflow
4. Should Resume executing from Task 2

Please provide me the solution