如何配置骆驼SJMS2组件的连接系数

时间:2020-06-05 16:15:52

标签: apache-camel jms

我对配置SJMS2组件的最佳方法感到困惑。我在一个简单的测试应用程序中使用dev.db.host,并尝试使用SJMS2 Camel Component从camel-spring-boot-starter写入ActiveMQ Artemis。 component documentation说它处理诸如连接缓存之类的事情,而这些事情我通常会在ConnectionFactory bean中进行配置,因此,我感觉我在Configuration中的定义要比不使用Camel的定义少。

文档似乎缺少使用Camel Spring时如何配置jsms2路由及其ConnectionFactory的清晰示例。是否在某个地方有很好的例子,或者有人可以告诉我它的外观?这是我当前的测试应用程序:

Spring Boot应用程序:

ProducerTemplate

路线:

@SpringBootApplication
public class WebhookpublisherApplication implements CommandLineRunner {

    @Autowired
    private ProducerTemplate producerTemplate;

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

    @Override
    public void run(String... args) throws Exception {
        producerTemplate.sendBody("direct:test", "testBody");
    }
}

JMS Config(骆驼组件显然没有使用它)

@Component
public class RestToJmsRoute extends RouteBuilder {

    @Override
    public void configure() {
        from("direct:test")
                .log("Recieved a test body: ${body}")
                .to("sjms2:topic:test-topic");
    }
}

POM.xml

@Configuration
public class JmsConfig {

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        return new ActiveMQConnectionFactory("tcp://localhost:61616");
    }
}

以上结果导致以下异常:

由以下原因引起:java.lang.IllegalArgumentException:ConnectionResource或 必须为sjms2:// test-topic

配置ConnectionFactory

1 个答案:

答案 0 :(得分:0)

我最终添加了以下配置类:

@Configuration
public class CamelContextConfig {

    @Bean
    public CamelContextConfiguration contextConfiguration() {
        return new CamelContextConfiguration() {
            @Override
            public void beforeApplicationStart(CamelContext context) {
                Sjms2Component component = new Sjms2Component();

                ActiveMQConnectionFactory activeMqConnectionFactory = new ActiveMQConnectionFactory("tcp://myArtemisHost:61616");
                activeMqConnectionFactory.setUser("user");
                activeMqConnectionFactory.setPassword("pass");

                component.setConnectionFactory(activeMqConnectionFactory);

                context.addComponent("sjms2", component);
            }

            @Override
            public void afterApplicationStart(CamelContext camelContext) {

            }
        };
    }
}

然后我将pom依赖项更改为以下内容:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-artemis</artifactId>
        <version>2.3.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>3.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-sjms2-starter</artifactId>
        <version>3.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>