订阅者应用程序不使用主题侦听 ActiveMQ 上的消息

如何解决订阅者应用程序不使用主题侦听 ActiveMQ 上的消息

我正在尝试使用 gradle 和 springboot 引导一个小型订阅者应用程序来收听发布到 ActiveMQ 上的主题的消息。我观察到,每当我尝试运行该应用程序时,它都无法将自己注册为 ActiveMQ 上的订阅者并收听发布到该主题的消息。该进程在没有堆栈跟踪的情况下终止。但是,github (Reference-2) 上的类似代码库但具有 maven 构建结构似乎可以连接并接收 ActiveMQ (localhost) 上发布的主题消息。对问题进行了长时间的调试 - 我无法找到根本原因。以下是供参考的代码片段:-

任何输入将不胜感激。谢谢!

注意:所有运行都是在 Java 1.8 环境下的 eclipse 中进行的。

JMSTopicSubscriber.java

@Component
public class JMSTopicSubscriber
{
   @JmsListener(destination = "${jms.enrol.topic.name}")
   public void receiveMessage(Exam exam)
   {
      System.out.println("Received message: " + exam.toString());
   }
}

JMSConfig.java

@EnableJms
@Configuration
@ComponentScan(basePackages = { "experiment.jms.subscriber" })
@PropertySource("classpath:application.properties")
public class JMSConfig
{
   @Value("${spring.activemq.broker-url}")
   String m_activeMQbrokerUrl;

   @Bean
   public ConnectionFactory connectionFactory()
   {
      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
      cf.setbrokerURL(m_activeMQbrokerUrl);
      return cf;
   }

   @Bean
   public JmsListenerContainerFactory<?> jmsListenerContainerFactory()
   {
      DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); 
      factory.setPubSubDomain(true);
      factory.setMessageConverter(messageConverter());
      factory.setConnectionFactory(connectionFactory()); 
      return factory; 
   }
   
   @Bean
   public MessageConverter messageConverter()
   {
       MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
       converter.settargettype(MessageType.TEXT);
       converter.setTypeIdPropertyName("_type");
       return converter;
   }
}

ExperimentTopicJmsSubscriberApplication.java

@SpringBootApplication
@ComponentScan(basePackages = { "experiment.jmsSubscriber" })
public class ExperimentTopicJmsSubscriberApplication
{
   public static void main(String[] args)
   {
      SpringApplication.run(ExperimentTopicJmsSubscriberApplication.class,args);
   }
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'experiment.jms.topic'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
    // ActiveMQ Dependencies. 
    implementation 'org.springframework.boot:spring-boot-starter-activemq'
}

test {
    useJUnitPlatform()
}

application.properties

spring.activemq.broker-url = tcp://localhost:61616

#topic name
jms.enrol.topic.name = allexams

spring.jms.pub-sub-domain=true

##Configure concurrent listeners using the spring.jms.concurrency and spring.jms.max-concurrency properties.
#spring.jms.concurrency property configures the minimum number of concurrent consumers.
spring.jms.listener.concurrency=2

#spring.jms.max-concurrency configures the maximum number of concurrent consumers.
spring.jms.listener.max-concurrency=2

包结构

enter image description here

数据模型

    public class Exam implements Serializable
    {
       private String m_examName; 
       private int m_examYear; 
       
       public Exam()
       {
          
       }
       
       public Exam( String examName,int examYear )
       {
          this.m_examName = examName; 
          this.m_examYear = examYear;  
       }
       
       public String getExamName()
       {
          return this.m_examName; 
       }
       
       public int getExamYear()
       {
          return this.m_examYear;
       }
       
       public String toString()
       {
          return "(Exam Name: " + this.m_examName + ","
                + "Exam Year: " + Integer.toString(this.m_examYear) + ")"; 
       }
     }

参考资料

  1. https://github.com/smitha-madhavamurthy/springboot-activemq-subscriber
  2. https://grokonez.com/java-integration/activemq-work-spring-jms-activemq-topic-publisher-subcribers-pattern-using-springboot

解决方法

我通过删除显式组件扫描注释标签解决了这个问题。现在,订阅者应用在 activemq 上监听主题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?