簡體   English   中英

JMS隊列接收導致應用程序崩潰

[英]JMS Queue receive causes application to crash

我創建了一個非常簡單的JMS Queue示例來發送和接收消息。 我已將其設置為在發送一定數量后接收消息,然后對其進行處理。 收到所有消息后,嘗試發送更多消息會導致應用程序崩潰。

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;
                }
            }
        }
    }
}

我運行一次init來創建連接,生產者和接收者,然后運行sendMessage 10次。 第十次,它吐出所有十個接收到的消息的輸出。 如果此后再打幾次sendMessage,我的應用程序將崩潰。 我嘗試更改它以創建並在每條消息后都保持不變的情況下關閉連接。 我正在運行glassfish應用程序Web服務器,並嘗試使用隊列來通知用戶嘗試訪問的每個其余調用。

原來的問題是,由於沒有超時,接收被無限期地掛起。 添加1毫秒的超時即可解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM