Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous processing in Java from a servlet

Tags:

I currently have a tomcat container -- servlet running on it listening for requests. I need the result of an HTTP request to be a submission to a job queue which will then be processed asynchronously. I want each "job" to be persisted in a row in a DB for tracking and for recovery in case of failure. I've been doing a lot of reading. Here are my options (note I have to use open-source stuff for everything).

1) JMS -- use ActiveMQ (but who is the consumer of the job in this case another servlet?)

2) Have my request create a row in the DB. Have a seperate servlet inside my Tomcat container that always runs -- it Uses Quartz Scheduler or utilities provided in java.util.concurrent to continously process the rows as jobs (uses thread pooling).

I am leaning towards the latter because looking at the JMS documentation gives me a headache and while I know its a more robust solution I need to implement this relatively quickly. I'm not anticipating huge amounts of load in the early days of deploying this server in any case.

A lot of people say Spring might be good for either 1 or 2. However I've never used Spring and I wouldn't even know how to start using it to solve this problem. Any pointers on how to dive in without having to re-write my entire project would be useful.

Otherwise if you could weigh in on option 1 or 2 that would also be useful.

Clarification: The asynchronous process would be to screen scrape a third-party web site, and send a message notification to the original requester. The third-party web site is a bit flaky and slow and thats why it will be handled as an asynchronous process (several retry attempts built in). I will also be pulling files from that site and storing them in S3.

like image 304
Ish Avatar asked Jan 31 '09 08:01

Ish


2 Answers

Your Quartz Job doesn't need to be a Servlet! You can persist incoming Jobs in the DB and have Quartz started when your main Servlet starts up. The Quartz Job can be a simple POJO and check the DB for any jobs periodically.

However, I would suggest to take a look at Spring. It's not hard to learn and easy to setup within Tomcat. You can find a lot of good information in the Spring reference documentation. It has Quartz integration, which is much easier than doing it manually.

like image 57
dlinsin Avatar answered Sep 28 '22 07:09

dlinsin


A suitable solution which will not require you to do a lot of design and programming is to create the object you will need later in the servlet, and serialize it to a byte array. Then put that in a BLOB field in the database and be done with it.

Then your processing thread can just read the contents, deserialize it and work with the ressurrected object.

But, you may get better answers by describing what you need your system to actually DO :)

like image 30
Thorbjørn Ravn Andersen Avatar answered Sep 28 '22 06:09

Thorbjørn Ravn Andersen