Send and Fetch messages to JBoss EAP 6 using Camel

In this tutorial I will show how you can send and receive messages to a JBoss EAP 6 application server featuring the HornetQ messaging broker.

This example application uses Java DSL to build up the ConnectionFactory to JBoss EAP 6 and send/receive messages. If you want to have a look at a Spring based example, see this tutorial: Routing JMS messages to JBoss EAP 6 using Camel

The first source code is named SendMessages and uses a simple timer object to trigger the creation of Messages which will be sent to a Queue named testQueue:

package com.sample;

import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

/**
 * A Camel Application
 */
public class SendMessages {

    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";

    private static final String DEFAULT_USERNAME = "jmsuser";
    private static final String DEFAULT_PASSWORD = "Password1!";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://localhost:4447";

    public static void main(String... args) throws Exception {
        CamelContext context = new DefaultCamelContext();

        ConnectionFactory connectionFactory = null;
        final Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
        env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
        env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
        Context ctx = new InitialContext(env);

        connectionFactory = (ConnectionFactory) ctx.lookup(DEFAULT_CONNECTION_FACTORY);

        org.apache.camel.component.jms.JmsComponent jms = new org.apache.camel.component.jms.JmsComponent();

        jms.setConnectionFactory(connectionFactory);

        context.addComponent("test-jms", jms);

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("timer://foo?repeatCount=5").
                          setBody(constant("Hello world at "+new java.util.Date())).
                              to("test-jms:queue:testQueue");
            }
        });

        context.start();

        Thread.sleep(5000);
        context.stop();
    }

}

Please note that in order to work, you must have defined an application user on EAP 6 named "jmsuser" with password "Password1" belonging to the "guest" group. Also the Queue named testQueue must be defined on the application server. You can do that using the CLI as follows:

jms-queue add --queue-address=testQueue --entries=queue/test,java:jboss/exported/jms/queue/test

The second example, called FetchMessages, can be used to fetch the messages on the testQueue,, printing them on the System.out stream using the stream: component:

package com.sample;

import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class FetchMessages {

    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";

    private static final String DEFAULT_USERNAME = "jmsuser";
    private static final String DEFAULT_PASSWORD = "Password1!";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://localhost:4447";

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        CamelContext context = new DefaultCamelContext();

        ConnectionFactory connectionFactory = null;
        final Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
        env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
        env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
        Context ctx = new InitialContext(env);

        connectionFactory = (ConnectionFactory) ctx.lookup(DEFAULT_CONNECTION_FACTORY);

        org.apache.camel.component.jms.JmsComponent jms = new org.apache.camel.component.jms.JmsComponent();

        jms.setConnectionFactory(connectionFactory);

        context.addComponent("test-jms", jms);

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("test-jms:queue:testQueue").to("stream:out");
            }
        });

        context.start();

        Thread.sleep(2000);
        context.stop();
    }

}

You can find the full project on github at: https://github.com/fmarchioni/mastertheintegration/tree/master/camel/jms/camel-eap6


Advertisement

Cookie Alert