2013-04-25 117 views
0

我试图测试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. 

回答

1

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

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

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

同样的事情为queueConnection

+0

谢谢,它的工作 但仍然有一个小问题,这是当我运行生产者,它的发送,但消费者没有收到它 – Sunrise 2013-04-25 20:48:29

1

我面临同样的问题。我认为QueueConnection和QueueSession的需要close.In上面的代码片段,addtional代码应该是:

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

我的问题得到了在此之后解决。

相关问题