You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 49 Next »

Overview of the ServiceMix 1.x File Binding Example

This document describes how to run the 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 of declarative programming
  • how to interact with the file system

The source 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 source code while reading this document.

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 to the ServiceMix bus, which routes the file via Normalized Message Router (NMR) to another component which deposits it into an outbox directory.

Note

The file to be transmitted must be in XML format.

Running the File Binding Example

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

cd [servicemix_install_dir]\examples\file-binding

where servicemix_install_dir is the directory in which ServiceMix was installed.

Before running the example, copy a test file into the inbox directory:

copy test-file.xml inbox

Then type:

[servicemix_install_dir]\bin\servicemix servicemix.xml

OR

..\..\bin\servicemix servicemix.xml


After a few seconds, the sample_xxx.xml file will appear in the outbox directory. To see this open another command window and do a directory listing on the outbox directory, for example:

dir [servicemix_install_dir]\examples\file-binding\outbox

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.

How it Works

The diagram below illustrates the logical flow of the program through the file binding components.



File Binding Logical Flow Diagram



The logical flow of the program is:

  1. The filePoller polls the inbox directory every 1000 ms looking for a file.
  2. Once a file appears in the inbox directory, the filePoller gets a thread from the workManager. The thread will be used to process the file.
  3. The filePoller creates a normalized message that contains the file to be transmitted. It routes the normalized message over the Normalized Message Router (NMR) to the fileSender component.
  4. The fileSender transforms the normalized message back into a file and "sends" it (places it) to the outbox directory.

Logging information is written to the console as files are transmitted. Typical output looks like the following:

 
 
ServiceMix ESB: 1.0.1

Loading ServiceMix from file: servicemix.xml
[INFO] XmlBeanDefinitionReader - -Loading XML bean definitions from file [C:\Program Files\servicemix-1.0.1\examples\file-binding\servicemi.xml]
[INFO] FileSystemXmlApplicationContext - -Bean factory for application context [org.springframework.context.support.FileSystemXmlApplication
Context;hashCode=7486844]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [workManager,jbi]; root of BeanFactory hierarchy
[INFO] FileSystemXmlApplicationContext - -2 beans defined in application context 
[org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=7486844]
[INFO] CollectionFactory - -JDK 1.4+ collections available
[INFO] CollectionFactory - -Commons Collections 3.x available
[INFO] FileSystemXmlApplicationContext - -Unable to locate MessageSource with name 'messageSource': using default [org.springframework.conte
xt.support.DelegatingMessageSource@1d6776d]
[INFO] FileSystemXmlApplicationContext - -Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using defaul
t [org.springframework.context.event.SimpleApplicationEventMulticaster@4fce71]
[INFO] DefaultListableBeanFactory - -Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanF
actory defining beans [workManager,jbi]; root of BeanFactory hierarchy]
[INFO] DefaultListableBeanFactory - -Creating shared instance of singleton bean 'workManager'
[INFO] DefaultListableBeanFactory - -Creating shared instance of singleton bean 'jbi'
Created MBeanServer with ID: 203c31:106bd250a5b:-7fff:Lisas:1
[INFO] SpringInitialContextFactory - -Loading JNDI context from: class path resource [jndi.xml]
[INFO] XmlBeanDefinitionReader - -Loading XML bean definitions from class path resource [jndi.xml]
[INFO] XmlBeanFactory - -Creating shared instance of singleton bean 'jndi'
RMIConnectorServer started at: service:jmx:rmi://lisas/jndi/rmi://localhost:1099/defaultJBIJMX
[INFO] JBIContainer - -ServiceMix JBI Container (http://servicemix.org/) name: defaultJBI running version: ServiceMix.
[INFO] JBIContainer - -Activating component for: [container=defaultJBI,name=fileSender,id=fileSender] with service: fileSender component: org.servicemix.components.file.FileWriter@b1cc87
[INFO] ComponentContextImpl - -Component: fileSender activated endpoint: fileSender : fileSender
[INFO] JBIContainer - -Activating component for: [container=defaultJBI,name=filePoller,id=filePoller] with service: filePoller component: or
g.servicemix.components.file.FilePoller@183e7de
[INFO] ComponentContextImpl - -Component: filePoller activated endpoint: filePoller : filePoller
[INFO] DeliveryChannel - -default destination serviceName for filePoller = fileSender

Note: In the servicemix.xml file, the "destinationService" attribute of the filePoller component is "foo:fileSender." The last line of output (above) shows the NMR using that to deliver the normalized message to fileSender.

Details

The following table provides more details about the function of each component and bean in the servicemix.xml file.

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 is specified in the servicmix.xml file in the filePoller component. In this case 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 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. Therefore, the source file name from the inbox directory is not the destination file name in the outbox directory.

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. Threads are used by this application to do the work of processing files.

Related Documentation

For more information on the following topics please see:

  • No labels