tags:

views:

199

answers:

6

For one of the URLs that my clients will be calling, I want it to return as soon as possible with minimal disruption, so even if the database is down or slow, the request still returns pretty fast.

I still need to do some processing of the data sent, so I am thinking about having a separate "queue" that holds the data and then process in almost realtime but in a separate thread.

Before I go off and start writing this queue I wanted to ask if there are readily available classes/libraries to do this ?

This is a java web app, deployed with jboss.

+9  A: 

Creating your own threads in a J2EE application is strongly discouraged and is outside the J2EE specification. I don't think you'll find a J2EE library to do what you want.

One of the main benefits of J2EE is that the server manages all your threads (and other resources) for you. If you start creating your own threads, the server doesn't know about them and can't look after them, so you're opening yourself up for potential resource leak problems amongst other headaches. To coin a phrase, you don't get a dog and bark yourself.

You'd be better off trying to create a solution that uses JMS. JMS is all about processing queues of messages so should be a good match to your requirements. Here's an article that talks about this a bit.

Dave Webb
Is it possible to ask the J2EE container to create a thread that can then be use to perform expensive database queries?
Steve Kuo
The container does thread pooling and data session pooling for free for you. The idea is that the developer shouldn't worry about these things. Of course a good container will offer extensive configuration options.
kgiannakakis
that's what I was afraid of, JMS feels a bit heavy handed for what I thought should be an easy fix.
webclimber
@webclimber - this is possible by using the commonj WorkManager as I indicated in my answer.
Robin
+2  A: 

An alternative to JMS will be to persist the requests in a database and then process them altogether with a scheduler. You could use Quartz of JEE timers for that.

kgiannakakis
A: 

Isn't this a perfect Scenario to rely on Ajax? Your long loading page is returned immediatly and contains an javascript ajax request to get your long processing request. While the request is pending, the user is faced with a spinner or other way to inform him, that the request might take some time. once it has returned, the response is inserted into your page via javascript.

Lars
Actually there is no guarantee the user will be there, and this part of the processing is not relevant to the user's request (at least to that one).
webclimber
A: 

I've used Doug Lea's PooledExecutor with great success to handle this kind of things. There's also the ExecutorService nowadays directly in Java 5 and newer.

There were some comments that it is strongly discouraged to use your own threads in J2EE. Well, anytime you do threading yourself you have to be careful and this is no different really. I find the J2EE way of letting the server take care of everything a bit disturbing and don't like the way it is limiting the design choices you can take.

Just my 50 cents...

Kaitsu
+1  A: 

Actually, you can create the threads you want using commonj. It provides a light weight way of using threads and timers within the JEE containers. More info here as well as this question has been raised many times.

This allows you to confirm to the restrictions of resource management within JEE and avoid the more heavyweight solution of JMS (although it will work as well).

Robin
A: 

The implementation ended up being a JMS message and a few Message Driven Beans processing the data wich has worked great and scaled pretty nice sofar.

I appreciate all the help and comments, specially the commonj, as well as the warnings about creating thread manually.

webclimber