简体   繁体   English

消息监听器和 jmslistener 注解有什么区别

[英]Whats the difference between message listeners and jmslistener annotation

So I'm diving deeper into the world of JMS.因此,我正在深入研究 JMS 的世界。 I am writing some dummy projects right now and understanding how to consume messages.我现在正在编写一些虚拟项目并了解如何使用消息。 I am using Active MQ artemis as the message broker.我使用 Active MQ artemis 作为消息代理。

Whilst following a tutorial, I stumbled upon something in terms on consuming messages.在学习教程时,我偶然发现了一些关于消费消息的内容。 What exactly is the difference between a message listener to listen for messages and using the @JmsListener annotion?消息侦听器侦听消息和使用@JmsListener注释之间到底有什么区别?

This is what I have so far:这是我到目前为止所拥有的:

public class Receiver {

  @JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "helloworld .q")
  public void receive(String message) {
      System.out.println("received message='" + message + "'.");
  }
}

@Configuration
@EnableJms
public class ReceiverConfig {

    @Value("${artemis.broker-url}")
    private String brokerUrl;

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory(){
        return new ActiveMQConnectionFactory(brokerUrl);
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(activeMQConnectionFactory());
        factory.setConcurrency("3-10");

        return factory;
    }

    @Bean
    public DefaultMessageListenerContainer orderMessageListenerContainer() {
        SimpleJmsListenerEndpoint endpoint =
                new SimpleJmsListenerEndpoint();
        endpoint.setMessageListener(new StatusMessageListener("DMLC"));
        endpoint.setDestination("helloworld.q"); //Try renaming this and see what happens.

        return jmsListenerContainerFactory()
                .createListenerContainer(endpoint);
    }

    @Bean
    public Receiver receiver() {
        return new Receiver();
    }
}

public class StatusMessageListener implements MessageListener {

    public StatusMessageListener(String dmlc) {
    }

    @Override
    public void onMessage(Message message) {
        System.out.println("In the onMessage().");
        System.out.println(message);
    }
}

From what I've read is that we register a message listener to the container listener which in turn is created by the listener factory.根据我的阅读,我们向容器侦听器注册了一个消息侦听器,而容器侦听器又由侦听器工厂创建。 So essentially the flow is this: DefaultJmsListenerContainerFactory -> creates -> DefaultMessageListenerContainer -> registers a message listener which is used to listen to messages from the endpoint configured.所以基本上流程是这样的: DefaultJmsListenerContainerFactory -> 创建 -> DefaultMessageListenerContainer -> 注册一个消息侦听器,用于侦听来自配置的端点的消息。 From my research, i've gathered that messageListeners are used to asynchornously consume messages from the queues/topic whilst using the @JmsListener annotation is used to synchronously listen to messages?根据我的研究,我收集到 messageListener 用于异步使用来自队列/主题的消息,而使用@JmsListener注释用于同步收听消息?

Furthermore, there's a few other ListenerContainerFactory out there such as DefaultJmsListenerContainerFactory and SimpleJmsListenerContainerFactory but not sure I get the difference.此外,还有一些其他的ListenerContainerFactory ,例如DefaultJmsListenerContainerFactorySimpleJmsListenerContainerFactory但不确定我是否明白其中的区别。 I was reading https://codenotfound.com/spring-jms-listener-example.html and from what I've gathered from that is Default uses a pull model so that suggests it's async so why would it matter if we consume the message via a messageListener or the annotation?我正在阅读https://codenotfound.com/spring-jms-listener-example.html并且从我收集的内容来看,默认使用拉 Z20F35E630DAF44DBFA4C3F68F5399D8 那么我们建议使用消息时为什么会这么说?通过 messageListener 或注释? I'm a bit confused and muddled up so would like my doubts to be cleared up.我有点困惑和困惑,所以希望我的疑虑能够被清除。 Thanks!谢谢!

This is the snippet of the program when sending 100 dummy messages (just noticed it's not outputting the even numbered messages..):这是发送 100 条虚拟消息时的程序片段(只是注意到它没有输出偶数消息..):

received message='This the 95 message.'.
In the onMessage().
ActiveMQMessage[ID:006623ca-d42a-11ea-a68e-648099ad9459]:PERSISTENT/ClientMessageImpl[messageID=24068, durable=true, address=helloworld.q,userID=006623ca-d42a-11ea-a68e-648099ad9459,properties=TypedProperties[__AMQ_CID=00651257-d42a-11ea-a68e-648099ad9459,_AMQ_ROUTING_TYPE=1]]
received message='This the 97 message.'.
In the onMessage().
ActiveMQMessage[ID:006ba214-d42a-11ea-a68e-648099ad9459]:PERSISTENT/ClientMessageImpl[messageID=24088, durable=true, address=helloworld.q,userID=006ba214-d42a-11ea-a68e-648099ad9459,properties=TypedProperties[__AMQ_CID=0069cd51-d42a-11ea-a68e-648099ad9459,_AMQ_ROUTING_TYPE=1]]
received message='This the 99 message.'.

The following configuration以下配置

@Configuration
@EnableJms
public class ReceiverConfig {

//your config code here..

}

would ensure that every time a Message is received on the Destination named "helloworld.q" , Receiver.receive() is called with the content of the message.将确保每次在名为"helloworld.q"的目标上收到消息时,都会使用消息的内容调用Receiver.receive()

You can read more here: https://docs.spring.io/spring/docs您可以在此处阅读更多信息: https://docs.spring.io/spring/docs

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

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