[英]JMS connection to remote client
我正在嘗試測試JMS應用程序,我沒有消費者的問題,但是當我嘗試運行具有以下代碼的生產者時
public class QueueProducer {
/**
* @param args
* @throws NamingException
* @throws JMSException
*/
public static void main(String[] args) throws JMSException, NamingException {
System.out
.println("--------Entering JMS Example QueueProducer--------");
Context context = QueueConsumer.getInitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context
.lookup("ConnectionFactory");
Queue queue = (Queue) context
.lookup("queue/zaneacademy_jms_tutorial_02");
QueueConnection queueConnection = queueConnectionFactory
.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false,
QueueSession.AUTO_ACKNOWLEDGE);
queueConnection.start();
QueueProducer queueProducer = new QueueProducer();
queueProducer.sendMessage("Message 1 From QueueProducer...",
queueSession, queue);
System.out.println("--------Exiting JMS Example QueueProducer--------");
}
public void sendMessage(String text, QueueSession queueSession, Queue queue)
throws JMSException {
QueueSender queueSender = queueSession.createSender(queue);
TextMessage textMessage = queueSession.createTextMessage(text);
queueSender.send(textMessage);
System.out.println("Message Sent : "+textMessage.getText());
queueSender.close();
}
}
它只顯示生產者中的消息,幾秒鍾后顯示此警告
WARN [SimpleConnectionManager] A problem has been detected with the connection to remote client 5c4o12- tsh1gl-hfybsrs4-1-hfybss2a-4, jmsClientID=b-l5ssbyfh-1-4srsbyfh-lg1hst-21o4c5. It is possible the client has exited without closing its connection(s) or the network has failed. All associated connection resources will be cleaned up.
可能需要關閉隊列連接。 嘗試添加queueConnection.close();
在main
結束時。
此外,需要關閉的資源應該在finally塊中完成。 即使在使用資源時發生異常,這也可確保資源處於關閉狀態。 例如:
QueueSender queueSender = ...
try {
// use queueSender
}
finally {
queueSender.close();
}
queueConnection
。
我遇到了同樣的問題。 我認為QueueConnection和QueueSession需要關閉。在上面的代碼片段中,附加代碼應該是:
queueConnection.stop();
queuesession.close();
queueConnection.close();
在此之后我的問題得到了解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.