Poll a filesystem for files with the Fuse ESB

Poll a filesystem for files with the Fuse ESB

This use case describes how to poll a file system and pass the file information on to another Service in FUSE ESB.
It demonstrates how to

  • Define a File Poller
  • Define a Service to receive the file information

In the Logisticx Demo this use case is defined in the "stock-processor-sa" assembly and the source code is within the "stock-processor" module of the Logsticx directory.



 

Walkthrough

To create the File Poller and deploy it to Fuse ESB you need to do the following:

1. Set up the stock processor project

  • Prerequisites
  • Create a POM file for this project

2. Creating a File Poller Service unit

3. Creating a Stock Service unit

4. Creating a Stock Service Assembly.

5. Deploy to FUSE ESB 

1. Setting up the project 

Pre-requisites 

To create and run the use case you need to download and install the following:

  • FUSE ESB 3.3.0.6 or higher
  • JDK 1.5 or higher
  • Apache Maven 2.0.6 or higher
  • The Logisticx demo source code

After installing Maven, you need to change the following settings in your operating system environment:

  • Set the M2_HOME environment variable to point at the Maven root directory.
  • Add the Maven bin directory (%M2_HOME%\bin on Windows or $M2_HOME/bin on UNIX) to your PATH.

Creating a Maven Project

All our use cases are Maven based around the Logisticx demo. This use case is a sub-project of the Logisticx demo.

So the first thing to do is create a directory called "
LOGISTICX_DIR/stock-processor" and place the following POM file within that directory.

Example 1.0: POM for Stock Processor

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

  <parent>
    <groupId>com.iona.fuse.demo.logisticx</groupId>
    <artifactId>logisticx-demo-parent</artifactId>
    <version>1.1-SNAPSHOT</version>
  </parent>

  <groupId>com.iona.fuse.demo.logisticx.stock.processor</groupId>
  <artifactId>stock-processor</artifactId>
  <version>${logisticx-version}</version>
  <name>Stock Processing Parent Project</name>
  <packaging>pom</packaging>

</project>

We have given this project a groupId of com.iona.fuse.demo.logisticx.stock.processor and an artifact of stock-processor.

Run the following to ensure everything builds ok.

=> mvn install

Also add the directory name as a module to the root Logisticx POM file. 

Example 1.1: Module definition

<modules>
    <module>model</module>
    <module>derby</module>
    <module>stock-processor</module>
   ....
  </modules>

We will now create each of our service units and assembly from here.

2. Creating a File Poller Service unit

As we plan to deploy our File Poller to FUSE ESB, we need to create a service unit to contain the service.

We will use a Maven archetype to create a project skeleton that will house the service. The resulting project will have a directory structure, a maven POM and a configuration file.

Run the Maven archetype command from the stock-processor directory.

Example 1.2: Archetype command 

mvn archetype:create \-DarchetypeGroupId=org.apache.servicemix.tooling
\-DarchetypeArtifactId=servicemix-file-poller-service-unit
\-DarchetypeVersion=3.3.1.5-fuse
\-DgroupId=com.iona.fuse.demo.logisticx.stock.processor
\-DartifactId=poll-stockfile-su
\-DremoteRepositories=[http://repo.open.iona.com/maven2/]

This creates a directory called poll-stockfile-su that is taken from the artifactId parameter.

The first three parameters to the mvn command identify which Maven archetype to use for the archetype:create goal, while the groupId and artifactId parameters identify the Maven project that is being generated.

Renaming the Project 

You need to give the project a unique name as follows:

In the poll-stockfile-su directory.

  1. Open the pom.xml in a text editor.
  2. Change the <name> attribute to <name>Stock File Polling File Service unit</name>.

Because we ran Maven's archetype:create from the directory containing the top level pom.xml, Maven has used the groupId from the parent pom.xml for our new service unit and added a module to the parent pom.xml.

Example 1.3: Module definition 

<modules>
    <module>poll-stockfile-su</module>
</modules>

Adding Configuration

The configuration for the File Poller will be added to the generated xbean.xml file.

 LOGISTICX_DIR/stock-processor/poll-stockfile-su/src/main/resources/xbean.xml

Change the xbean.xml file to the following

 Example 1.3: File Poller Configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:file="http://servicemix.apache.org/file/1.0"
       xmlns:logisticx="http://logisticx.demo.fuse.iona.com/">

    <file:poller service="logisticx:file"
        endpoint="poller"          file="file:${user.dir}${file.separator}StockInventory${file.separator}"
        targetService="logisticx:StockService"
        targetEndpoint="StockEndpoint"
        deleteFile="true"
        autoCreateDirectory="true"
        delay="120000" period="120000"/>

</beans>

The uses the ServiceMix File component which provides JBI integration to the file system. It can be used to read & write files via URI or to periodically poll directories for new files.

  • For demo purposes it will auto create a directory called StockInventory in the user directory which is generally the directory that the jdk has been invoked. You may change it to a prefered location on your local machine.
  • It will start to poll this directory two minutes after startup and every two minutes thereafter.
  • To demonstrate that new stock has arrived the user will have to copy some XML files that contain product information to this StockInventory directory - the polling service will take these files and forward it to the StockService where it will update the Warehouse Service to increase the quantity of products available.
  • On processing the file it will be deleted.

You will need to add the following dependencies to your poll-stockfile-su POM file

Example 1.4: Poll StockFile POM

<?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.iona.fuse.demo.logisticx.stock.processor</groupId>
  <artifactId>poll-stockfile-su</artifactId>
  <packaging>jbi-service-unit</packaging>
  <version>${logisticx-version}</version>
  <name>Stock File Polling File Service unit</name>
  <url>http://www.myorganization.org</url>

  <dependencies>
    <dependency>
      <groupId>org.apache.servicemix</groupId>
      <artifactId>servicemix-file</artifactId>
      <version>${servicemix-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.servicemix</groupId>
      <artifactId>servicemix-core</artifactId>
      <version>${servicemix-version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

 ....
</project>

3. Creating a Stock Service unit

The Stock Service uses the ServiceMix Bean component which provides integration with beans (POJOs) with the JBI bus to make it easy to use POJOs to process JBI message exchanges.

The Stock Service pojo implements the MessageExchangeListener interface. It acts as a consumer to send messages to a Warehouse Service to update the Stock Inventory. It uses the injected DeliveryChannel to send back the exchanges. 

Run the following maven archetype command from the stock-processor directory to create the skeleton project for the servicemix bean.

Example 1.5: Archetype command 

mvn archetype:create \-DarchetypeGroupId=org.apache.servicemix.tooling
\-DarchetypeArtifactId=servicemix-bean-service-unit
\-DarchetypeVersion=3.3.1.5-fuse
\-DgroupId=com.iona.fuse.demo.logisticx.stock.processor
\-DartifactId=stock-service-su
\-DremoteRepositories=[http://repo.open.iona.com/maven2/]

This creates a directory called stock-service-su.

Adding Configuration

Change the configuration for the Stock Service in the generated xbean.xml file.

LOGISTICX_DIR/stock-processor/stock-service-su/src/main/resources/xbean.xml

Example 1.6: Stock Service configuration

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

<beans xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:logisticx="http://logisticx.demo.fuse.iona.com/">

       <bean:endpoint service="logisticx:StockService"
             endpoint="StockEndpoint" bean="#stockProcessorBean" />
     <bean id="stockProcessorBean" class="com.iona.fuse.demo.logisticx.stock.processor.StockProcessor" />

</beans>

The Bean will create a single instance of the POJO per endpoint.

Implementation of MessageExchangeListener

Every message received for the POJO will be dispatched to the onMessageExchange method.

The archetype will generate a Sample Pojo file.

Example 1.7: Sample MyBean.java

package com.iona.fuse.demo.logisticx.stock.processor;

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;

public class MyBean implements MessageExchangeListener {

    @Resource
    private DeliveryChannel channel;

    public void onMessageExchange(MessageExchange exchange) throws MessagingException {
        System.out.println("Received exchange: " + exchange);
        exchange.setStatus(ExchangeStatus.DONE);
        channel.send(exchange);
    }

}

Here you have control to create new exchanges with other service and you may change the name of the Pojo.

The Stock Service in the Logisticx Demo sends a message to Warehouse web Service to update its Stock Inventory.

4. Creating a Stock Service Assembly


You will now create a Servicemix Service Assembly to wrap all the service units together and deploy it to FUSE ESB. Use the archetype:create command again.

mvn archetype:create
\-DarchetypeGroupId=org.apache.servicemix.tooling
\-DarchetypeArtifactId=servicemix-service-assembly
\-DarchetypeVersion=3.3.1.5-fuse
\-DgroupId=com.iona.fuse.demo.logisticx.stock.processor
\-DartifactId=stock-processor-sa
\-DremoteRepositories=[http://repo.open.iona.com/maven2/]

This generates a project called stock-processor-sa with a pom.xml file. Update the pom.xml file to add in the two dependencies on the service units.

 Example 1.13: stock-processor-sa POM

<?xml version="1.0" encoding="UTF-8"?><project>
  <modelVersion>4.0.0</modelVersion>

   ....

  <groupId>com.iona.fuse.demo.logisticx.stock.processor</groupId>
  <artifactId>stock-processor-sa</artifactId>
  <packaging>jbi-service-assembly</packaging>
  <name>Stock Processor SA</name>
  <version>${logisticx-version}</version>
  <url>http://www.myorganization.org</url>

  <dependencies>
    <dependency>
      <groupId>com.iona.fuse.demo.logisticx.stock.processor</groupId>
      <artifactId>poll-stockfile-su</artifactId>
      <version>${logisticx-version}</version>
    </dependency>
    <dependency>
      <groupId>com.iona.fuse.demo.logisticx.stock.processor</groupId>
      <artifactId>stock-service-su</artifactId>
      <version>${logisticx-version}</version>
    </dependency>
  </dependencies>

    .....

</project>

If you now run

=> mvn install

from the stock-processor directory you should be ready to deploy your stock-processor-sa to your installation of Servicemix.

5. Deploying to Fuse ESB

You are now ready to deploy your service assembly to FUSE ESB.

Deploying to Servicemix 3

Before you start, make sure that you have started ServiceMix.

You can navigate to your project's SA directory and use the JBI Maven plugin to deploy your project.

Run mvn jbi:projectDeploy

The command will not only deploy the service assembly, but also install any dependencies automatically. For this use case it will deploy the servicemix-file and servicemix-bean components.

You may also copy the SA to the hotdeploy folder.

Deploying to Servicemix 4 

You are now ready to deploy your service assembly to FUSE ESB.

Copy the built artifact to the "deploy" directory of FUSE ESB 4.

Start Servicemix - you can check if the service has started by running the "jbi list" comand

Labels

 
(None)