I have a Java RESTful webservice that will be called about every 10 seconds by another process. If conditions are right, the webservice needs to perform a potentially extensive ETL process (say 10 – 20 seconds). However, we want to return to the calling application right away indicating the payload was delivered successfully to the web service.
Summary of Requirements:
- Validate authentication and input parameters.
- If Validation fails, return error in XML.
- Start thread performing ETL process. Process will only proceed under certain conditions.
- Return XML indicating payload delivered and validation of input passed.
I have coded this and it appears to run correctly. But it doesn’t seem right. The thread I created will potentially run longer than the main web service thread.
Anyone want to point me to a better way of doing this?
that is exactly the right way to do it. what “doesn’t seem right” about the solution?
my only suggestion would be that you shouldn’t just launch threads willy-nilly. you should have some sort of shared ExecutorService managing these ETL process threads. that will give you visibility into what has been launched and is still running as well as the ability to limit/control the number of background processes (and potentially deal with any rogue executions).
you also might want to generate some sort of “jobId” for the launched process and return that to the caller. then you could add a call to your api which a client could use to check on the status of a job.