jakarta-ee - 通过JMSCorrelationID过滤JMS消息接收器

标签 jakarta-ee queue jms

如何在Java(JRE/JDK/J2EE 1.4)中实例化仅接收与给定JMSCorrelationID匹配的消息的JMS队列监听器?我希望接收的消息已发布到队列中,而不是主题,尽管如果需要可以更改。
这是我当前用于将消息放入队列中的代码:

/**
 * 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 

最佳答案

队列连接设置相同,但是一旦有了QueueSession,就可以在创建接收方时设置选择器。

    QueueReceiver receiver = session.createReceiver(myQueue, "JMSCorrelationID='theid'");

然后
receiver.receive()

或者
receiver.setListener(myListener);

关于jakarta-ee - 通过JMSCorrelationID过滤JMS消息接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/149037/

相关文章:

java - 我的 peek() 方法不会返回队列的头部

java - 为什么将客户端 JBoss 连接回收到远程队列后仍然抛出 SpyJMSExceptions?

java - 将对象的 ArrayList 拆分为大小相等的 block ,例如在 Java 中为 1 MB

jakarta-ee - 在不同机器上具有层的 Netbeans Java EE 项目

c - 队列类型数据结构使用数组实现

jakarta-ee - ActiveMQ 独立服务器部署或嵌入 Spring Webapp

java - Active MQ - HelloWorld 示例异常

tomcat 服务器上的 javax.net.ssl.SSLHandshakeException

java - Weblogic 类加载器多次加载类

java - 抛出 NullPointerException 并且程序未按预期终止