Question

In Java I have a web service that starts some long running processes on different threads so that I can return a http status without the connection timing out.

My question involves creating a single long running thread that will simply run the ExecuterCompletionService.take.get() to get the threads that have completed and finish the processing of them.

will it be ok to run get the finished objects in that separate thread if the completion service is created in the main thread?

EG.

MainThread

  1. Create Completion service
  2. Accept requests and create long running process
  3. submit the Callable to the completion service
  4. If everything went well return HTTP status of 200

Monitor Thread

  1. Store a reference to the Completion service created in the Main thread.
  2. run completionservice.take.get()
  3. Generate report for completed task
  4. repeate

Is there anything special I need to worry about that I have overlooked? Like synchronizing issues. I know internally the completion service uses a blocking queue.

Was it helpful?

Solution

IMHO , I don't see a issue with your approach as long as you keep one ExecutorCompletionService instance which will used to submit multiple Callable Jobs.

in this approach, you will lose the running Job results when the server went down / crash . And you will be using the JVM memory to keep the Jobs live. Again it depends upon the volume of requests and number of Jobs would run in parallel and the time taken to complete the task.

if you dont requried to run all the request in parallel then for your use case , JMS would be the perfect fit . In order to avoid the request time out for long running process , we can place the job in JMS and in later you can consume the Job through JMSlistener (Message Listener / MDB ) and process one by one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top