Building your first JBoss Fuse project with Maven

Note: There’s an updated tutorial about JBoss Fuse here.

In this tutorial we will create our first Red Hat JBoss Fuse project using Maven. The project will contain a Camel route with OSGi blueprint support that is ready to be deployed in JBoss Fuse.

Requisites:

You need Maven and Red Hat Fuse installed.

Let’s build the project from the camel-archetype-blueprint archetype:

mvn:archetype:generate

Choose a number or apply filter : camel-archetype-blueprint

Choose archetype:

1: remote -> org.apache.camel.archetypes:camel-archetype-blueprint (Creates a new Camel project with OSGi blueprint support. Ready to be deployed in OSGi.)

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co

ntains): : 1

Choose org.apache.camel.archetypes:camel-archetype-blueprint version:

. . . .

45: 2.15.0

Choose a number: 45: 45

Define value for property 'groupId': : com.hellofuse
Define value for property 'artifactId': : fuse-blueprint
Define value for property 'version':  1.0-SNAPSHOT: : 1
Define value for property 'package':  com.hellofuse: :


Confirm properties configuration:

groupId: com.hellofuse
artifactId: fuse-blueprint
version: 1

package: com.hellofuse

camel-version: 2.15.0
log4j-version: 1.2.17
maven-bundle-plugin-version: 2.3.7
maven-compiler-plugin-version: 2.5.1
maven-resources-plugin-version: 2.6
slf4j-version: 1.7.10

 Y: : Y

Now the project fuse-blueprint has been created. You can perform the above steps using your favorite IDE as well. In case you have done it using just the shell, just import the Maven project in your IDE. The project contains a blueprint.xml file under src/main/resources/OSGI-INF/blueprint

The Blueprint Service is a way of instantiating and consuming services provided by others by means of an external XML configuration file. The Blueprint service is heavily inspired by the Spring Framework

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

  <bean id="helloBean" class="com.hellofuse.HelloBean">
      <property name="say" value="Hi from Camel"/>
  </bean>

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <route id="timerToLog">
      <from uri="timer:foo?period=5000"/>
      <setBody>
          <method ref="helloBean" method="hello"/>
      </setBody>
      <log message="The message contains ${body}"/>
      <to uri="mock:result"/>
    </route>
  </camelContext>

</blueprint>

The archetype contains a simple Camel Route which moves messages using a Timer component to a Mock endpoint. As the purpose of this tutorial is to create a brand new blueprint we will comment the bean and camelContext defintion.

Defining our service

Our service will move text files contained in the filesystem to the ActiveMQ broker that is embedded into JBoss Fuse. Please note that you need to provide the userName and password created using the esb:create-admin-user in the Fuse console.

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

	<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
		<property name="brokerURL" value="tcp://localhost:61616" />
		<property name="userName" value="admin" />
		<property name="password" value="Password1!" />
	</bean>

	<camelContext xmlns="http://camel.apache.org/schema/blueprint">
		<route id="file-to-jms-route">
			<from uri="file:/usr/data/in?noop=true" />
			<log message="Receiving order ${file:name}" />
			<to uri="activemq:incomingOrders" />
			<to uri="mock:result"/>
		</route>
	</camelContext>

</blueprint>

The Mock component included provides a powerful declarative testing mechanism as it allows declarative expectations to be created on any Mock endpoint before a test begins. As the archetype includes a Test class, you can perform a test and the expectations can be asserted in a test case to ensure the system worked as expected.

Here is the Mock class that will be used to test the Service:

public class RouteTest extends CamelBlueprintTestSupport {
	
    @Override
    protected String getBlueprintDescriptor() {
        return "/OSGI-INF/blueprint/blueprint.xml";
    }

    @Test
    public void testRoute() throws Exception {
        // the route is timer based, so every 5th second a message is send
        // we should then expect at least one message
    	 
        getMockEndpoint("mock:result").expectedMinimumMessageCount(1);

        // assert expectations
        assertMockEndpointsSatisfied();
    }

}

That’s all about the code. Now in order to run the test you need to include within your pom.xml include the camel-jms and activemq-camel dependency:

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-jms</artifactId>
	<version>2.14.1</version>
</dependency>
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-camel</artifactId>
	<version>5.7.0</version>
</dependency>

Fine, now start JBoss Fuse:

bin\fuse

Execute from the shell:

mvn clean install

This will package your project and execute a route test.

Now install the OSGI bundle on Jboss Fuse. From the Karaf console execute:

JBossFuse:karaf@root> osgi:install -s mvn:com.hellofuse/fuse-blueprint/1

Bundle ID: 259

Now enter the hawtio console at: http://localhost:8181/hawtio/index.html . As you can see, the execution can be debugged from the Camel tab, which contains the statistics about the route that has been invoked:

jboss fuse tutorial example hello world

Within the ActiveMQ tab, you can check for the messages that have been directed to the “incomingOrders” queue:

jboss fuse tutorial example hello world

By clicking on Browse you can display the body of the message as shown by the following picture:

jboss fuse tutorial example hello world

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