简体   繁体   English

JMS队列接收导致应用程序崩溃

[英]JMS Queue receive causes application to crash

I've created a very simple JMS Queue example to send and receive messages. 我创建了一个非常简单的JMS Queue示例来发送和接收消息。 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. 我运行一次init来创建连接,生产者和接收者,然后运行sendMessage 10次。 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. 如果此后再打几次sendMessage,我的应用程序将崩溃。 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. 我正在运行glassfish应用程序Web服务器,并尝试使用队列来通知用户尝试访问的每个其余调用。

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. 添加1毫秒的超时即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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