sdoca
sdoca

Reputation: 8042

JMS/Glassfish - MDB Not Consuming Queue Messages

I am using JMS for the first time and am using Glassfish 3.1.1. I have set up a JMS Connection Factory:

Pool Name: jms/QueueConnectionFactory
JNDI Name: jms/QueueConnectionFactory
Resource Type: javax.jms.QueueConnectionFactory

and a Destination Resource:

JNDI Name: jms/ProcessBatchQueue
Physical Destination: ProcessBatchQueue
Resource Type: javax.jms.Queue

I have deployed a war with a servlet that accepts a file, parses it and saves the contents to a database. If this is all successful it sends a message to the queue:

@Resource(lookup = "jms/ProcessBatchQueue")
private Queue processBatchQueue;

private void sendProcessBatchMessage(String batchID) throws JMSException
{
    log.info("Attempting to send process batch message for batch ID: "
            + batchID);

    Connection jmsConnection = connectionFactory.createConnection();
    Session jmsSession = jmsConnection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);

    TextMessage message = jmsSession.createTextMessage();
    message.setText(batchID);

    MessageProducer msgProducer = jmsSession.createProducer(processBatchQueue);
    msgProducer.send(message);

    jmsConnection.close();
}

I have a deployed an ear with an MDB that should be listening to the queue and actioning the message:

@MessageDriven(mappedName = "jms/ProcessBatchQueue")
public class BatchReceiver
{
    private final Logger log = LoggerFactory.getLogger(BatchReceiver.class);

    public void onMessage(Message message)
    {
        log.info("Received message from jms/ProcessBatchQueue: " + message);

        try
        {
            if (message instanceof TextMessage)
            {
                String batchId = ((TextMessage) message).getText();
                // do processing
            }
            else
            {
                log.error("Received invalid message type from jms/ProcessBatchQueue");
            }
        }
        catch (Exception ex)
        {
            String error = "Received error '" + ex.toString()
                    + "' retrieving message from jms/BatchProcessingTopic.";

            Throwable linkedEx = ex.getCause();

            if (linkedEx != null)
            {
                log.error(error += "Linked exception: " + linkedEx.getMessage(),
                    linkedEx);
            }
            else
            {
                log.error(error + ", " + ex.getMessage(), ex);
            }
        }
    }
}

In my war logs, I get the

log.info("Attempting to send process batch message for batch ID: " + batchID);

statement, but in the ear logs, I get nothing that would indicate that the MDB is receiving the message.

My understanding is that I should be able to "just" deploy the ear with the MDB and it should start receiving the messages. Is there a config step that I've missed?

Is there some way to confirm that the message generated in the servlet is making it to the queue in the first place? There are no errors in any log including server.log.

Upvotes: 1

Views: 1718

Answers (1)

MaDa
MaDa

Reputation: 10762

Your bean does not implement javax.jms.MessageListener, it just has an onMessage() method with a the same signature.

It's also possible that you miss the activationConfig part of the annotation, but I'm not sure if it's required in Java EE 6. See if it's the case anyway:

@MessageDriven(
    activationConfig = { 
            @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
            @ActivationConfigProperty(propertyName = "destination", propertyValue = "ProcessBatchQueue")},
    mappedName = "jms/ProcessBatchQueue")

Upvotes: 2

Related Questions