ServiceMix (Fuse ESB) - File SU + JMS SU Example - Part 1

ServiceMix (Fuse ESB) – File SU + JMS SU Example – Part 1

Continuing my adventure with ESB’s and ServiceMix in particular I will describe a simple example of File system and JMS integration.
First of all, we need to do some preparation work to be able to send JMS messages and check if they are delivered/consumed on the JMS message broker. For the purpose of this tutorial I’m going to use Apache ActiveMQ, which by default is provided with ServiceMix.
In this part I’m going to show you how to install ActiveMQ web console for simple monitoring tasks as well as how to build a simple Java application sending XML messages to our JMS message broker.
In the second part of this tutorial I’ll post an example showing how to use ESB to connect JMS and File service units, the application will consume JMS messages and save them to the file system.
So let’s get started.

1. I assume you have downloaded the Fuse ESB and you are able to start it. If not, please have a look into my previouse tutorial under ‘ESB’ category: http://www.softwarepassion.com/apache-servicemix-fuse-esb-with-camel/ , you don’t have to go through the whole tutorial, just read the part about installing and running Fuse ESB instance.

2. First we start with ActiveMQ web console. To install the webconsole you have to execute two commands on the servicemix(fuseesb) console, go to the terminal window where your servicemix is started and type:

features:install war
features:install activemq-web-console

TIP: You can find out more about available features by executing:

features:list

This should install your activemq webconsole application. Go and check it out at: http://localhost:8181/activemqweb/


3. Now we are going to create simple java application allowing us to send JMS messages to our ActiveMQ instance. Most of this steps are borrowed from the blog post of Ian Christian found here

First, create the simplest maven skeleton by issuing:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.softwarepassion.activemq -DartifactId=producer -Dversion=1.0

Open up the newly created project in eclipse (or any other editor/ide you use) and change the pom.xml file by adding some dependencies:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.softwarepassion.activemq</groupId>
  <artifactId>producer</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>producer</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.jms</groupId>
      <artifactId>jms</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
     <artifactId>activemq-core</artifactId>
      <version>5.2.0</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>jboss</id>
      <url>http://repository.jboss.com/maven2</url>
      <releases>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
</project>

Once this is ready, we can start coding our Java stuff.
We need just two classes:

package com.softwarepassion.activemq;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnectionFactory;



public class App
{

    public static String brokerURL = "tcp://localhost:61616";
   
    public static void main( String[] args ) throws JMSException
    {
        // setup the connection to ActiveMQ
        ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);
        Producer producer = new Producer(factory, "test");
        producer.run();
        producer.close();
    }
}
package com.softwarepassion.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
 
public class Producer
{
    private ConnectionFactory factory;
    private Connection connection;
    private Session session;
    private MessageProducer producer;
 
    public Producer(ConnectionFactory factory, String queueName) throws JMSException
    {
        this.factory = factory;
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(queueName);
        producer = session.createProducer(destination);
    }
 
    public void run() throws JMSException
    {
        for (int i = 0; i < 10; i++)
        {
            System.out.println("Creating Message " + i);
            Message message = session.createTextMessage(this.mTxt);
            producer.send(message);
        }
    }
 
    public void close() throws JMSException
    {
        if (connection != null)
        {
            connection.close();
        }
    }
 
    private String mTxt =   "<?xml version="1.0" encoding="UTF-8"?>" +
                            "<note>" +
                            "<to>Anyone</to>" +
                            "<from>Kris</from>" +
                            "<heading>Simple Message</heading>" +
                            "<body>Check out my Twitter account @grajo</body>" +
                            "</note>";
}

Once you build your app with maven you can test it by running the App main method. Make sure that your ServiceMix instance is up and running. On Eclipse you can use the shortcut to run your application (Alt + Shift + X and then J).

Once you run your app the new queue named ‘test’ should be created in your ActiveMQ and 10 XML messages should be visible on that queue.

You can download the eclipse maven based project with the full source code for this example from github at: https://github.com/softberries/activemq_tut
In the next part we will build a ServiceMix application which will listen on the test queue for incoming messages and save them as files onto filesystem.

4 responses on “ServiceMix (Fuse ESB) – File SU + JMS SU Example – Part 1

  1. porn videos sex May 2, 2013 at 6:08 pm

    Hi there, I enjoy reading through your post.

    I like to write a little comment to support you.

  2. http://teenjenna.com July 21, 2013 at 3:03 am

    You made some really good points there. I checked on the internet to find out more about the issue
    and found most individuals will go along with your views on
    this website.

Leave a Reply