Failed to create session factory with embedded ActiveMQ Artemis server in Spring Boot test

56 Views Asked by At

I'm encountering a jakarta.jms.JMSException: Failed to create session factory error when attempting to access a test embedded ActiveMQ Artemis server in Spring Boot. Here's my setup:

jakarta.jms.JMSException: Failed to create session factory

ConnectionFactory Bean:

    @Bean
    public ConnectionFactory connectionFactory() throws JMSException {
        return new ActiveMQConnectionFactory("vm://localhost");
    }

Test code:

@SpringBootTest
@ContextConfiguration(classes = {JmsConfig.class, JmsListenerTest.TestConfig.class})
@EnableAutoConfiguration
public class JmsListenerTest {

    @Configuration
    static class TestConfig {
        @Bean
        public ConnectionFactory connectionFactory() throws JMSException {
            return new ActiveMQConnectionFactory("vm://localhost");
        }
    }

    @Autowired
    private JmsTemplate jmsTemplate;

    @SpyBean
    private NotifierMessageListener listener;

    @Test
    public void receive()  {

        final var message = new NacqMessage("s2", "receive test");
        jmsTemplate.send(NotifierMessageListener.MAILBOX_JSON, s -> s.createObjectMessage(message)); //throws Failed to create session factory

      ...
    }
}

Relevant gradle dependencies:

implementation 'org.springframework.boot:spring-boot-starter-artemis'
implementation 'org.springframework:spring-jms'
testImplementation 'org.apache.activemq:artemis-jakarta-server:2.32.0' //not sure if needed.

The issue arises only with the embedded server, whereas using a non-embedded server works fine. I've referred to the official documentation but haven't found sufficient guidance on configuring the embedded server correctly. What additional configuration might be needed in this scenario?

Official Documentation Reference: Using ActiveMQConnectionFactory on Artemis Page

1

There are 1 best solutions below

4
Justin Bertram On

The URL vm://localhost is not valid. The in-vm connector must reference the ID where the server is listening. You should almost certainly using vm://0 instead.

Of course, this assumes that an embedded instance of ActiveMQ Artemis is actually being started for your test and said instance has an in-vm acceptor listening on vm://0.