synchronous execution of long running task

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Member II

synchronous execution of long running task

Hi,

I am using Activti version 6.0.0.

I want synchronous executions of task in activiti,but I have a long running task which might take 20 minutes to execute.
I wanted to get process instance id quickly in my controller without considering completion of task in bpmn,so I added a intermediate timer event of "0 minute" in my bpmn file.

I  have started executing a process,lets say with processInstanceid p1.At that time only I again start the process,lets say with processInstanceid  p2.
Now both processes arrive till the task where there is a wait event of 20 minutes(I have used while loop for this).Now according to the flow both processes should come till the end task and finish their execution but instead of that none of the process finish its execution and run multiple times from first task to end task.

how to run multiple processes independently and parallaly from start task to end task (one of these tasks have code which will take long time to execute)

8 Replies
Highlighted
Senior Member

Re: synchronous execution of long running task

Hi Adi,

Can I ask you why are you running those task as synchronous tasks? 

It really sounds like an async task to me. Doing those in a sync way will lead you to all sort of errors that you will need to deal with. Imagine what will happen if the jvm dies while you are executing those tasks? how are you planning to recover from such situations. Using the Job Executor and async tasks will deal with those situations for you. 

Can you share more context of the class that is doing the while loop? I bet that the problem is there. 

Highlighted
Member II

Re: synchronous execution of long running task

Hi Mauricio

I want synchronous execution of task since the output of one task acts as input to other.The long running task in which I have introduced a while loop returns some result which I am using in subsequent task. If I make my long running task(while loop) as async then the subsequent task which is dependent on the while loop task will execute with null values before we get an output from while loop task. 

That's why I have kept these tasks synchronous. 

Is their any solution to achieving synchronous execution then?

Highlighted
Senior Member

Re: synchronous execution of long running task

Yes, so I if you have one task after the other even if they are Async that will work. T1 -> T2, even if they are async they will be executed one after the other, meaning that T2 will be only executed after T1. Adding those while loops are introducing more problems than solutions. 

Highlighted
Member II

Re: synchronous execution of long running task

Hi Mauricio,

This is my while loop:-

while(some condition==true)
{
if(test1==true){

//assign some values
}
else if(test2==true){
//assign some values
}
else{
Thread.sleep(3000);
}
}

I marked my tasks as async task and was able to get sequential execution of these tasks but sometimes during execution we get a exception like:-

   .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.4.5.RELEASE)
 
2018-04-23 18:39:06.446  INFO 16544 --- [           main] c.g.c.demo.demoWorkflowServiceApplication  : Starting demoWorkflowServiceApplication on G864Q6C2E with PID 16544 (C:\Workplace\workflow\demo-app-svc\target\classes started by 212558691 in C:\Workplace\workflow\demo-app-svc)
2018-04-23 18:39:06.462  INFO 16544 --- [           main] c.g.c.demo.demoWorkflowServiceApplication  : The following profiles are active: dev
2018-04-23 18:39:06.621  INFO 16544 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@28b46423: startup date [Mon Apr 23 18:39:06 IST 2018]; root of context hierarchy
2018-04-23 18:39:16.452  INFO 16544 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$a3e53bcf] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-04-23 18:39:18.030  INFO 16544 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 4000 (http)
2018-04-23 18:39:18.059  INFO 16544 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2018-04-23 18:39:18.062  INFO 16544 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2018-04-23 18:39:18.494  INFO 16544 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-23 18:39:18.495  INFO 16544 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 11885 ms
2018-04-23 18:39:19.163  INFO 16544 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-04-23 18:39:19.177  INFO 16544 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-23 18:39:19.181  INFO 16544 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-23 18:39:19.182  INFO 16544 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-23 18:39:19.182  INFO 16544 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-23 18:39:19.815  INFO 16544 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'WorkflowUnit'
21  WorkflowUnit  WARN   [main] openjpa.Runtime - An error occurred while registering a ClassTransformer with PersistenceUnitInfo: name 'WorkflowUnit', root URL [file:/C:/Workplace/workflow/demo-app-svc/target/classes]. The error has been consumed. To see it, set your openjpa.Runtime log level to TRACE. Load-time class transformation will not be available.
2018-04-23 18:39:20.414  INFO 16544 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'WorkflowUnit'
Mapper added
2018-04-23 18:39:27.091  INFO 16544 --- [           main] aultActiviti5CompatibilityHandlerFactory : Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
2018-04-23 18:39:27.311  INFO 16544 --- [           main] o.a.engine.impl.ProcessEngineImpl        : ProcessEngine default created
2018-04-23 18:39:27.317  INFO 16544 --- [           main] o.a.e.i.a.DefaultAsyncJobExecutor        : Starting up the default async job executor [org.activiti.spring.SpringAsyncExecutor].
2018-04-23 18:39:27.320  INFO 16544 --- [       Thread-2] o.a.e.i.a.AcquireAsyncJobsDueRunnable    : {} starting to acquire async jobs due
2018-04-23 18:39:27.320  INFO 16544 --- [       Thread-4] o.a.e.i.a.ResetExpiredJobsRunnable       : {} starting to reset expired jobs
2018-04-23 18:39:27.320  INFO 16544 --- [       Thread-3] o.a.e.i.a.AcquireTimerJobsRunnable       : {} starting to acquire async jobs due
2018-04-23 18:39:29.506  INFO 16544 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@28b46423: startup date [Mon Apr 23 18:39:06 IST 2018]; root of context hierarchy
2018-04-23 18:39:29.730  INFO 16544 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/run/{processXMLId}],methods=[POST],consumes=[application/json]}" onto public java.lang.String com.ge.cpm.demo.controller.WorkflowController.run(java.lang.String,java.lang.String,java.lang.String,com.ge.cpm.demo.controller.ProcessModel)
2018-04-23 18:39:29.737  INFO 16544 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/history],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.ge.cpm.demo.controller.WorkflowController.getHistoryForMultipleId(java.lang.String)
2018-04-23 18:39:29.746  INFO 16544 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-23 18:39:29.747  INFO 16544 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-23 18:39:29.895  INFO 16544 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-23 18:39:29.895  INFO 16544 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-23 18:39:30.065  INFO 16544 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-23 18:39:31.253  INFO 16544 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-23 18:39:31.419  INFO 16544 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 4000 (http)
2018-04-23 18:39:31.427  INFO 16544 --- [           main] c.g.c.demo.demoWorkflowServiceApplication  : Started demoWorkflowServiceApplication in 26.113 seconds (JVM running for 35.125)
Starting Workflow service
2018-04-23 18:39:38.485  INFO 16544 --- [nio-4000-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-04-23 18:39:38.486  INFO 16544 --- [nio-4000-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-04-23 18:39:38.531  INFO 16544 --- [nio-4000-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 45 ms
2018-04-23 18:39:51.882  INFO 16544 --- [nio-4000-exec-2] com.ge.cpm.demo.activiti.CheckStatusTask  : Number of process definitions : 24
2018-04-23 18:39:51.987  INFO 16544 --- [nio-4000-exec-2] com.ge.cpm.demo.activiti.CheckStatusTask  : Number of tasks : 0
Number of tasks after process start: 0
********in Call url **************
processInstanceId 1005001
url http://localhost:3018/model/982782982792792
2018-04-23 18:40:26.245 ERROR 16544 --- [cTaskExecutor-4] o.a.e.impl.interceptor.CommandContext    : Error while closing command context
 
java.lang.NullPointerException: null
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:151) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:35) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.persistence.entity.AbstractEntityManager.delete(AbstractEntityManager.java:87) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:137) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:35) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.persistence.entity.AbstractEntityManager.delete(AbstractEntityManager.java:82) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.asyncexecutor.DefaultJobManager.unacquire(DefaultJobManager.java:230) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable$3.execute(ExecuteAsyncRunnable.java:169) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable$3.execute(ExecuteAsyncRunnable.java:167) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:37) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:78) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:57) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:42) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:63) ~[activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:47) [activiti-spring-6.0.0.jar:6.0.0]
                at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) [spring-tx-4.3.7.RELEASE.jar:4.3.7.RELEASE]
                at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:45) [activiti-spring-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:29) [activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44) [activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:39) [activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable.unacquireJob(ExecuteAsyncRunnable.java:167) [activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable.lockJobIfNeeded(ExecuteAsyncRunnable.java:154) [activiti-engine-6.0.0.jar:6.0.0]
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable.run(ExecuteAsyncRunnable.java:72) [activiti-engine-6.0.0.jar:6.0.0]
                at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
 
Exception in thread "SimpleAsyncTaskExecutor-4" java.lang.NullPointerException
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:151)
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:35)
                at org.activiti.engine.impl.persistence.entity.AbstractEntityManager.delete(AbstractEntityManager.java:87)
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:137)
                at org.activiti.engine.impl.persistence.entity.JobEntityManagerImpl.delete(JobEntityManagerImpl.java:35)
                at org.activiti.engine.impl.persistence.entity.AbstractEntityManager.delete(AbstractEntityManager.java:82)
                at org.activiti.engine.impl.asyncexecutor.DefaultJobManager.unacquire(DefaultJobManager.java:230)
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable$3.execute(ExecuteAsyncRunnable.java:169)
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable$3.execute(ExecuteAsyncRunnable.java:167)
                at org.activiti.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:37)
                at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:78)
                at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:57)
                at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:42)
                at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48)
                at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:63)
                at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:47)
                at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
                at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:45)
                at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:29)
                at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44)
                at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:39)
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable.unacquireJob(ExecuteAsyncRunnable.java:167)
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable.lockJobIfNeeded(ExecuteAsyncRunnable.java:154)
                at org.activiti.engine.impl.asyncexecutor.ExecuteAsyncRunnable.run(ExecuteAsyncRunnable.java:72)
                at java.lang.Thread.run(Thread.java:748)
 

What could be the possible reason for this exception?

Highlighted
Senior Member

Re: synchronous execution of long running task

So, did you removed the loop after doing the task Async? 
You don't need that anymore. 

Highlighted
Member II

Re: synchronous execution of long running task

While loop is written in my java code,I need it because after certain time I have to perform some task repeatedly until some condition is satisfied.

So I don't know how to handle the that thing form activiti. 

So I have not removed the while loop.

Is there any way to implement it through activiti?

Highlighted
Senior Member

Re: synchronous execution of long running task

It is quite difficult to help if we don't know what you are trying to do. When you say "I have to perform some task repeatedly until some condition is satisfied." what kind of task is it? Are you calling a rest service? Are you doing an in JVM method call? Activiti doesn't really matter what do you have in that code, but you need to be aware that if you are using the Job Executor to execute that task in an Async way, your code will be running in another thread, so I will recommend you for example not to run another thread inside your code. If you give more concrete details of how your task is being executed and what that task is doing we are more than happy to help you by providing some alternatives that you can try out. 

cheers

Highlighted
Member II

Re: synchronous execution of long running task

I am doing a rest call inside while loop to get  status of some rest call.I am continuously calling the rest service (after every 30s using thread.sleep(30000) )until I get "Completed" or "Error" in Response. The while loop will break when i get "Completed" or  "Error " in response or if timeout of 20 minutes is reached. We are not using any configuration for activating job executor in xml file.