Quick guide to running Camel applications on WildFly Swarm

IMPORTANT: The Thorntail project (formerly known as WildFly Swarm) has reached End Of Life. The last Thorntail release is the 2.7.0. You are recommended to evaluate a migration plan for your applications. Check this tutorial to learn more: How to migrate Thorntail applications

WildFly Swarm offers an innovative approach to packaging and running Java EE applications by packaging them with just enough of the server runtime to “java -jar” your application. In this tutorial we will learn how a Camel application using JMS messages can be launched using WildFly Swarm (built on the top of WildFly 11).

The simplest way to set up a WildFly Swarm application is by using the WildFly Swarm project generator which is available at: http://wildfly-swarm.io/generator/

The dependencies needs to be specified, so include camel-jms and camel-cdi. The core camel library will be automatically linked by those dependencis.

 

WildFly Swarm camel tutorial

 

Click on Generate Project which will download the Maven projects in a zip file.

Unzip the project and add the following example class into the src/main/java/org/wildfly/swarm/examples/camel/jms folder :

package org.wildfly.swarm.examples.camel.jms;

import java.util.Date;

import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.jms.ConnectionFactory;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.jms.JmsComponent;

@ApplicationScoped
@ContextName("camel-jms-context")
public class JmsRouteBuilder extends RouteBuilder {

    @Resource(mappedName = "java:jboss/DefaultJMSConnectionFactory")
    private ConnectionFactory connectionFactory;

    @Override
    public void configure() throws Exception {
 
        JmsComponent component = new JmsComponent();
        component.setConnectionFactory(connectionFactory);

        getContext().addComponent("jms", component);
 
        from("timer:produceJMSMessage?period=5s&delay=0&fixedRate=true")
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                Integer count = exchange.getProperty(Exchange.TIMER_COUNTER, Integer.class);
                Date date = exchange.getProperty(Exchange.TIMER_FIRED_TIME, Date.class);

                exchange.getOut().setBody(String.format("Message %d created at %s", count, date.toString()));
            }
        })
         .to("jms:queue:SampleQueue");

        from("jms:queue:SampleQueue")
        .log("TestQueue received message: ${body}");

    }
}

The purpose of this sample class will be firing JMS messages into the jms:queue:SampleQueue. The messages will be consumed and printed by a second Route.

All you need now is binding and running your application with:

$ mvn wildfly-swarm:run

In a while the required dependencies will be downloaded. At the end of this process, the application server will start and the Camel Ruoute will be deployed.

Check from the logs that messages are being delivered:

2018-01-18 18:07:40,770 INFO  [org.apache.camel.component.jms.DefaultJmsMessageListenerContainer] (Camel (camel-jms-context) thread #0 - JmsConsumer[SampleQueue]) Successfully refreshed JMS Connection
2018-01-18 18:07:40,822 INFO  [route2] (Camel (camel-jms-context) thread #2 - JmsConsumer[SampleQueue]) TestQueue received message: Message 1 created at Thu Jan 18 18:07:34 CET 2018
2018-01-18 18:07:40,830 INFO  [route2] (Camel (camel-jms-context) thread #2 - JmsConsumer[SampleQueue]) TestQueue received message: Message 2 created at Thu Jan 18 18:07:39 CET 2018
2018-01-18 18:07:45,015 INFO  [route2] (Camel (camel-jms-context) thread #2 - JmsConsumer[SampleQueue]) TestQueue received message: Message 3 created at Thu Jan 18 18:07:44 CET 2018
2018-01-18 18:07:49,991 INFO  [route2] (Camel (camel-jms-context) thread #2 - JmsConsumer[SampleQueue]) TestQueue received message: Message 4 created at Thu Jan 18 18:07:49 CET 2018
2018-01-18 18:07:54,991 INFO  [route2] (Camel (camel-jms-context) thread #2 - JmsConsumer[SampleQueue]) TestQueue received message: Message 5 created at Thu Jan 18 18:07:54 CET 2018

Enjoy the Camel ride on WildFly Swarm!

Found the article helpful? if so please follow us on Socials