0
votes

I'm confused about the best way to configure the SJMS2 component. I'm using camel-spring-boot-starter in a simple test application and trying to write from a ProducerTemplate to ActiveMQ Artemis using the SJMS2 Camel Component. The component documentation says it handles things like connection caching and such which I would normally configure in my ConnectionFactory bean, so I'm getting the sense that I should defining less in my Configuration than I would without using Camel.

The documentation seems to lack a clear example of how to configure an jsms2 route and its ConnectionFactory when using Camel Spring. Is there a good example of this somewhere or could someone show me how that would look? Here's my current test application:

Spring Boot Application:

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

Route:

@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 (which is clearly not being used by the Camel component)

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

The above is resulting in the following exception:

Caused by: java.lang.IllegalArgumentException: ConnectionResource or ConnectionFactory must be configured for sjms2://test-topic

1
Of course after I look at the documentation again I see the section about spring boot auto configuration. Looks like I need to use the starter artifact for camel-sjms2. I'll still need to figure out exactly what I should be configuring and how, though so I won't delete this question yet.b15

1 Answers

0
votes

I ended up adding the following configuration class:

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

            }
        };
    }
}

and then I changed my pom dependencies to these:

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