Saturday, January 10, 2009

JSM messages left in queue, not consumed

I've been working on a project involving a servlet that had to consume messages from a JMS queue and dole them out to the requesting client as they arrived. For this I chose the asynchronous message consumption, to avoid blocking the servlet thread, to allow for other processing that had to be done while waiting for messages to arrive.

When testing I noticed that the servet would pick up the messages as they arrived in the queue, but for some reason they would be left there. At the same time the following exception would appear in the server log:

DirectConsumer:Caught Exception delivering messagecom.sun.messaging.jmq.io.Packet cannot be cast to com.sun.messaging.jms.ra.DirectPacket
.

After doing some investigations I found this notice in the J2EE tutorial: "The Java EE platform specification does not impose strict constraints on how web components should use the JMS API. In the Application Server, a web component can send messages and consume them synchronously but cannot consume them asynchronously".

Switching to synchronous consumption, using a timed receive took care of the problem.