如何知道侦听器是否在JMS中收到消息? [英] How to know if a listener gets the message in JMS?

查看:89
本文介绍了如何知道侦听器是否在JMS中收到消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring JMS和ActiveMQ将消息从一个发送者异步发送到多个侦听器.所有侦听器都订阅ActiveMQ主题,以便可以将消息传递给他们.我想知道特定的侦听器是否从发件人那里获取消息.有没有办法做到这一点?

I'm using Spring JMS and ActiveMQ to send message from one sender to multiple listeners asynchronously. All listeners subscribe to an ActiveMQ Topic so that the message can be delivered to them. I want to know if a particular listener gets the message from the sender. Is there a way to achieve this?

添加了JMS发送者和监听者类

这是我的消息发送者类:

This is my message sender class:

public class CustomerStatusSender {
    private JmsTemplate jmsTemplate;
    private Topic topic;

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    public void simpleSend(final String customerStatusMessage) {
        jmsTemplate.send(topic, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage("hello from sender.");
                message.setStringProperty("content", customerStatusMessage);
                return message;
            }
        });
    }
}

这是消息侦听器之一:

public class CustomerStatusListener implements MessageListener {
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                System.out.println("Subscriber 1 got you! The message is: "
                        + message.getStringProperty("content"));
            } catch (JMSException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}

在sender类中调用simpleSend()方法后,所有订阅此主题的侦听器都将异步获取此消息.但是我想知道这个特定的CustomerStatusListener是否从消息发件人那里收到消息.如何异步进行?我假设如果要异步执行,则无法按照答案之一的建议在发送方和监听方中使用ReplyTo.我需要在发件人和侦听器类中添加什么才能从侦听器获取消息接收确认?

Upon calling simpleSend() method in the sender class, all listeners subscribed to this topic will get this message asynchronously. But I want to know if this particular CustomerStatusListener receives the message from the message sender. How to do it asynchronously? I assume that I can't use ReplyTo in the sender and listener as suggested in one of the answers if I want to do it asynchronously. What do I need to add in the sender and listener classes to get message receipt confirmation from the listener?

推荐答案

我以前遇到过这种情况.我使用了两个主题来处理场景.第一个主题用于发布,第二个主题用于收据.这是我在应用程序中遇到的流程.

I ran into a situation like this before. I've used two topics to handle the scenario. 1st topic is used for publish and the second topic is used for receipts. Here's the flow I had in my application.

  1. 发件人向第一个主题发布请求"消息.
  2. 侦听器在侦听Message< Request>时接收到该消息.目的.
  3. 处理完消息后,侦听器发送Message< Ack>.反对第二主题.
  4. 发件人依次收听Message< Ack>.对象接收它.我添加了有关侦听器的可识别信息,以找出哪些侦听器最终收到了我的请求Message< Request>.

我希望它能解决您的问题.

I hope it solves your problem..

这篇关于如何知道侦听器是否在JMS中收到消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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