views:

418

answers:

3

Suppose I have a message driven bean (MDB) in a Java application server. The MDB receives a message from a JMS queue and passes it to a message processor. In my case, a message processor is an extremely heavy weight object that requires extensive initialization so I don't want to create a new one to handle each message. Instead I would like to create a pool of message processors ahead of time and use them to handle messages.

So, my question is: what is the 'correct' way to build this pool in a J2EE app server? Do any servers have built-in support for defining custom (non-connection) object pools? I would like to leverage whatever built-in support there is for this pattern before I just cram the pool into a singleton and hope for the best. In particular:

  • How do I define/instantiate the pool?
  • How do I access the pool? JNDI?
  • What management capabilities are provided by the app server?

I know how to implement an object pool in general. My question is mostly about creating a pool in a J2EE app server.

I'm planning on using Glassfish, but I"m flexible if JBoss or something else will make this easier.

Thanks!

+1  A: 

You could try Apache Commons Pool, it's a generalised mechanism for pooling application objects.

skaffman
A: 

Java 5 comes with the Executor API that can do this.

Aaron Digulla
I'm not sure how this solves my problem, especially in the context of a J2EE app server.
Dave Ray
You just have to synchronize the start/stop of your application in the app server with the executor and it should work.
Aaron Digulla
+3  A: 

EJBs themselves are usually managed as pooled objects by most application servers.

The most obvious way to implement your application is to use the MDB itself as the message processor, and then configure the pooling using the application server deployment configuration, which of course is specific to the server you are actually using.

Massimiliano Fliri
I'll give this a try. I wonder about how much control I'll get. For example, if there is a period of inactivity, will the AS empty the pool of MDBs necessitating another long initialization process the next time a message comes in?
Dave Ray
This is really AS specific. My main experience is with JBoss and Weblogic, which both allow to customize passivation and disposal policies as well as pool size and behaviour. I think GlassFish is not different, and you can find relevant documentation here: http://docs.sun.com/app/docs/doc/820-7695/beaiu?a=view
Massimiliano Fliri