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

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

This is the second part of the tutorial on how to build ServiceMix ESB application connecting file system service unit with JMS.
You can find the first part of the tutorial here where I’ve described how to prepare your environment to test our application and possibly other applications in the future. The first part was about creating simple java application to send XML messages to ActiveMQ broker as well on how to set up ActiveMQ web console to watch incoming/outgoing messages.
In this part I will describe how to create necessary service units and service assembly to be able to receive JMS messages on the ESB and save them to the file system.

We start with creating a parent project folder for our service units and service assembly, create an empty directory and put the following pom.xml file in it:

<?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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
  <groupId>com.softwarepassion.jmsfile</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Tutorial</name>
  <url>http://servicemix.org</url>
  <modules>
  </modules>
</project>

Now we are going to create our first service unit responsible for filesystem interaction, execute the following maven command:

mvn archetype:create \
  -DarchetypeGroupId=org.apache.servicemix.tooling \
  -DarchetypeArtifactId=servicemix-file-service-unit \
  -DgroupId=com.softwarepassion.jmsfile \
  -DartifactId=jmsfile_file \
  -Dversion=1.0.0

You can find out more about File SU directly on the ServiceMix website at: http://servicemix.apache.org/servicemix-file.html and about all maven artefacts here: http://servicemix.apache.org/maven-archetypes.html , the information there can be a bit outdated though.

Next we create JMS SU for managing our ActiveMQ communication:

mvn archetype:create \
  -DarchetypeGroupId=org.apache.servicemix.tooling \
  -DarchetypeArtifactId=servicemix-jms-provider-service-unit \
  -DgroupId=com.softwarepassion.jmsfile \
  -DartifactId=jmsfile_jms \
  -Dversion=1.0.0

More info about JMS service unit can be found @ http://servicemix.apache.org/servicemix-jms.html and here: http://servicemix.apache.org/servicemix-jms-new-endpoints.html

Finally we create service assembly connecting our service units:

mvn archetype:create \
  -DarchetypeGroupId=org.apache.servicemix.tooling \
  -DarchetypeArtifactId=servicemix-service-assembly \
  -DgroupId=com.softwarepassion.jmsfile \
  -DartifactId=jmsfile_sa \
  -Dversion=1.0.0

All the modules should be automatically added to our main pom.xml file in the parent project folder.
After creating our Service Assembly we need to update its pom.xml to include dependencies to our service units:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.softwarepassion.jmsfile</groupId>
      <artifactId>jmsfile_file</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.softwarepassion.jmsfile</groupId>
      <artifactId>jmsfile_jms</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
  ...
</project>

Before executing ‘mvn install’ on the main pom.xml we need to do one more thing which is updating the jms service unit with ‘activemq-core’ dependency

...
<dependencies>
    <dependency>
      <groupId>org.apache.servicemix</groupId>
      <artifactId>servicemix-jms</artifactId>
      <version>2011.01</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>5.2.0</version>
    </dependency>
  </dependencies>
...

Now you are ready to run ‘mvn install’ and see if all is ok at this point.

Ok, so we have two service units and service assembly ready, now its time to wire them together, this is done using xml in xbean.xml files. First we modify our xbean.xml file for the file su located at:
‘jms_file_tut/jmsfile_file/src/main/resources’

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by Apache ServiceMix Archetype -->
<beans xmlns:file="http://servicemix.apache.org/file/1.0"
    xmlns:replaceMe="http://servicemix.apache.org/replaceMe" xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://servicemix.apache.org/file/1.0 http://servicemix.apache.org/schema/servicemix-file-2011.01.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>

    <!--
        The ServiceMix File component provides integration to the file system.
        It can be used to read and write files via URI or to periodically poll directories
        for new files.
       
            http://servicemix.apache.org/servicemix-file.html
    -->
    <!-- BEGIN SNIPPET: file-sender -->
    <!--
        The sender endpoint creates a file from the incoming message and writes it into the specified directory.
       
        Attributes:
            service                     : the endpoint service name
            endpoint                    : the endpoint name
            directory                   : the target directory where to write created file
            autoCreateDirectory         : creates directory if it doesn't exist
            append                      : append to an existing file instead of overwriting it
            marshaler                   : implementation of the FileMarshaler to use (DefaultFileMarshaler by default)
            tempFilePrefix              : the prefix of the temporary file used during file generation
            tempFileSuffix              : the suffix of the temporary file used during file generation
    -->
    <file:sender service="jmsfile:fileservice"
                 endpoint="jmsfile:fileendpoint"
                 directory="file:/home/kris/Desktop/filesfromjms" />
    <!-- END SNIPPET: file-sender -->

</beans>

This is a simple single endpoint declaration with directory name specified for saving our JMS messages.

For the jms service unit our xbean.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:jms="http://servicemix.apache.org/jms/1.0"
       xmlns:test="http://test"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/jms/1.0 http://servicemix.apache.org/schema/servicemix-jms-3.2.3.xsd
       http://activemq.org/config/1.0 http://activemq.apache.org/schema/core/activemq-core-4.1.1.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>

    <jms:consumer service="jmsfile:jmsservice"
                  endpoint="jmsfile:jmsendpoint"
                  destinationName="test"
                  connectionFactory="#connectionFactory"
          targetService="jmsfile:fileservice"
              targetEndpoint="jmsfile:fileendpoint" />
   
    <amq:connectionFactory id="connectionFactory" brokerURL="tcp://localhost:61616" />

</beans>

This endpoint will consume JMS messages sent to the ‘test’ queue.

Once the wiring is complete, you can run ‘mvn install’ on the main pom.xml file and then copy the ‘.zip’ file from service assembly target folder into the ‘deploy’ directory of your service mix instance.

To watch the log output continuously you can execute the following command on the service mix console:

log:tail

Now you can execute our Producer program introduced in Part I of this tutorial and send 10 test messages to ActiveMQ. You should find them saved into the directory specified in xbean.xml of the file service unit configuration.

The full source code for this example is available at github at: https://github.com/softberries/jms_file_tut

Leave a Reply