2013-09-29 81 views
1

嗨,大家好我必須開發一個應用程序,我必須在兩個企業應用程序之間建立橋樑,並使用JMS建立這兩個應用程序之間的通信。在這裏,我使用Message Driven Bean將JMS消息從一個應用程序發送到另一個應用程序。當我的一個應用程序可以將消息發送給其他應用程序,但無法將應答程序發回給請求者時,我感到非常震驚。如何在企業Java應用程序中發送jms回覆

private void sendJMSMessageToMyTestQueue(Message messageData) { 
    try { 
     context.createProducer().send(myTestQueue, messageData); 
     System.out.println("\tTime:  " + System.currentTimeMillis() + " ms"); 
     System.out.println("\tMessage ID: " + messageData.getJMSMessageID()); 
     System.out.println("\tCorrel. ID: " + messageData.getJMSCorrelationID()); 
     System.out.println("\tReply to: " + messageData.getJMSReplyTo()); 
     //System.out.println("\tContents: " + messageData.getText()); 
    } catch (JMSException ex) { 
     Logger.getLogger(HomeLoanJMS.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 



private void recieveSync() { 

    Message msg = context.createConsumer(myTestQueue).receive(); 
    if (msg instanceof TextMessage) { 
     try { 
      TextMessage tms = (TextMessage) msg; 
      System.out.println("Received reply "); 
      System.out.println("Received reply "); 
      System.out.println("\tTime:  " + System.currentTimeMillis() + " ms"); 
      System.out.println("\tMessage ID: " + tms.getJMSMessageID()); 
      System.out.println("\tCorrel. ID: " + tms.getJMSCorrelationID()); 
      System.out.println("\tReply to: " + tms.getJMSReplyTo()); 
      System.out.println("\tContents: " + tms.getText()); 
      System.out.println("Coming Reply: ---->>>>" + tms.getText()); 

     } catch (JMSException ex) { 
      Logger.getLogger(HomeLoanJMS.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

@Override 
public void sendMessage(String message) { 
    try { 
     TextMessage msg = context.createTextMessage(message); 
     // msg.setJMSType("Textmsg"); 
     msg.setText(message); 
     msg.setJMSReplyTo(myTestQueue); //setting reply destination 
     sendJMSMessageToMyTestQueue(msg); 
     System.out.println("after reply"); 
    // recieveSync(); 
    } catch (JMSException ex) { 
     Logger.getLogger(HomeLoanJMS.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

但是,當由回答者收到此消息,我不打印getReplyTo目的地,但是當我建立一個答覆,併發送回它設定的目標值爲空,並且永遠不會執行答覆。回覆代碼如下

@MessageDriven(activationConfig = { 
     @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") 
    , @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/myTestQueue") 
}) 
public class TestingMDB implements MessageListener { 
    @EJB 
    private SavingBeanRemote savingBean; 
    @Resource(mappedName = "jms/myTestQueue") 
    private Queue myTestQueue; 
    @Inject 
    @JMSConnectionFactory("jms/myTestQueueConnectionFactory") 
    private JMSContext context; 

    public TestingMDB() { 
    } 

    @Override 
    public void onMessage(Message message) { 
     try { 
      if (message instanceof MapMessage) { 
       MapMessage mmsg = (MapMessage) message; 
       String id = mmsg.getString("messageType"); 
       System.out.println(id); 
       String cid = mmsg.getString("C_ID"); 
       System.out.println(cid); 

      } else if (message instanceof TextMessage && (message.getJMSReplyTo() != null)) { 
       TextMessage tm = (TextMessage) message; 
       System.out.println("Received request"); 
       System.out.println("\tTime:  " + System.currentTimeMillis() + " ms"); 
       System.out.println("\tMessage ID: " + tm.getJMSMessageID()); 
       System.out.println("\tCorrel. ID: " + tm.getJMSCorrelationID()); 
       System.out.println("\tReply to: " + tm.getJMSReplyTo()); 
       System.out.println("\tContents: " + tm.getText()); 
       String contents = tm.getText(); 

       Double balance = savingBean.getBalance("c0566664", "a4016692"); 
       String text = (String)balance.toString(); 
       Destination replyDestination = (Destination)message.getJMSReplyTo(); 

       TextMessage replyMessage = context.createTextMessage(); 
       replyMessage.setText(text); 
       replyMessage.setJMSCorrelationID(tm.getJMSMessageID()); 
       context.createProducer().send(replyDestination, replyMessage); 
       System.out.println("Sent reply"); 
       System.out.println("\tTime:  " + System.currentTimeMillis() + " ms"); 
       System.out.println("\tMessage ID: " + replyMessage.getJMSMessageID()); 
       System.out.println("\tCorrel. ID: " + replyMessage.getJMSCorrelationID()); 
       System.out.println("\tReply to: " + replyMessage.getJMSReplyTo()); 
       System.out.println("\tContents: " + replyMessage.getText()); 
       TextMessage tms = context.createTextMessage("Reply from Client"); 

       for (int i = 0; i < 2; i++) { 
        try { 
         try { 
          System.out.println(message.equals(tm)); 

          Thread.sleep(1000); 
          System.out.println(tm.getText()); 
          System.out.println(tm.getText()); 

         } catch (InterruptedException ex) { 
          Logger.getLogger(TestingMDB.class.getName()).log(Level.SEVERE, null, ex); 
         } 

        } catch (JMSException ex) { 
         Logger.getLogger(TestingMDB.class.getName()).log(Level.SEVERE, null, ex); 
        } 

       } 

      } else { 
       System.out.println("error"); 
      } 
     } catch (JMSException ex) { 
      Logger.getLogger(TestingMDB.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

所以發生了什麼,我可以發送消息,但無法收到答覆。請幫助我,我失去了什麼或做錯了什麼。謝謝

回答

0

它看起來像你使用相同的隊列(myTestQueue)都請求消息和回覆消息。這很糟糕,因爲您接收者可能會收到回覆消息而不是發件人/請求者。

一般來說,在做JMS請求/回覆時,使用javax.jms.QueueRequestor應該比發送請求消息並嘗試接收請求消息時執行「手動」實現要好。

相關問題