In this exercise, you will have to add two service units and redeploy the service assembly by yourself.

Introduction

Instead of simply copying files from one directory to the other, we are now going to send the message straight to a JMS queue. We also want to retain a copy of the original message for archiving, so we are going to use the wiretap pattern for that.

Creating the projects

Adding Camel to the mix

You can also use servicemix-camel instead of servicemix-eip to implement the wiretap pattern. Just omit the steps related to tutorial-eip-su and have a look at Replacing tutorial-eip-su with tutorial-camel-su to learn how to do this.

First of all, we have to create two additional project modules for our two new service units. Instead of using the plain servicemix-service-unit archetype as before, we are now going to use two new archetypes:

  • servicemix-eip-service-unit to create module tutorial-eip-su
  • servicemix-jms-provider-service-unit to create tutorial-jms-su

Check this page if you need help in running the archetype. These archetypes are a bit more advanced:

  • they already add the required <dependency/> for the JBI component they target
  • they provide you with a sample xbean.xml to start from.

Afterwards, add these two new service units to the service assembly by adding the necessary <dependency/>s to the SA's pom.xml. If you want, you can also change the <name/>s to get a cleaner build log.

Configure tutorial-jms-su

Change the tutorial-jms-su's xbean.xml to access a queue named queue/tutorial on ActiveMQ (which is embedded in ServiceMix).

<beans xmlns:jms="http://servicemix.apache.org/jms/1.0"
       xmlns:tut="urn:servicemix:tutorial"
       xmlns:amq="http://activemq.org/config/1.0">

    <jms:endpoint service="tut:jms"
                  endpoint="myQueue"
                  role="provider" 
                  destinationStyle="queue"
                  jmsProviderDestinationName="queue/tutorial"
                  connectionFactory="#connectionFactory"/>

    <amq:connectionFactory id="connectionFactory" brokerURL="tcp://localhost:61616" />

</beans>

Configure tutorial-eip-su

Change the tutorial-eip-su's xbean.xml to define the wiretap we need.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:eip="http://servicemix.apache.org/eip/1.0"
       xmlns:tut="urn:servicemix:tutorial">

  <eip:wire-tap service="tut:wiretap" endpoint="endpoint">
    <eip:target>
      <eip:exchange-target service="tut:jms" />
    </eip:target>

    <eip:inListener>
      <eip:exchange-target service="tut:file" endpoint="sender" />
    </eip:inListener>
  </eip:wire-tap>

</beans>

We want to forward the message exchange to the jms endpoint, so we specify that information on the first <eip:exchange-target/>. The second one refers back to the <file:sender/> endpoint we declared earlier.

Configure tutorial-file-su

You have to change the targetService on the <file:poller /> endpoint to refer to our newly created wiretap. Afterwards, it should like this:

<file:poller service="tut:file" 
             endpoint="poller"
             file="file:/home/gert/poller" 
             targetService="tut:wiretap"/>

Build and deploy

When everything is done, you are ready to build and deploy the service assembly. When you do the build, the log should be similar to this one (if the SA isn't built last, you forgot to add the necessary <dependency/> to that pom.xml file):

[INFO] Scanning for projects...
[INFO] Reactor build order: 
[INFO]   Tutorial
[INFO]   Tutorial :: File SU
[INFO]   Tutorial :: JMS SU
[INFO]   Tutorial :: EIP SU
[INFO]   Tutorial :: SA
       ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
       ...

For the deploy, you have two ways :

  • you can deploy it directly using the JBI maven plugin
    mvn jbi:projectDeploy -DforceUpdate=true
    
  • you can create the SA zip file and copy the SA zip file into the SERVICEMIX_HOME/hotdeploy directory
    mvn install
    

Testing

If you copy a file into the poller's target directory now, it will also be moved into the sender's directory, just as before. However, there should also be a copy of the message in our JMS queue. To check this, connect to ServiceMix using a JMX console (refer back to our previous tutorial if you need help doing this) and navigate the MBean tree to org.apache.activemq/localhost/Queues. A queue should have been auto-created with the name queue/tutorial and the EnqueueCount attribute is showing the number of messages that have already been sent (in our case: 1).

Things to remember

  • More advanced Maven archetypes already create a sample xbean.xml file and add the necessary JBI component dependency to the pom.xml
  • Adding SU to an existing SA is as simple as adding <dependency/>s to the SA's pom.xml
  • To update a service assembly already deployed on servicemix, you need to use the mvn jbi:projectDeploy -DforceUpdate=true command or to create the new SA zip file using mvn install and copy the zip file into the SERVICEMIX_HOME/hotdeploy directory

Further reading

  • servicemix-jms provides more information on the configuration options we used for tutorial-jms-su
  • servicemix-eip gives you more details about the wire tap EIP, as well as all the other patterns that are supported by ServiceMix