1、一个Queue时候,Sender和Receiver使用同一个Queue
[Sender]
      QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
      QueueSender queueSender = queueSession.createSender(queue);

      TextMessage textMessage = queueSession.createTextMessage();
      textMessage.clearBody();
      textMessage.setText(message);

      queueSender.send(textMessage);

[Receiver]
   QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
   QueueReceiver queueReceiver = queueSession.createReceiver(queue);   

   queueReceiver.setMessageListener(msgRcvr);
   
   //msgRcvr-->MessageListener的onMessage方法
   public synchronized void  onMessage(Message message) {
   if (message instanceof TextMessage) {
        TextMessage textMessage = (TextMessage) message;
    try {
         System.out.println("Message content is:" + textMessage.getText());
    }catch (JMSException e) {
         e.printStackTrace();
    }
   }
  }

 



2、两个Queue时候
   a.可以使用两个Queue
   b.可以是一个普通的Queue,一个TemporaryQueue
  
[Sender]
      QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
      Queue tempQueue = queueSession.createTemporaryQueue();
      String cID = (int)(Math.random()*10)+"";
      
      TextMessage textMessage = queueSession.createTextMessage();      
      textMessage.setJMSCorrelationID(cID);
      textMessage.setJMSReplyTo(tempQueue);
      textMessage.setText(message);
     
      QueueSender queueSender = queueSession.createSender(senderQueue);
      queueSender.send(textMessage);
     
      QueueReceiver queuereceiver = queueSession.createReceiver(tempQueue,"JMSCorrelationID='" + cID +"'");
      Message msg = queuereceiver.receive(5000);
      if (msg instanceof TextMessage) {
       System.out.println("RETURN Message:" + ((TextMessage)msg).getText());
      }


[Receiver]
   public synchronized void  onMessage(Message message) {
   if (message instanceof TextMessage) {
     TextMessage oldMessage = (TextMessage) message; 
              
     Queue tempQueue = (Queue)oldMessage.getJMSReplyTo();
     queueSender = queueSession.createSender(tempQueue);
     
     TextMessage newMessage = queueSession.createTextMessage();  
     newMessage.setJMSCorrelationID(oldMessage.getJMSCorrelationID());
     newMessage.setText("test");
     
     queueSender.send(newMessage);

  }

 


【注】
      1)JMSCorrelationID 比较关键,只是一个身分标识,上面有3个地方使用
      2)Template 用于临时template中,她的设置影响 cID
如果String cID = (Math.random()*10) + ""; 抛出“Byte quota exceeded for...”于template 设置有关