Search code examples
spring-bootactivemq-artemis

Spring Boot embedded ActiveMQ Artemis JMS broker behaviour


I've been struggling lately with the spring-boot-artemis-starter. My understanding of its Spring Boot support was the following:

  • set spring.artemis.mode=embedded and, like Tomcat, Spring Boot will instantiate a broker reachable through TCP (server mode). The following command should be successful: nc -zv localhost 61616
  • set spring.artmis.mode=native and Spring Boot will only configure the jms template according to the spring.artemis.* properties (client mode).

The client mode works just fine with a standalone ActiveMQ Artemis server on my machine. Unfortunately, I could never manage to reach the TCP port in server mode.

I would be grateful if somebody confirms my understanding of the embedded mode.

After some digging I noted that the implementation provided out of the box by the spring-boot-starter-artemis uses org.apache.activemq.artemis.core.remoting.impl.invm.InVMAcceptorFactory acceptor. I'm wondering if that's not the root cause (again I'm by no means an expert). But it appears that there is a way to customize artemis configuration. Therefore I tried the following configuration without any luck:

@SpringBootApplication
public class MyBroker {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyBroker.class, args);
    }

    @Autowired
    private ArtemisProperties artemisProperties;

    @Bean
    public ArtemisConfigurationCustomizer artemisConfigurationCustomizer() {
        return configuration -> {
            try {
               configuration.addAcceptorConfiguration("netty", "tcp://localhost:" + artemisProperties.getPort());
            } catch (Exception e) {
                throw new RuntimeException("Failed to add netty transport acceptor to artemis instance");
            }
        };
    }
}

Solution

  • You just have to add a Connector and an Acceptor to your Artemis Configuration. With spring-boot-artemis-starter Spring creates a configuration bean which will be used for EmbeddedJMS configuration. You can see this in ArtemisEmbeddedConfigurationFactory where an InVMAcceptorFactory will be set for the configuration. You can edit this bean and change ActiveMQ Artemis' behaviour through custom ArtemisConfigurationCustomizer bean which will be sucked up by Spring autoconfig and be applied to the configuration.

    An example config class for your Spring Boot application:

    import org.apache.activemq.artemis.api.core.TransportConfiguration;
    import org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptorFactory;
    import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory;
    import org.springframework.boot.autoconfigure.jms.artemis.ArtemisConfigurationCustomizer;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class ArtemisConfig implements ArtemisConfigurationCustomizer {
        @Override
        public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) {
            configuration.addConnectorConfiguration("nettyConnector", new TransportConfiguration(NettyConnectorFactory.class.getName()));
            configuration.addAcceptorConfiguration(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
        }
    }