1
votes

I have a program where a session bean is created in one ejb project and uses a queue to send a message over to a message driven bean at a separate project. At the message driven bean, I manage to retrieve the message sent over from the session bean. The message will then be used to get another value, which I have to send back to the session bean. Any ideas on how to do it. Many thanks for any help given.

ManagerBean:(partial code)

            queueConnection=queueConnectionFactory.createConnection();
            session=queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            replyQueue=session.createTemporaryQueue();
            consumer=session.createConsumer(replyQueue);
            consumer.setMessageListener(new LpsListener());
            queueConnection.start();
            producer=session.createProducer(queue);

            message=session.createMapMessage();
            message.setJMSReplyTo(replyQueue);
            producer.send(message);

and my MDB:(partial in OnMessage)

            msg=(MapMessage)message;

            String memId=msg.getString("MemberId");
            int fine=mmr.getFine(memId);

            queueConnection=queueConnectionFactory.createConnection();
            replyDest=msg.getJMSReplyTo();
            replyCorrelationMsgId=msg.getJMSMessageID();
            queueSession=queueConnection.createSession(true, 0);
            queueProducer=queueSession.createProducer(replyDest);
            replyMsg=createReplyMsg(queueSession, replyCorrelationMsgId);
            queueProducer.send(replyMsg);

            private ObjectMessage createReplyMsg(Session session, String msgId)throws                      JMSException{
            ObjectMessage replyMsg=session.createObjectMessage();
            replyMsg.setIntProperty("fine", fine);
            replyMsg.setJMSCorrelationID(msgId);
            return replyMsg;
            }
2
Can you add code for createReplyMsg.Nayan Wadekar
@NayanWadekar okay. addeduser1097856
You have to configure ManagerBean to listen to the specific queue to which MDB replies. It's difficult to pinpoint issue, try to debug yourself, whether message sending fails, is it received properly at other end etc.Nayan Wadekar
@NayanWadekar so the code for my mdb should be correct? by the code i have written in ManagerBean, I have set a listener for any reply by the mdb, where the destination is supplied by message.setJMSReplyTo(replyQueue). I have tried my best to debug, still not sure where the problem lies.user1097856

2 Answers

0
votes

If you want to send the message back to a new instance of the bean. ie. A stateless bean, you Can Just Inject a new instance of the Bean using @EJB annotation. If You want to send it to a specific Stateful bean, then you should have some kind of way of identifying the Bean in the first message, like a bean id. This id will then be used to identify the bean, when you're sending the return message.

0
votes

You can use JMSCorrelationID in conjunction with JMSReplyTo to have request/response mechanism with JMS.

Below is the untested sample code.

Manager Bean

message.setJMSReplyTo(queue);
String correlationId = generateRandomString();
message.setJMSCorrelationID(correlationId);
producer.send(message);

Message Driven Bean

responseMessage.setJMSCorrelationID(requestMessage.getJMSCorrelationID());
queueProducer.send(requestMessage.getJMSReplyTo(), responseMessage);

Edit : Excerpts from documentation to clarify further.

  • JMSCorrelationID : A client can use the JMSCorrelationID header field to link one message with another. A typical use is to link a response message with its request message.

  • JMSReplyTo : The JMSReplyTo header field contains the destination where a reply to the current message should be sent. In some cases a client may wish to match a request it sent earlier with a reply it has just received. The client can use the JMSCorrelationID header field for this purpose.

[Emphasis mine]