服务端向queue 发送一条消息, 并等待客户端进行处理,然后接收客户端返回的消息
服务端在向客户端发送消息的时候设置了JMSReplyTo ,msg.setJMSReplyTo(responseQueue);
然后客户端取得responseQueue ,并将返回的消息发送到responseQueue,以便服务端进行接收
但是responseQueue 队列里的消息有很多,需要区分哪一条是服务器端想要接收和,于是需要进行消息过滤
MessageConsumer (QueueReceiver, TopicSubscriber 的你类) 消息处理类有两种处理方式 ,同步或异步
receive() 同步
setMessageListener(MessageListener listener) 异步
server
msg.setJMSReplyTo(responseQueue);
String filter = “JMSCorrelationID='” + msg.getJMSMessageID() + “‘”;
session.createReceiver(responseQueue, filter);
client
responseMsg.setJMSCorrelationID(msg.getJMSMessageID());
Queue responseQueue = (Queue) msgObj.getJMSReplyTo();
session.createSender(responseQueue).send(responseMsg);
jndi.properties
connectionFactoryNames = connectionFactory,queueConnectionFactory,topicConnectionFactry,connF
queue.requestQueue=jms.queue1
queue.responseQueue=jms.queue2
// 处理客户端反馈回来的消息
String filter = “JMSCorrelationID='” + msg.getJMSMessageID() + “‘”;
QueueReceiver receiver = session.createReceiver(responseQueue,
filter);
TextMessage resMsg = (TextMessage) receiver.receive(20 * 1000);
if (resMsg == null) {
System.out.print(“客户端超时,无法收到客户端返回的消息;”);
} else {
System.out.println(“客户端返回的信息是:” + resMsg.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
public void exit() {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
System.exit(0);
}
public static void main(String[] args) {
ChatServer server = new ChatServer();
server.request();
server.exit();
}
}
}
}
过滤
filter
server
String uuid = UUID.randomUUID().toString();
msg.setStringProperty(“uuid”, uuid);
String filter = “JMSCorrelationID='” + uuid + “‘”;
QueueReceiver receiver = session.createReceiver(responseQueue, filter);
client
responseMsg.setJMSCorrelationID(msg.getStringProperty(“uuid”));