Spring JMS 生产者和消费者交互 [英] Spring JMS Producer and Consumer interaction

查看:48
本文介绍了Spring JMS 生产者和消费者交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定消费者是否已经收到了来自生产者的消息,或者如何通知生产者该消息已经在 Spring JMS 中发送/消费了?

解决方案

you need to use request/reply by using org.springframework.jms.core.JmsTemplate.sendAndReceive(...) on生产者端和消费者端在容器中设置的 MessageListener 方法必须返回一个值.

或者让订阅 ActiveMQ.Advisory.MessageConsumed.Queue 的生产者通过访问一些属性(如 orignalMessageId)在消息被消费时得到通知,看看这里http://activemq.apache.org/advisory-message.html

你可以这样使用它:

 目标advisoryDe​​stination = org.apache.activemq.advisory.AdvisorySupport.getMessageConsumedAdvisoryTopic(session.createQueue("yourQueue"));MessageConsumer 消费者 = session.createConsumer(advisoryDe​​stination);消费者.setMessageListener(新消息监听器(){@覆盖公共无效 onMessage(消息 msg){System.out.println(msg);System.out.println(((ActiveMQMessage) msg​​).getMessageId());ActiveMQMessage aMsg = (ActiveMQMessage) ((ActiveMQMessage) msg​​).getDataStructure();System.out.println(aMsg.getMessageId());}});

How can i determine if the consumer already received the message from the producer or how can i notify the producer that the message have already been sent/consumed in Spring JMS?

解决方案

you need to use request/reply by using org.springframework.jms.core.JmsTemplate.sendAndReceive(...) on the producer side and on the consumer side the MessageListener method set in the container must return a value.

or make your producer subscribing to ActiveMQ.Advisory.MessageConsumed.Queue to be notified when the message is consumed by having access to some properties like orignalMessageId, take a look here http://activemq.apache.org/advisory-message.html

you can use it like this :

            Destination advisoryDestination = org.apache.activemq.advisory.AdvisorySupport
                    .getMessageConsumedAdvisoryTopic(session.createQueue("yourQueue"));
            MessageConsumer consumer = session.createConsumer(advisoryDestination);
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message msg) {
                    System.out.println(msg);
                    System.out.println(((ActiveMQMessage) msg).getMessageId());
                    ActiveMQMessage aMsg = (ActiveMQMessage) ((ActiveMQMessage) msg).getDataStructure();
                    System.out.println(aMsg.getMessageId());
                }
            });

这篇关于Spring JMS 生产者和消费者交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆