java - 与远程客户端的 JMS 连接

标签 java jakarta-ee jms

我正在尝试测试 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.

最佳答案

也许需要关闭队列连接。尝试在 main 末尾添加 queueConnection.close();

此外,需要关闭的资源应该在finally block 中完成。即使在使用资源时发生异常,这也可确保资源关闭。例如:

QueueSender queueSender = ...
try {
    // use queueSender
}
finally {
    queueSender.close();
}

对于queueConnection也是如此。

关于java - 与远程客户端的 JMS 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223460/

相关文章:

java - JSESSIONID 在 cookie 中获取两次

java - 无状态 session bean 中的多线程?

java - 需要不同的父子命名空间前缀

java - 按对象内部的值过滤 RecyclerView

java - 使用动态代理时,如何访问底层对象的注释?

java - J2EE : Singleton vs keeping things in session

java - 使用 <payload> 元素的 Citrus <TestMessage> 子标签时,出现以下错误

java - JMS 中的动态消息选择器

java - 将数据发送到队列并在同一函数中监听数据

java - Java 处理链中的作业排队