如何在Java(JRE/JDK/J2EE 1.4)中實例化JMS隊列偵聽器,該偵聽器只接收與給定的JMSCorrelationID匹配的消息?我正在尋找的信息已發佈到隊列中,而不是主題,但如果需要,這些信息可能會更改。
下面是我目前使用把消息隊列代碼:/**
* 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
我最近一直在閱讀上的同一主題,並有一個問題如下:將接收器仍然收到不包含所需的相關ID的消息,並自動刪除它們W/O處理,還是會JMS提供程序本身不會將這些消息傳遞給接收方,以便它們仍然在隊列中?我覺得後者是正確的做法,但要驗證。謝謝。 – shrini1000 2011-08-03 04:41:42
@ shrini1000你是對的。 – Trying 2013-10-21 22:21:15