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();
}
}
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:
In consumer (your message-driven bean) use this temporary queue:
This way you will simulate callback mechanism. Object
retMsgshould contain processed result and you are done.