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

...

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 NMR to the fileSender component per the specified "destinationService". The destinationService is specified in the servicemix.xml file as an attribute the filePoller component. In this 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, 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 its' 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 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 necessary recommended for understanding the information in this section below.

filePoller Details

  1. The ServiceMix container reads the servicemix.xml file and sees it needs to instantiate a FilePoller.
  2. The container calls the init() method of FilePoller, as well as the init() methods of its parents.
  3. The container determines that FilePoller is an MBean and, therefore, calls the start() method of FilePoller, which it inherits from its parent PollingComponentSupport.
  4. The start() method will: (See:
    a PollingComponentSupport.java code fragment below)
    A. Create a timerTask.
    b B. Schedule the the timerTask at a fixed rate. The start() method uses the "timer" (created in the init method) to schedule the timerTask at a fixed rate: timer.scheduleAtFixedRate(timerTask, firstTime, period). Recall "period" is a property of FilePoller. It was assigned the value of 1000ms by dependency injection from the servicemix.xml file.
    c C. timer will call the run() method of the timerTask periodically. timerTask's run method() is defined inline. It This run() method will get the workManager object (see below for details).
    d D. The workManager will call "scheduleWork(PollingComponentSupport.this)". Note: it passes in PollingComponent support, which by virtue of its inheritance hierarchy, is of type "Work", which is a "Runnable" object."
    e E. "workManager.scheduleWork(Work)" will likely create or get a Thread, passing in a Runnable object, i.e. PollingComponentSupport, and call its run() method.
    f F. PollingComponent's run() method calls poll(), which is implemented in FilePoller.
    g G. From this point on the call sequence can be followed in FilePoller...
    h H. The start() method of PollingComponentSupport, will eventually calls call super.start(), which propagates up to call the start() method of BaseLifeCycle, which sets the component state to "RUNNING."

Eventually, one of the threads that is polling (see step g. aboveG) for a file in the inbox directory will see a file in the inbox directoryone. It will use workManager's thread pool to get a thread for processing the file. Processing the file consists of streaming it in 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.

...

  1. The timerTask uses threads from the thread pool to periodically check the inbox directoroy directory for files.
  2. The workManager will also allocate a thread to process a file (read, normalize and send to NMR). The workManager calls a scheduleWork() method which is non-blocking. Therefore, if multiple files need to be processed, ilePoller FilePoller can continue making requests to the workManager to schedule work.

Summarizing, 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: