简体   繁体   English

如何知道侦听器是否在JMS中获取消息?

[英]How to know if a listener gets the message in JMS?

I'm using Spring JMS and ActiveMQ to send message from one sender to multiple listeners asynchronously. 我正在使用Spring JMS和ActiveMQ异步地将消息从一个发送者发送到多个侦听器。 All listeners subscribe to an ActiveMQ Topic so that the message can be delivered to them. 所有侦听器都订阅ActiveMQ主题,以便可以将消息传递给它们。 I want to know if a particular listener gets the message from the sender. 我想知道特定的侦听器是否从发件人那里获取了消息。 Is there a way to achieve this? 有没有办法实现这个目标?

Edit: Added the JMS sender and listener classes 编辑:添加了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;
            }
        });
    }
}

And this is one of the message listeners: 这是消息监听器之一:

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);
            }
        }
    }
}

Upon calling simpleSend() method in the sender class, all listeners subscribed to this topic will get this message asynchronously. 在sender类中调用simpleSend()方法后,订阅此主题的所有侦听器都将异步获取此消息。 But I want to know if this particular CustomerStatusListener receives the message from the message sender. 但我想知道这个特定的CustomerStatusListener收到来自邮件发件人的邮件。 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. 我假设我不能在发送者和监听器中使用ReplyTo ,如果我想异步地执行其中一个答案。 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. Sender posts a 'Request' message to 1st topic. 发件人向第一个主题发布“请求”消息。
  2. Listener receives the message as it listens to Message<Request> object. 侦听器在侦听Message <Request>对象时收到消息。
  3. After processing the message Listener sends Message<Ack> object to 2nd Topic. 处理完消息后,Listener将Message <Ack>对象发送到2nd Topic。
  4. Sender in turn listens to Message<Ack> object recieves it. 发件人依次侦听Message <Ack>对象收到它。 I've added identifiable information about the listener to find out which listeners ultimately got my request Message<Request>. 我添加了有关侦听器的可识别信息,以找出哪些侦听器最终得到了我的请求消息<Request>。

I hope it solves your problem.. 我希望它能解决你的问题..

You would have to set a replyTo and have the listener reply when it gets a message. 您必须设置replyTo并在收到消息时让侦听器回复。 You can create a TemporaryQueue for the reply. 您可以为回复创建TemporaryQueue。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM