0
votes

Scenario

I am consuming messages from a WebLogic Queue by implementing messageListener. The onMessage() call is successful and the message received can be printed from within the onMessage function.

Requirement

To process this msgText as soon as it received and return the processed result to a calling method.

Code

@Override

public void onMessage(Message msg) {

try {
    String msgText;
    if (msg instanceof TextMessage) {
        msgText = ((TextMessage) msg).getText();
    } else {
        msgText = msg.toString();
    }
    System.out.println(msgText);

} catch (JMSException ex) {
    ex.printStackTrace();
}

}

2
What problem are you having?durron597
the intention is for a result to be returned to a calling method in another classukuta

2 Answers

0
votes

It is not possible directly because your message-driven bean doesn't have a clue which method posted the message on a message queue and generally there's no callback mechanism in MDBs.

But, there is a trick that can help. It is called TemporaryQueue and it should be used as follows. In the message producer add this portion of code:

// OPEN CONNECTION AND CREATE SESSION
..
TemporaryQueue tempQueue = session.createTemporaryQueue();
msg.setJMSReplyTo(tempQueue);
// SEND MESSAGE
..
MessageConsumer mc = session.createConsumer(tempQueue);
Message retMsg = mc.receive(); //WAITING FOR A RESPONSE..

In consumer (your message-driven bean) use this temporary queue:

Queue destination = (Queue) message.getJMSReplyTo();
// CREATE PRODUCER, CREATE MESSAGE AND SEND THE MESSAGE

This way you will simulate callback mechanism. Object retMsg should contain processed result and you are done.

0
votes

Are you trying to return to the result of the processed message to calling function? If so, I think it will not be possible here, since this will be an asynchronous call.

If not, please post your question clearly.