问题描述
我正在尝试使用 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
包结构
数据模型
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) + ")";
}
}
参考资料
- https://github.com/smitha-madhavamurthy/springboot-activemq-subscriber
- https://grokonez.com/java-integration/activemq-work-spring-jms-activemq-topic-publisher-subcribers-pattern-using-springboot
解决方法
我通过删除显式组件扫描注释标签解决了这个问题。现在,订阅者应用在 activemq 上监听主题。