I need to connect my Spring Boot 3.1.2 application to multiple different ActiveMQ Artemis brokers.
If I try to connect only one in application.yaml it works well. But when I want to create two different JmsTemplates I cannot send message to broker with "Channel disconnected" error.
Failure captured on connectionID=076130d3, performing failover or reconnection now
org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException: AMQ219006: Channel disconnected
It can connect to browser because I see it in the ActiveMQ Artemis console as a session. My configuration is like this (first I try to connect to the first broker):
@Bean(name = "firstConnectionFactory")
public ConnectionFactory firstConnectionFactory() {
try {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
cf.setBrokerURL("tcp://localhost:61616");
cf.setUser("user");
cf.setPassword("password");
return cf;
}
catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
@Bean(name = "firstTemplate")
public JmsTemplate firstTemplate() {
JmsTemplate template=new JmsTemplate();
template.setConnectionFactory(firstConnectionFactory());
return template;
}
and I try to use it in a service like this:
private final JmsTemplate firstTemplate;
@Autowired
public TestSender(@Qualifier("firstTemplate") JmsTemplate firstTemplate) {
this.firstTemplate=firstTemplate;
}
...
private void createTestRecord() throws Exception {
JmsEntity e = JmsEntity.builder()
...
.build();
String json = this.objectMapper.writeValueAsString(e);
LOGGER.debug(String.format("send message: %s", json));
this.firstTemplate.convertAndSend("topic", json);
}
But if I try to configure it by application.yaml like this:
spring:
artemis:
mode: native
user: user
password: password
broker-url: tcp://localhost:61616
I can inject a JmsTemplate and it works well.
What is the difference between yaml configuration and programmatically configuration?
Comment From: wilkinsona
What is the difference between yaml configuration and programmatically configuration?
You haven't provided enough information about your configuration to answer this definitely. One possibility is that Boot may be wrapping the ActiveMQConnectionFactory in a CachingConnectionFactory (this is the default behavior unless you have set spring.jms.cache.enabled to false). As described in its javadoc, CachingConnectionFactory "switches the reconnectOnException property to true by default, allowing for automatic recovery of the underlying Connection".
If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.