当前位置:  开发笔记 > 运维 > 正文

JMS消息接收器通过JMSCorrelationID过滤

如何解决《JMS消息接收器通过JMSCorrelationID过滤》经验,为你挑选了2个好方法。

如何在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 

Robin.. 11

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

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

然后

receiver.receive()

要么

receiver.setListener(myListener);


James Strach.. 5

BTW虽然它不是您提出的实际问题 - 如果您正在尝试通过JMS实现请求响应,我建议您阅读本文,因为JMS API比您想象的要复杂得多,并且有效地执行此操作要比它更难看起来.

特别是为了有效地使用JMS,您应该尽量避免为单个消息等创建消费者.

另外,因为JMS API非常复杂,无法正确有效地使用 - 特别是对于池,事务和并发处理 - 我建议人们从应用程序代码中隐藏中间件,例如使用Apache Camel的Spring Remoting实现JMS



1> Robin..:

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

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

然后

receiver.receive()

要么

receiver.setListener(myListener);



2> James Strach..:

BTW虽然它不是您提出的实际问题 - 如果您正在尝试通过JMS实现请求响应,我建议您阅读本文,因为JMS API比您想象的要复杂得多,并且有效地执行此操作要比它更难看起来.

特别是为了有效地使用JMS,您应该尽量避免为单个消息等创建消费者.

另外,因为JMS API非常复杂,无法正确有效地使用 - 特别是对于池,事务和并发处理 - 我建议人们从应用程序代码中隐藏中间件,例如使用Apache Camel的Spring Remoting实现JMS

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有