我正在尝试引导一个小型用户应用程序,使用gradle和springboot来收听发布到ActiveMQ主题的消息。我注意到,每当我尝试运行该应用程序时,它都无法注册为ActiveMQ上的订阅者,也无法收听发布到主题上的消息。进程在没有堆栈跟踪的情况下终止。但是,在github (Reference-2)上有一个类似的代码库,但使用的是maven构建结构,它似乎连接并接收了ActiveMQ (localhost)上发布的主题消息。我已经调试这个问题很长时间了--我还没能找出根本原因。以下是供参考的代码片段:
如有任何意见,将不胜感激。谢谢!
注意:在Java1.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) + ")";
}
}参考资料
发布于 2021-01-09 15:31:53
我通过删除显式组件扫描注释标签解决了这个问题。现在,订阅者应用程序在activemq上收听主题。
https://stackoverflow.com/questions/65639490
复制相似问题