Hello,

I'm trying to retrieve the response-message through the correlation-id.

This is the code I now have. Anyone any idea how I can find the right message.

public String sendMessage(String sendMessage) {
Queue tmpQueue=null;
TextMessage messageTxt = null;
String correlationId=null;
String returnValue = "";
String myFilter;
Context ctx;
try {
System.out.println("START");
System.out.println("sendMessage : " + sendMessage);
ctx = (Context) new InitialContext().lookup("java:comp/env");
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
.lookup("jms/MyQCF");
QueueConnection qc = qcf.createQueueConnection();
Queue q = (Queue) ctx.lookup("jms/MyQSender");
QueueSession qs = qc.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
TextMessage tm = qs.createTextMessage();
tm.setText(sendMessage);
QueueSender sender = qs.createSender(q);
((MQQueue)q).setTargetClient ( 1);


// set the reply queue
tmpQueue = (Queue) ctx.lookup("jms/MyQReply");
Destination dest = tmpQueue;

tm.setJMSReplyTo(tmpQueue);

System.out.println("Queue name : " + q.getQueueName());
sender.send(tm);
// retrieve JMS id
correlationId = tm.getJMSCorrelationID();
System.out.println("Correlation-id : " + correlationId);
myFilter = "JMSCorrelationID = '" + correlationId + "'";
sender.close();

// retrieve message
qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
q = (Queue) ctx.lookup("jms/MyQReceiver");
System.out.println("Creating receiver");
QueueReceiver receiver = qs.createReceiver(q);
qc.start();
while (true) {

Message message = receiver.receive(1000);
if (message != null) {
if (message instanceof TextMessage) {
messageTxt = (TextMessage) message;
returnValue = messageTxt.getText();
System.out.println("Received message: "
+ messageTxt.getText());
} else {
System.out.println("Breaking out");
break;
}
}
break;
}
receiver.close();
qs.close();

qc.close();

} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
return returnValue;
}