Lọc bộ nhận tin nhắn JMS bằng JMSCorrelationID
Làm thế nào tôi có thể khởi tạo trình nghe xếp hàng JMS trong java (JRE/JDK/J2EE 1.4) mà chỉ nhận được các tin nhắn khớp với một JMSCorrelationID đã cho? Các thông điệp mà tôi đang tìm kiếm để nhận được đã được xuất bản cho một hàng đợi và không phải là một chủ đề, mặc dù điều đó có thể thay đổi nếu cần thiết.
Dưới đây là đoạn code mà tôi đang sử dụng để đưa thông điệp trong hàng đợi:/**
* publishResponseToQueue publishes Requests to the Queue.
*
* @param jmsQueueFactory -Name of the queue-connection-factory
* @param jmsQueue -The queue name for the request
* @param response -A response object that needs to be published
*
* @throws ServiceLocatorException -An exception if a request message
* could not be published to the Topic
*/
private void publishResponseToQueue(String jmsQueueFactory,
String jmsQueue,
Response response)
throws ServiceLocatorException {
if (logger.isInfoEnabled()) {
logger.info("Begin publishRequestToQueue: " +
jmsQueueFactory + "," + jmsQueue + "," + response);
}
logger.assertLog(jmsQueue != null && !jmsQueue.equals(""),
"jmsQueue cannot be null");
logger.assertLog(jmsQueueFactory != null && !jmsQueueFactory.equals(""),
"jmsQueueFactory cannot be null");
logger.assertLog(response != null, "Request cannot be null");
try {
Queue queue = (Queue)_context.lookup(jmsQueue);
QueueConnectionFactory factory = (QueueConnectionFactory)
_context.lookup(jmsQueueFactory);
QueueConnection connection = factory.createQueueConnection();
connection.start();
QueueSession session = connection.createQueueSession(false,
QueueSession.AUTO_ACKNOWLEDGE);
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setJMSCorrelationID(response.getID());
objectMessage.setObject(response);
session.createSender(queue).send(objectMessage);
session.close();
connection.close();
} catch (Exception e) {
//XC3.2 Added/Modified BEGIN
logger.error("ServiceLocator.publishResponseToQueue - Could not publish the " +
"Response to the Queue - " + e.getMessage());
throw new ServiceLocatorException("ServiceLocator.publishResponseToQueue " +
"- Could not publish the " +
"Response to the Queue - " + e.getMessage());
//XC3.2 Added/Modified END
}
if (logger.isInfoEnabled()) {
logger.info("End publishResponseToQueue: " +
jmsQueueFactory + "," + jmsQueue + response);
}
} // end of publishResponseToQueue method
Gần đây tôi đã đọc cùng một chủ đề và có câu hỏi như sau: người nhận vẫn nhận được những tin nhắn không chứa id tương quan bắt buộc và thả chúng một cách im lặng để xử lý hoặc sẽ Bản thân nhà cung cấp JMS không cung cấp các thông điệp như vậy cho người nhận, để họ vẫn còn trên hàng đợi? Tôi cảm thấy thứ hai là cách tiếp cận đúng, nhưng muốn xác minh. Cảm ơn. – shrini1000
@ shrini1000 bạn là chính xác. – Trying