在 Spring Boot 中通过 ActiveMQ Artemis 发送异步消息时如何创建会话工厂?
How to create Session Factory when sending async messages via ActiveMQ Artemis in Spring Boot?
我正在尝试从 sender Spring Boot app to a receiver Spring 启动应用向主题发送简单消息。但我不断收到两个例外。
接收器中出现第一个异常Spring启动应用程序:
javax.jms.JMSException: Failed to create session factory
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:882) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.springframework.jms.connection.SingleConnectionFactory.doCreateConnection(SingleConnectionFactory.java:410) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.initConnection(SingleConnectionFactory.java:350) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.getConnection(SingleConnectionFactory.java:328) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.createConnection(SingleConnectionFactory.java:243) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:196) ~[spring-jms-5.3.18.jar:5.3.18]
几秒钟后,发件人应用中出现以下异常:
org.apache.activemq.artemis.api.core.ActiveMQConnectionTimedOutException: AMQ219013: Timed out waiting to receive cluster topology. Group:null
at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:748) ~[artemis-core-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:880) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.springframework.jms.connection.SingleConnectionFactory.doCreateConnection(SingleConnectionFactory.java:410) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.initConnection(SingleConnectionFactory.java:350) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.getConnection(SingleConnectionFactory.java:328) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.createConnection(SingleConnectionFactory.java:243) ~[spring-jms-5.3.18.jar:5.3.18]
我正在关注 Spring 引导微服务上的 Udemy course,其中第 13 章和第 14 章专门介绍 JMS 消息传递,因此,除了 [=67 的版本之外,我的配置几乎相同=] Boot,我的是 2.6.6,教练的是 2.1.x 或类似的东西。我必须保留 2.6.6,因为我的项目中还有其他一些东西。
我确实设法通过 Docker 启动了 Active MQ 服务器,就像这样:
C:\Users\Miljan>docker run -it --rm -p 8161:8161 -p 61616:61616 vromero/activemq-artemis
在运行一张图片之前,我必须下载image,我可以看到服务器是运行:
此外,我可以登录控制台(用户名:artemis
;密码:simetraehcapa
;):
发件人应用中的其他配置:
@EnableAsync
@EnableScheduling
@Configuration
public class AsyncConfig {
public static final String EXECUTOR = "SchedulerExecutor";
@Bean(name = EXECUTOR)
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix(EXECUTOR + "-");
executor.initialize();
return executor;
}
}
和
@Configuration
public class JmsConfig {
public static final String QUEUE = "queue";
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter(ObjectMapper objectMapper) {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
converter.setObjectMapper(objectMapper);
return converter;
}
}
和
spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=8161
spring.artemis.user=artemis
spring.artemis.password=simetraehcapa
最后,我实际发送消息的方式是通过简单的 GET
请求 (http://localhost:8080/send
):
@GetMapping("/send")
public void sendMessage(){
Student s = new Student("MIKE");
this.jmsTemplate.convertAndSend(JmsConfig.QUEUE, s);
}
和听众class:
@Component
@EnableJms
public class Listener {
@Autowired
private JmsTemplate jmsTemplate;
@JmsListener(destination = "queue")
public void listen(Student student){
System.out.println(student);
}
}
所以我的问题是:
- 如何配置会话工厂?
- 我是否遗漏了一些我不知道的其他配置?
我认为问题出在您配置中的这一行:
spring.artemis.port=8161
这指向 ActiveMQ Artemis 中的嵌入式 Web 服务器使用的 HTTP 端口。这不适用于 JMS 客户端。你应该使用:
spring.artemis.port=61616
我正在尝试从 sender Spring Boot app to a receiver Spring 启动应用向主题发送简单消息。但我不断收到两个例外。
接收器中出现第一个异常Spring启动应用程序:
javax.jms.JMSException: Failed to create session factory
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:882) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.springframework.jms.connection.SingleConnectionFactory.doCreateConnection(SingleConnectionFactory.java:410) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.initConnection(SingleConnectionFactory.java:350) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.getConnection(SingleConnectionFactory.java:328) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.createConnection(SingleConnectionFactory.java:243) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:196) ~[spring-jms-5.3.18.jar:5.3.18]
几秒钟后,发件人应用中出现以下异常:
org.apache.activemq.artemis.api.core.ActiveMQConnectionTimedOutException: AMQ219013: Timed out waiting to receive cluster topology. Group:null
at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:748) ~[artemis-core-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:880) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294) ~[artemis-jms-client-2.19.1.jar:2.19.1]
at org.springframework.jms.connection.SingleConnectionFactory.doCreateConnection(SingleConnectionFactory.java:410) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.initConnection(SingleConnectionFactory.java:350) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.getConnection(SingleConnectionFactory.java:328) ~[spring-jms-5.3.18.jar:5.3.18]
at org.springframework.jms.connection.SingleConnectionFactory.createConnection(SingleConnectionFactory.java:243) ~[spring-jms-5.3.18.jar:5.3.18]
我正在关注 Spring 引导微服务上的 Udemy course,其中第 13 章和第 14 章专门介绍 JMS 消息传递,因此,除了 [=67 的版本之外,我的配置几乎相同=] Boot,我的是 2.6.6,教练的是 2.1.x 或类似的东西。我必须保留 2.6.6,因为我的项目中还有其他一些东西。
我确实设法通过 Docker 启动了 Active MQ 服务器,就像这样:
C:\Users\Miljan>docker run -it --rm -p 8161:8161 -p 61616:61616 vromero/activemq-artemis
在运行一张图片之前,我必须下载image,我可以看到服务器是运行:
此外,我可以登录控制台(用户名:artemis
;密码:simetraehcapa
;):
发件人应用中的其他配置:
@EnableAsync
@EnableScheduling
@Configuration
public class AsyncConfig {
public static final String EXECUTOR = "SchedulerExecutor";
@Bean(name = EXECUTOR)
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix(EXECUTOR + "-");
executor.initialize();
return executor;
}
}
和
@Configuration
public class JmsConfig {
public static final String QUEUE = "queue";
@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter(ObjectMapper objectMapper) {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
converter.setObjectMapper(objectMapper);
return converter;
}
}
和
spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=8161
spring.artemis.user=artemis
spring.artemis.password=simetraehcapa
最后,我实际发送消息的方式是通过简单的 GET
请求 (http://localhost:8080/send
):
@GetMapping("/send")
public void sendMessage(){
Student s = new Student("MIKE");
this.jmsTemplate.convertAndSend(JmsConfig.QUEUE, s);
}
和听众class:
@Component
@EnableJms
public class Listener {
@Autowired
private JmsTemplate jmsTemplate;
@JmsListener(destination = "queue")
public void listen(Student student){
System.out.println(student);
}
}
所以我的问题是:
- 如何配置会话工厂?
- 我是否遗漏了一些我不知道的其他配置?
我认为问题出在您配置中的这一行:
spring.artemis.port=8161
这指向 ActiveMQ Artemis 中的嵌入式 Web 服务器使用的 HTTP 端口。这不适用于 JMS 客户端。你应该使用:
spring.artemis.port=61616