【发布时间】:2021-04-27 17:27:19
【问题描述】:
我无法从本地运行的 ActiveMQ 获取消息。我可以将它们生产到队列中,并且我的 PC 也注册为生产者。但是,应该将机器上的另一个 Spring App 配置为侦听器。到目前为止,它不起作用。 ActiveMQ 正在侦听默认端口。
我的发件人 JMS 配置:
package at.dkepr.queueservice;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
@Configuration
public class JmsConfig {
@Bean
public Queue queue(){
return new ActiveMQQueue("indexing-queue");
}
}
这是消费者:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import at.dkepr.entity.UserSearchEntity;
@Component
@EnableJms
public class JmsConsumer {
private final Logger logger = LoggerFactory.getLogger(JmsConsumer.class);
@JmsListener(destination = "indexing-queue", containerFactory = "jmsListenerContainerFactory")
public void receive(UserSearchEntity user){
logger.info(user.getEmail());
}
}
在application.propertiers我已经添加了必要的属性:
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
UserSearchEntity 也实现了Serializable。
据我所知,对于此设置,我什至不需要为消费者进行配置。无论如何,我添加了一个。
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
@Configuration
@EnableJms
public class ConsumerConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerUrl);
return activeMQConnectionFactory;
}
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory());
factory.setConcurrency("1-3");
return factory;
}
}
我没有收到任何错误日志。不幸的是,它根本没有做任何事情。
这是来自 ActiveMQ Web 控制台的屏幕截图,其中包含已排队的消息:
当我截取此屏幕截图时,我的消费应用程序正在运行,但代理显然无法识别它,因为“消费者数量”是 0。
编辑:
我刚刚尝试将侦听器添加到生产者所在的同一个 Spring 应用程序中。令人惊讶的是,Listener 连接良好。似乎问题在于不同的 Spring 应用程序。但是,我对两个 Spring 应用程序都使用了相同的 application.properties。配置文件也一样。
【问题讨论】:
-
是的,我确实做到了。但它不被识别。
-
添加了属性。我将 ActiveMQ 配置保留为默认设置。所以它正在监听默认端口。