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.
Related
On the Springboot-based application, I am creating a thread to finish a batch job and returning back immediately. Now, I need to forcefully kill the thread in some scenarios like a timeout.
Can anyone help me to fix this use case?
If you're using Java's Executors Framework, you can simply use shutDown() method.
Otherwise, you can send an interrupt signal using Thread.interrupt() after your task is completed.
You can use Javamelody monitoring tool integrated in your application which provides you a nice dashboard of all the current running thread and a kill button to terminate the thread. This tool is extremely easy to integrate in Spring Boot applications - you just need to include couple of dependencies in your build file. Also you would like to protect the dashboard endpoint with some authentication - javamelody provides you with basic auth feature as well.
Another DIY way is to create an endpoint in your application to return thread id for the batch process ( you would need to give some meaningful name to tth thread group to differentiate it from other threads) and another endpoint which takes in thread id as parameter and can stop/kill the thread.
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.
I am using Spring Boot, and I have a POST endpoint which needs to do various things such as persist an object to the database, and then also call 3-4 other services. However, I'd like to return a response shortly after the database call is persisted and then call the other services on another thread, asynchronously? The call to the other 3-4 services are okay to be eventually consistent (e.g. call to keen.io analytics service). How is this easily doable in Spring Boot?
I think that you do not even need spring for this job. You could use java.util.concurrent.CompletableFuture#runAsync to run sth async on a different thread.
Also you could use the spring async support. Just annotate a spring bean method (in your case returning void) with #Async. And do not forget to enable async support by annotating a configuration class with #EnableAsync.
One solution is to have a thread running that is monitoring a queue to determine when to execute some work. When a request has completed its immediate task then it would add information to the queue so that the monitoring thread can understand that it needs to do some work. You could have a pool of threads monitoring the queue to improve performance. This is a fairly standard pattern for event based programming which offers ability to have actions run async.
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.