首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >订阅者-应用程序不使用主题收听ActiveMQ上的消息

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

Stack Overflow用户
提问于 2021-01-09 04:56:57
回答 1查看 351关注 0票数 0

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

如有任何意见,将不胜感激。谢谢!

注意:在Java1.8环境下,所有的运行都是在eclipse内部完成的。

JMSTopicSubscriber.java

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

JMSConfig.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
@SpringBootApplication
@ComponentScan(basePackages = { "experiment.jmsSubscriber" })
public class ExperimentTopicJmsSubscriberApplication
{
   public static void main(String[] args)
   {
      SpringApplication.run(ExperimentTopicJmsSubscriberApplication.class, args);
   }
}

build.gradle

代码语言:javascript
复制
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

代码语言:javascript
复制
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

封装结构

数据模型

代码语言:javascript
复制
    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
EN

回答 1

Stack Overflow用户

发布于 2021-01-09 15:31:53

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65639490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档