简体   繁体   中英

Whats the difference between message listeners and jmslistener annotation

So I'm diving deeper into the world of 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.

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?

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. 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?

Furthermore, there's a few other ListenerContainerFactory out there such as DefaultJmsListenerContainerFactory and SimpleJmsListenerContainerFactory but not sure I get the difference. 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? 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..):

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.

You can read more here: https://docs.spring.io/spring/docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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