1

HI guys I have to develop an application where i have to build a bridge between two enterprise application and I am using JMS to establish the communication between these two application. Here I am using Message Driven Bean to send the JMS message from one application to another. I am struck where when my one application can send the message to other one but can not send the reply back to the requestor one.

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);
    }
}

But when this message is received by the replier I does print the getReplyTo destination but when i build a reply and send it back it set the destination value as null and reply is never executed. the replier code is given below

@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);
        }
    }
}

so what is happening that i can send a message but can not receive the reply. please help me what am i missing or doing wrong. thanks

Eric Sant'Anna
  • 267
  • 4
  • 17
monty
  • 23
  • 5

1 Answers1

0

It looks like you're using the same queue (myTestQueue) both for the request message and the reply message. This is bad because, you're receiver might be receiving the reply message instead of the sender/requestor.

In general, when doing request/reply with JMS, using javax.jms.QueueRequestor should be better than doing a "manual" implementation with sending the request message followed by trying to receive it.

Miichi
  • 1,739
  • 1
  • 14
  • 16