Search code examples
javaspringspring-bootjmsactivemq-artemis

ActiveMQ Artemis connection timeout in Spring Boot


I have some troubles with sending message to ActiveMQ Artemis in Spring Boot:

Uncategorized exception occurred during JMS processing; nested exception is ActiveMQConnectionTimedOutException[errorType=CONNECTION_TIMEDOUT message=AMQ219013: Timed out waiting to receive cluster topology. Group:null]

I use the apache/activemq-artemis:latest-alpine Docker container. I wasn't configure it and open 5672 port. If I try to send message by JMS template I got this error. My beans (on Kotlin):

@EnableJms
@Configuration
class JmsConfig {
    @Bean
    fun jmsTemplate(connectionFactory: ConnectionFactory): JmsTemplate {
        val jmsTemplate = JmsTemplate(connectionFactory)
        jmsTemplate.messageConverter = messageConverter()
        return jmsTemplate
    }

    @Bean
    fun messageConverter(): MessageConverter {
        val converter = MappingJackson2MessageConverter()

        converter.setTargetType(MessageType.TEXT)
        converter.setTypeIdPropertyName("_type")

        return converter
    }
}
@Configuration
class ActiveMQConfig {

    @Bean
    fun connectionFactory(): ConnectionFactory {
        return ActiveMQConnectionFactory("tcp://localhost:5672?protocols=STOMP,AMQP,MQTT", "artemis", "artemis")
    }
}

The place where I send message

@Service
class EmailGateway (
    private val template: JmsTemplate,
) {
    fun sendEmail(to: String, letter: Letter) {
        template.convertAndSend("test-queue", EmailInfo(to, letter))
    }
}

EmailInfo is a business logic class, just class with fields.

My depedendencies

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-quartz")
    implementation("org.springframework.boot:spring-boot-starter-artemis")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0")
    implementation("io.jsonwebtoken:jjwt:0.12.5")
    implementation("javax.jms:javax.jms-api:2.0.1")
    implementation("org.apache.activemq:artemis-jakarta-client:2.33.0")
    implementation("org.apache.activemq:artemis-jakarta-server:2.33.0")
    implementation("org.apache.activemq:artemis-jms-server:2.33.0")
    implementation("org.apache.activemq:artemis-jms-client:2.33.0")
    implementation("org.springframework.boot:spring-boot-starter-mail")
    runtimeOnly("org.postgresql:postgresql")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

If I turn off ActiveMQ Artemis the error will be changed to:

Uncategorized exception occurred during JMS processing; nested exception is ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ219007: Cannot connect to server(s). Tried with all available servers.]

I think I need to configure my ActiveMQ Artemis, but I didn't find some extra settings in my case. Spring Boot guidelines tell what I just need to use JMS template.

P.S. in embedded configuration all works correctly


Solution

  • Your ActiveMQConnectionFactory is configured to use port 5672. This ConnectionFactory will use the Core protocol. However, in the default broker configuration port 5672 is configured exclusively for AMQP. Try using this instead:

    return ActiveMQConnectionFactory("tcp://localhost:61616", "artemis", "artemis")