简体   繁体   English

JMS连接到远程客户端

[英]JMS connection to remote client

I'm trying to test a JMS Application, I don't have a problem with the consumer , but when I try to run the producer which have the following code 我正在尝试测试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();
    }

}

It display just the message in producer, after a few seconds it display this warning 它只显示生产者中的消息,几秒钟后显示此警告

 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.

Maybe the queue connection needs to be closed. 可能需要关闭队列连接。 Try adding queueConnection.close(); 尝试添加queueConnection.close(); at the end of main . main结束时。

In addition, resources that need to be closed should be done in a finally block. 此外,需要关闭的资源应该在finally块中完成。 This ensures the resource is closed even if an exception occurs while working with the resource. 即使在使用资源时发生异常,这也可确保资源处于关闭状态。 For example: 例如:

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

Same thing for queueConnection . queueConnection

I have faced the same issue. 我遇到了同样的问题。 I think QueueConnection and QueueSession need to close.In above code snippet, addtional code should be : 我认为QueueConnection和QueueSession需要关闭。在上面的代码片段中,附加代码应该是:

        queueConnection.stop();
        queuesession.close();
        queueConnection.close();

My issue got resolved after this. 在此之后我的问题得到了解决。

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

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