骆驼ActiveMQ + Spring boot无法读取Spring ActiveMQ配置

6
我正在尝试使用Spring Boot 1.5.2.RELEASE + Camel (Spring Boot Starter) + ActiveMQ来实现一个非常简单的路由,即从特定队列中读取并进行日志记录。然而,似乎它没有选择我的spring.activemq配置URL,因为我在日志中看到它正在尝试连接到不同的URL,并且它继续连接它,我的Spring Boot应用程序从未启动。基于我提供的配置,以下是我的问题:
  1. 修复配置以允许Spring的activemq配置
  2. 配置maxReconnectAttempts,以便如果ActiveMQ实例关闭,则不会一直尝试连接无法到达的URL
任何帮助都将不胜感激。我在stackoverflow上搜索了相关问题,但没有给我解决我面临的问题的解决方案。
我在控制台上看到的错误,这个错误持续了大约60-70次尝试并继续增加。您可以看到Camel正在选择的代理URL是一些默认URL,可能是Spring默认配置的。
Failed to connect to [tcp://localhost:61616] after: 10 attempt(s) continuing to retry.

以下是我目前的配置/代码:

pom.xml - 相关部分

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- Spring Cloud is part of the project where I am configuring camel routes -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-dependencies</artifactId>
            <version>2.19.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- I have this as the same project works as a web app as well 
    and therefore I do not need the 
    camel.springboot.main-run-controller=true configuration to be set
    which is as per camel's spring boot documentation-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Camel - start -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-camel</artifactId>
    </dependency>
    <!-- Camel - end -->

</dependencies>

应用程序配置文件 application.yml(参考 Spring Boot ActiveMQProperties

spring:
  activemq:
    brokerUrl: tcp://my.company.host:[port] //This port is up and running
    user: user
    password: password

JAVA中的Camel路由

package com.mycamel.route;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class SampleAmqCamelRouter extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("activemq:some.queue").to("log:com.mycamel.route?level=INFO&groupSize=10");
    }

}
1个回答

5
首先,您应向pom.xml添加"spring-boot-starter-activemq"依赖项。然后,您可以使用其自动配置功能,该功能将基于您在application.yml中指定的属性创建ConnectionFactory。
之后,您还需要配置Camel的ActiveMQComponent。如果您想重用由自动配置创建的ConnectionFactory,则可以通过以下方式实现:
@Configuration
public class ActiveMQComponentConfig {

    @Bean(name = "activemq")
    public ActiveMQComponent createComponent(ConnectionFactory factory) {
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConnectionFactory(factory);
        return activeMQComponent;
    }
}

您可以在Camel的ActiveMQ文档中找到更多信息。


非常感谢!添加“activemq” bean解决了问题。我确实添加了spring-boot-starter-activemq依赖项,但也没有起作用。实际上,两者的组合才起到了作用。 - Sayantan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接