简体   繁体   中英

JMS Queue receive causes application to crash

I've created a very simple JMS Queue example to send and receive messages. I have it set up to receive the messages after a certain number have been sent and then do work on them. After it receives all of the messages, trying to send more messages causes the application to crash.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.jms.*;

@Startup
@Singleton
public class JMSQueue {
    /** SLF4J logger. */
    @SuppressWarnings("unused")
    private final Logger log = LoggerFactory.getLogger(JMSQueue.class);

    @Resource(mappedName = "jms/__defaultQueue")
    private Queue queue;

    @Resource(mappedName = "jms/__defaultQueueConnectionFactory")
    private QueueConnectionFactory factory;

    private int count = 0;

    private QueueConnection connection;
    private QueueSession session;
    private MessageProducer producer;
    private QueueReceiver receiver;

    public void init(){
        try {
            connection = factory.createQueueConnection();
            session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            producer = session.createProducer(queue);
            receiver = session.createReceiver(queue);
            connection.start();
        } catch (JMSException e) {
            log.error("JMS Queue Initialization failed.", e);
        }
    }

    public void sendMessage() throws JMSException {
        String messageBody = "ping" + count;
        Message request = session.createTextMessage(messageBody);
        request.setJMSReplyTo(queue);
        producer.send(request);
        count++;
        if (count >= 10) {
            count = 0;
            Message response = receiver.receive();
            while (response != null){
                String responseBody = ((TextMessage) response).getText();
                log.debug("jms - " + responseBody);
                try {
                    response = receiver.receive();
                } catch(JMSException e){
                    response = null;
                }
            }
        }
    }
}

I run init once to create the connection, producer, and receiver, and then I run sendMessage 10 times. On the tenth time it spits out the output of all ten received messages. If I then hit sendMessage a couple of times after that, my application crashes. I have tried changing it to create and close the connection after each message which didn't change anything. I'm running a glassfish application web server and trying to use the queue to be notified of every rest call that users try to access.

Turns out the issue was that the receive was hanging indefinitely due to there not being a timeout. Adding a timeout of 1 millisecond solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM