Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 消息侦听器和jmslistener注释之间有什么区别_Java_Spring Jms - Fatal编程技术网

Java 消息侦听器和jmslistener注释之间有什么区别

Java 消息侦听器和jmslistener注释之间有什么区别,java,spring-jms,Java,Spring Jms,所以我正在深入JMS的世界。 我现在正在写一些虚拟项目,了解如何使用消息。我使用activemqartemis作为消息代理 在学习教程的过程中,我偶然发现了一些关于消费信息的东西。消息侦听器侦听消息与使用@JmsListener注释之间的区别到底是什么 这就是我到目前为止所做的: public class Receiver { @JmsListener(containerFactory = "jmsListenerContainerFactory", destinati

所以我正在深入JMS的世界。 我现在正在写一些虚拟项目,了解如何使用消息。我使用activemqartemis作为消息代理

在学习教程的过程中,我偶然发现了一些关于消费信息的东西。消息侦听器侦听消息与使用
@JmsListener
注释之间的区别到底是什么

这就是我到目前为止所做的:

public class Receiver {

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


从我读到的内容来看,我们将消息侦听器注册到容器侦听器,容器侦听器又由侦听器工厂创建。基本上,流程是这样的: DefaultJmsListenerContainerFactory->creates->DefaultMessageListenerContainerContainer->注册一个消息侦听器,用于侦听来自所配置端点的消息。 根据我的研究,我发现MessageListener用于异步使用队列/主题中的消息,而使用
@JmsListener
注释用于同步侦听消息

此外,还有一些其他的
ListenerContainerFactory
,比如
DefaultJmsListenerContainerFactory
SimpleJMListenerContainerFactory
,但我不确定我是否能得到区别。我正在阅读,从我收集的信息来看,默认情况下使用了一个拉模型,这表明它是异步的,那么,如果我们通过messageListener或注释使用消息,又有什么关系呢?我有点困惑和糊涂,所以我想澄清我的疑虑。谢谢

这是发送100条伪消息时的程序片段(刚刚注意到它没有输出偶数消息..):


下面的配置

@Configuration
@EnableJms
public class ReceiverConfig {

//your config code here..

}
将确保每次在名为
“helloworld.q”
的目标上接收到消息时,都会使用消息内容调用
Receiver.receive()


您可以在此处阅读更多内容:

此处对其进行了解释,因此本质上是相同的?您应该修改您的问题,以便清楚地知道您是专门询问Spring JMS资源,而不是普通JMS资源。
javax.jms.MessageListener
(其行为在jms规范中有概述)不同于Spring的各种包装。
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);
    }
}
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.'.
@Configuration
@EnableJms
public class ReceiverConfig {

//your config code here..

}