Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

This document describes how to run the ServiceMix's File Binding example and provides details about what it does. For information on the business use case, please refer to: Use Case for File Binding.

The File Binding example illustrates the following:

  • use an example of declarative programming
  • how to interact with the file system
  • how to use a WorkManager thread pool

The XML code for the File Binding example is located in the ServiceMix installation directory under the examples\file-binding directory in the servicemix.xml file. It is recommended that you refer to the servicemix.xml file while reading this document. The Java class files are located in the servicemix-1.0.jar file in the ServiceMix installation directory. To look at the Java source code, either unjar and decompile the .class files or download the source code. The source code is necessary for #Useful Code Hints section below.

The File Binding XML file, servicemix.xml, contains two components and a supporting bean. One component waits for a file to be deposited into the the inbox directory, retrieves it, sends it creates a normalized message that contains the file, then sends the message to the Normalized Message Router (NMR). The NMR routes the file to another the other component, which deposits it into an outbox directory. The bean provides a thread pool to enhance the performance of the reading and processing of the files. Details about the components and bean are discussed below.

Info
titleNote

The file to be transmitted from the inbox to outbox must be in XML format.

Running the File Binding Example

From a command shell, go to the File Binding example directory:

Code Block
cd [servicemix_install_dir]\examples\file-binding

...

If you would like to see more files moved from inbox to outbox, copy another file into the inbox directory. The file binding program continually polls (every 1000 ms) for new files while it is running, so any new file placed in inbox, will be transmitted to outbox.

Stopping the File Binding Example

To terminate the File Binding example type "CTRL-C" in the command shell in which it is running and answer "y" to the "Terminate batch job (y/n)?" question.

...

Component or Bean ID

Description

filePoller

This component periodically checks the "inbox" directory looking for files. If there is a file or directory present, it adds the file to the "workingSet", which is a collection of files to be processed. The workManger is invoked to schedule the work of processing the file from the workingSet. Another thread is created and the processing of the file begins. Processing consists of marshalling the file (streaming it from disk into a normalized message). The normalized message is sent over the Normalized Message Router ( NMR ) to the fileSender component per the specified "destinationService" which . The destinationService is specified in the servicmixservicemix.xml file in as an attribute the filePoller component. In this case example, the "destinationService" is the fileSender component. Finally, after it has been processed, the filePoller deletes the file from the source directory.

fileSender

This component is the "destinationService" for the filePoller. It receives normalized messages from filePoller. The messages it receives are the files that filePoller has transferred to it via the NMR. It converts the normalized message to its original file format and sends it to the destination directory, in this case the outbox directory. This component creates the filename to which to copy the file to by concatenating the string "sample_" with the process id following by ".xml". The concatenated string is passed to the org.servicemix.expression.JaxenStringXPathExpression bean as an argument to the constructor, as can be seen by the constructor-arg value tag in the XML file.

workManager

This bean is used by the filePoller to increase the throughput of the application. The workManager is a thread pool whose size can be adjusted declaratively in the servicemix.xml file. The other components in the File Binding application ask the workManager for threads as needed. For example, threads are used by this application to periodically (every second) check for files in the inbox. Other threads are used to do the work of processing files (streaming them in, normalizing them, and sending them to the NMR). Note: The fileSender component also uses a thread to do itits' s work, however, it is not using a thread from the workManager's thread pool.

Useful Code Hints

This section describes some interesting aspects of the start-up sequence and how the ServiceMix JBI container interacts with the File Binding application. The Java class files are located in the servicemix-1.0.1.jar file in the ServiceMix installation directory. To look at the Java source code, unjar and decompile the .class files or download the source code. Please note: the downloadable source code is slightly different than the compiled binary code.

Viewing the Java source code is recommended for understanding the information in this section.

filePoller Details

...

1. Servicemix

  1. The ServiceMix container reads the servicemix.xml file and sees it needs to

...

  1. instantiate a FilePoller.
  2. The container calls the init() method of

...

  1. FilePoller, as well as the init() methods of its parents.

...

  1. The container determines that

...

  1. FilePoller is an MBean and, therefore, calls

...

  1. the start

...

  1. () method of

...

  1. FilePoller, which it inherits from its parent PollingComponentSupport.

...

  1. The start() method will: (See: PollingComponentSupport.java code fragment below)
    A. Create a timerTask

...

  1. .
    B. Schedule the timerTask at a fixed rate. The start() method uses the "timer" (created in the init method) to schedule the timerTask

...

  1. at a fixed rate

...

  1. : timer.scheduleAtFixedRate(timerTask, firstTime, period)

...

  1. . Recall "period" is a property of FilePoller. It was assigned the value of 1000ms by dependency injection from the servicemix.xml file.
    C. timer will call the run() method of the timerTask periodically

...

  1. . timerTask's run method() is defined inline.

...

  1. This run() method will get the workManager object (see below for details).

...

  1. D. The workManager will call "scheduleWork(PollingComponentSupport.this)".

...

  1. Note: it passes in PollingComponent support, which by

...

  1. virtue of its inheritance hierarchy, is

...

  1. of type "Work", which is

...

  1. a "

...

  1. Runnable" object.

...

  1. E.

...

  1. "workManager.scheduleWork(Work)" will

...

  1. get a Thread, passing in a

...

  1. Runnable object, i.e. PollingComponentSupport, and call its

...

  1. run

...

  1. () method.

...

  1. F. PollingComponent's run() method calls poll(), which is implemented in

...

  1. FilePoller.

...

  1. G. From this point on the call sequence can be followed in

...

  1. FilePoller...
    H. The start() method of PollingComponentSupport, will eventually call super.start(), which propagates up to call the start() method of BaseLifeCycle, which sets the component state to "RUNNING

...

WORK MANAGER DETAILS

Work manager is used in two ways:

When the application is run, it has an inbox and outbox. Each instance of the application may move files between different source and destination directories, i.e. a different inbox and outbox as specified in its own servicemix.xml file.

Each The work manager will allocate a thread for each instance of the application.

...

  1. ."

Eventually, one of the threads that is polling (see step G) for a file in the inbox directory will see a one. It will use workManager's thread pool to get a thread for processing the file. Processing the file consists of streaming it from inbox, creating a normalized message, and sending the message to the NMR.

Code Block
titlePollingComponentSupport.java
borderStylesolid

  timerTask = new TimerTask() {
     public void run() {
          try {
                getWorkManager().scheduleWork(PollingComponentSupport.this);
          }
          catch (Throwable e) {
                log.error("Failed to schedule work: " + e, e);
          }
     }
  };
  if (firstTime != null) {
     timer.scheduleAtFixedRate(timerTask, firstTime, period);
  }
  else {
     timer.scheduleAtFixedRate(timerTask, delay, period);
  }
}
super.start();

workManager Details

workManager is a property of the FilePoller object. This property is defined by a local reference, the "ref" attribute in the servicemix.xml file. The local reference is a bean which instantiates org.activemq.work.SpringWorkManager.

The SpringWorkManager is a Spring bean. By default when a Spring bean starts, the properties are set, and then the afterPropertiesSet() method is called by the container.

The workManager is used to allocate threads. The FilePoller asks for threads from the workManager for two operations:

  1. The timerTask uses threads from the thread pool to periodically check the inbox directory for files.
  2. The workManager will also allocate a thread to process

...

  1. a file (read, normalize and send to NMR). The

...

  1. workManager calls a scheduleWork() method

...

  1. which is non-blocking. Therefore, if multiple files need to be

...

  1. processed,

...

  1. FilePoller can continue making

...

  1. requests to the

...

  1. workManager to schedule work.

1. The Timer object schedules a TimerTask

which was defined in the servicemix.xml and "given" to the filePoller.

Workmanager is a property of the filePoller object. This property is actually given a local reference to a bean, which is defined in the same xml file (the workmanager is an org.activemq.work.springworkmanager type

The spring workmanager is a spring bean, which by default, when a spring bean starts the properties are set, which are defined in the service.xml file. A method called "afterpropertiesset" is called by the containerSummarizing, when the ServiceMix container instantiates an MBean it firsts sets the property values if there are any, then calls the init() method of the class and its' parent classes, if applicable. Then it calls the start() method of the class. When a Spring bean starts up, the properties are set and then the afterPropertiesSet() method is called.

Related Documentation

For more information on the following topics please see: