We have a lot of automated tasks in a activiti process which takes a lot of time to complete. Like REST calls to external system OR JMS message send and receive response.
I can do this using two options :
Service Async task
Send - receive task: Here we can have one task which will call the external program with correlation id as execution id and move to the next step which would be a receive task. Once we get a response from the external system we can signal the process.
Can someone validate what would be the correct approach or any better way to handle this in activiti.
If you have control over the receiver services, you could also use an intermediate message event to pause execution and have the external (long running) service release the process by injecting a message.
Your two approaches each have advantages/disadvantages
Service Async Task - I am assuming you are proposing to keep the "pause" logic inside the service task and simply block on the task until the external service comes back. While this is neater and keeps the logic inside the service task implementation, it doesn't allow for boundary timer events or other BPMN error/timeout handling to move the process on if the service timesout. Certainly you can throw a BPMNError which will bubble up but it makes the actual logic harder to follow.
Send-Receive Tasks - This assumes you have control over the external services as it is very similar to the third option I mentioned using intermediate message events. I tend to prefer this approach as the logic is clear and obvious. However, it does mean you must have the ability for the external service to "send" something back to the receive task to progress the flow.
Either way, both options have their place and much depends on the nature of your external service.
Hope this helps.
Related
Recently I tried to create a task with Dropwizard that would be triggered within a resource but I can't find a way to do it.
I know that there are a integration with Quartz but that doesn't fit my needs (don't want to schedule tasks).
Is the only option to make a POST to the task endpoint? If so, how can I do a request to /tasks/myTask ?
I don't want to change the architecture to something like producer/consumer, where I create a task in the resource and enqueue it to have then something executing the tasks enqueued.
I posted a sample of how you can use a Managed service to execute tasks.
Running async jobs in dropwizard, and polling their status
Is there a specific reason why you need to invoke the code as a task? I would extract the logic from the Task and put it in it's own class. Then you can use it from multiple places irrespective of the implementation. If it needs to be performed asynchronously, I've had success running Akka workers triggered from inside my Dropwizard services.
as we know the struts interceptor execute and wait will take care of long running process by not getting the request to timeout and destroy it sends wait and at last the desired response i want to implement the same for long running process in spring and hibernate.
I recommend you to use DeferredResult of Spring. It´s a Future implementation, that use the http long poling technique.
http://docs.spring.io/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/web/context/request/async/DeferredResult.html
So let´s says that you will make a request, and the server it will return you the deferredResult, and then your request will keep it open until the internal process(Hibernate) finish his task. The timeout is configurable in the constructor.
Here another example http://www.javacodegeeks.com/2013/03/deferredresult-asynchronous-processing-in-spring-mvc.html
In order to keep the session open throughout the lifetime of a request we tie it to the view. This is done either by using Spring's OpenSessionInViewInterceptor or OpenSessionInViewFilter
An open session in view filter will ensure that the Hibernate session is kept open all the way up to the rendering of the view.
Or
You can use a task queue in the backend for a long running process like this.
Work Queues (aka: Task Queues) is to avoid doing a resource-intensive task immediately and having to wait for it to complete. Instead the tasks are scheduled to be done later. Task is encapsulated as a message and sent to a queue. A worker process running in the background will pop the tasks and eventually execute the job. When you run many workers the tasks will be shared between them.
This concept is especially useful in web applications where it's impossible to handle a complex task during a short HTTP request window.
I have a simple DropWizard service and I'd like a REST API to start a long running processing task - both CPU and I/O bound. REST call will not wait for task completion, notification will happen by polling/long polling/web socket.
For now, I'd prefer if I can do this in Dropwizard and keep everything in single deployable JAR. What are my options?
UPDATE: I am interested in what my options are regarding running long running tasks in Dropwizard, deployed as single jar without external dependencies. Just spawn a new thread? Assuming there are just few such requests it would probably work but there should be better options.
You probably want to use a managed resource:
https://dropwizard.io/en/stable/manual/core.html#managed-objects
to setup a thread pool. Then, your initial request could push a message onto a queue. Your thread pool can pull messages off the queue and process them asynchronously.
You could maybe provide an additional endpoint so that clients can obtain the current state of the asynchronous process.
I'm very confused about GAE's concepts of Tasks, Task Queues (both push and pull), Cron Jobs and how each of these relate to Frontend vs. Backend instances.
I'm trying to achieve a situation where some HTTP requests can be serviced immediately, whereas some get queued. Queued requests might ultimately end up triggering my own code to execute (once they are consumed) or they might hit one of the GAE service APIs (LogQuery, etc.).
I can't seem to wrap my head around how to design these two scenarios and let alone do the code up. To make things worse I've read literature that suggests there's certain task/queue-related coding you want to do differently depending on whether the code is executing on a Frontend or Backend instance. Thanks in advance for any help here! Bonus points for some concrete examples!
You write the code, Tasks and Cron execute it.
Task is a wraper for a set of properties, the main one is Url that should execute. You code (handler, servlet) should reside on that url. Task sit in the TaskQueues, which have certain default properties on how fast, how many in parallel, etc.. they execute the Tasks. So basically a To-Do list, that sequentially executes tasks with no guarantee when a task will start.
Cron is a service that periodically calls Url that you provided. In a sense its a scheduler.
Your Url (= your handler/servlet) can reside on frontend instance (default) or backend instance (must set special property on Task or in Cron settings). The main difference being that front requests must complete in 10min, while backend requests can take indefinitelly.
What is the purpose of Task Queue Java API? How does it work, and where should it be used?
The homepage looks pretty unambiguous:
With the Task Queue API, applications
can perform work outside of a user
request but initiated by a user
request. If an app needs to execute
some background work, it may use the
Task Queue API to organize that work
into small, discrete units, called
Tasks. The app then inserts these
Tasks into one or more Queues. App
Engine automatically detects new Tasks
and executes them when system
resources permit.
One thing GAE does is keep your request-response cycle very short lived to increase scalability. That is why lot of things like database access and http requests are handled asynchronously.
However there are requests that just can't be fully handled in realtime. This is either because such requests do some long computation (so they can be done in the background) or because they are periodic tasks like cron jobs that you need to schedule and execute repeatedly.
Tasks Queue let do you that.