ServiceMix (Fuse ESB) – Bean Service Unit – Part 3

ServiceMix (Fuse ESB) – Bean Service Unit – Part 3

Today I will describe how to connect bean type service unit with our existing application build in Part 1 and Part 2.

The ServiceMix Bean component provides integration with beans (POJOs) with the JBI bus to make it easy to use POJOs to process JBI message exchanges. Like in an Message Driven Bean in J2EE a POJO will receive a message from the NMR and process it in any way it likes. Unlike in a JMS component where the coding is already done the Bean component gives the developer the freedom to create any type of message handling but it must be hand coded all the way.

This part of the tutorial shows how to integrate bean service unit to process simple xml message.

We start off by creating a new service unit for our existing project. To do this, go to the project parent folder and execute the following mvn command:

mvn archetype:create \
  -DarchetypeGroupId=org.apache.servicemix.tooling \
  -DarchetypeArtifactId=servicemix-bean-service-unit \
  -DarchetypeVersion=2011.01 \
  -DgroupId=com.softwarepassion.jmsfile \
  -DartifactId=jmsfile_bean \
  -Dversion=1.0.0

This command will create our new service unit project and adds new module into our parent pom.xml file.

Now we need to add a dependencies to our newly created service unit for working with MessageExchangeListener:

<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.softwarepassion.jmsfile</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.softwarepassion.jmsfile</groupId>
    <artifactId>jmsfile_bean</artifactId>
    <version>1.0.0</version>
    <packaging>jbi-service-unit</packaging>
    <name>Apache ServiceMix :: Bean Service Unit</name>
    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.servicemix.tooling</groupId>
                <artifactId>jbi-maven-plugin</artifactId>
                <version>4.4</version>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.servicemix</groupId>
            <artifactId>servicemix-bean</artifactId>
            <version>2011.01</version>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix</groupId>
            <artifactId>servicemix-core</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>
</project>

as well as add our service unit dependency to the service assembly:

....
<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>
    <dependency>
        <groupId>com.softwarepassion.jmsfile</groupId>
        <artifactId>jmsfile_bean</artifactId>
        <version>1.0.0</version>
    </dependency>
  </dependencies>
...

Remember that you can make any service unit an eclipse project by executing ‘mvn eclipse:eclipse’ inside that project main directory (where the pom.xml file is located).

Run ‘mvn install’ to test the newly created skeleton code.

If all is fine, you can now proceed wiring our new service unit. For this purpose to make it as simple as possible we will create new file service endpoint which will poll the directory for new files and pass them to our bean service endpoint.

At this point our application reads the incoming messages from the JMS queue and save them to the specified directory. Right now we add another endpoint witch will pick up the files from that directory and process them using bean type endpoint – possibly useless in the real world but will show you how to create and connect different types of endpoints in ServiceMix ESB.

First, we need to modify xbean.xml file in our existing file service unit:

<?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"
>

    <file:sender service="jmsfile:fileservice"
                 endpoint="jmsfile:fileendpoint"
                 directory="file:/home/kris/Desktop/filesfromjms" />
   
    <file:poller service="jmsfile:fileservice"
                 endpoint="jmsfile:fileendpointpoller"
                 targetService="jmsfile:beanservice"
                 targetEndpoint="jmsfile:bean"
                 file="file:/home/kris/Desktop/filesfromjms"/>
</beans>

file:poller will poll our directory for new files and pass them to the jmsfile:beanservice service.

Our xbean.xml for bean service unit will look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by Apache ServiceMix Archetype -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:file="http://servicemix.apache.org/file/1.0"
       xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:ns="urn:test:example"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://servicemix.apache.org/file/1.0
         http://servicemix.apache.org/file/1.0/servicemix-file.xsd
         http://servicemix.apache.org/bean/1.0
         http://servicemix.apache.org/bean/1.0/servicemix-bean.xsd"
>
       
  <bean:endpoint service="jmsfile:beanservice" endpoint="jmsfile:bean" bean="#myBean"/>

  <!--
    The POJO used by the bean endpoint.
  -->
  <bean id="myBean" class="com.softwarepassion.jmsfile.MyBean"/>
  <!-- END SNIPPET: bean -->

</beans>

and finally our class for handling messages polled from the directory:

package com.softwarepassion.jmsfile;

import org.apache.servicemix.MessageExchangeListener;

import javax.annotation.Resource;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;

public class MyBean implements MessageExchangeListener {

    @Resource
    private DeliveryChannel channel;

    public void onMessageExchange(MessageExchange exchange) throws MessagingException {
        System.out.println("Received exchange: " + exchange);
       
        System.out.println("Received exchange: " + exchange);
        System.out.println("getPattern exchange: " + exchange.getPattern());
        System.out.println("getStatus exchange: " + exchange.getStatus());
        System.out.println("Role exchange: " + exchange.getRole().toString());
        NormalizedMessage in = exchange.getMessage("in");
        System.out.println("normalized message: "+in.getContent().toString());
        exchange.setStatus(ExchangeStatus.DONE);
        channel.send(exchange);
    }

}

The basic skeleton for xbean.xml and MyBean.java files were created by the maven archetype in the earlier step.

Now you are ready to test our application, you don’t have to send messages to the queue if you don’t want to, you can just put any xml file into the same directory where the jms messages were saved earlier. Once you deploy the service assembly zip file into your Fuse ESB instance the file should be printed to the console.

Similar to the previous parts, you can get the full source code from GitHub https://github.com/softberries/jms_file_tut/ ‘bean_su’ branch.

Leave a Reply