如何配置Camel SJMS 2组件的连接系数

hjzp0vay  于 2023-10-18  发布在  Apache
关注(0)|答案(1)|浏览(118)

我对配置SJMS 2组件的最佳方式感到困惑。我在一个简单的测试应用程序中使用camel-spring-boot-starter,并尝试使用SJMS 2 Camel组件从ProducerTemplate写入ActiveMQ Artemis。component documentation说它处理连接缓存之类的事情,我通常会在我的ConnectionFactory bean中配置这些东西,所以我觉得我应该在我的Configuration中定义比不使用Camel更少的东西。
文档似乎缺乏一个清晰的例子来说明如何在使用Camel Spring时配置jsms 2路由及其ConnectionFactory。有没有一个很好的例子,或者有人可以告诉我,这将是什么样子?以下是我目前的测试应用程序:
Spring Boot 应用:

@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");
    }
}

路线:

@Component
public class RestToJmsRoute extends RouteBuilder {

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

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

@Configuration
public class JmsConfig {

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

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>cameljmstest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cameljmstest</name>
    <description>test app</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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</groupId>
            <artifactId>camel-rest</artifactId>
            <version>3.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-sjms2</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

上述情况导致以下例外情况:
原因:java.lang.IllegalArgumentException:必须为sjms 2配置ConnectionResource或ConnectionFactory://test-topic

cwdobuhd

cwdobuhd1#

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

@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>

相关问题