Skip to Main Content U.S. Department of Energy

Splitter Sample

MifSplitter implements the Splitter integration pattern. A splitter is a object that receives a composite message and separates its parts into individual messages, which are sent separately.

Currently, MIF supports one type of builtin splitter called a ListSplitter. This object accepts a message containing a List, splits it up into individual elements, then returns each as a separate message. MIF does not currently support a user-defined splitter, but may do so in the future.

Sample Overview

In this sample, the user is prompted to enter a name (any string of words separated by spaces will work). The name is sent, via a stdio inbound endpoint to the splitterModule. The diagram below shows the inner workings of this module: it is received by a MIF processor that acts as a transformer (functionally similar to the Message Translater Pattern), splitting the full name into a list of individual names. This list is sent as a single message to the ListSplitter which splits each item in the list into a separate message, and sends each separately over the outbound endpoint. Lastly, a BridgeModule receives each individual string, and prints them to the console.

Diagram of Splitter Sample

Running the Sample

To run this sample, you need only to start the pipeline configured by the class: NameSplitterDriver. So, to run it on the command line, enter:

bin/ -c gov.pnnl.mif.samples.splitter.NameSplitterDriver

This will start MIF, which eventually prints the console message:

2010-05-28 13:48:09,775 INFO  [MifManagerImpl]  -- MIF has started --

Then enter a name, when prompted (any string of words separated by spaces will work):

  enter name: John Doe

Since the bridge module is set to verbose mode, it prints every message it receives, so you will see the following output on the MIF console:

2010-05-28 13:49:38,595 INFO  [BridgeModuleDelegate] received: Doe
2010-05-28 13:49:38,597 INFO  [BridgeModuleDelegate] received: John

This shows that the single string we sent into the MIF pipeline was split into separate strings, which were received separately by the BridgeModule.

Code Walkthrough

Implementing the StringToListTransformer

In this pipeline, we create a MifModule, whose processor (StringToListTransformer) transforms the full name into a list of individual names. So, the implementation of this processor is simply:

	public Object listen(Object input) {
		String nameString = (String) input;
		String[] arr  = nameString.split(" ");
		return Arrays.asList(arr);

Constructing the Pipeline

The plipeline is configured by creating a MifPipeline as usual. Then we add the SplitterModule as follows:

MifModule splitterModule = pipeline.addMifModule(StringToListTransformer.class, "stdio://in?promptMessage=enter name: ", "vm://bridge");

Then the ListSplitter is created, and set on the splitterModule

MifSplitter listSplitter = pipeline.addMifListSplitter();

Finally, we add the BridgeModule at set it to verbose mode, which causes it to print to the console every message it receives:

pipeline.addBridgeModule("vm://bridge", null).setVerbose(true);
splitter_sample.txt · Last modified: 2010/05/28 15:38 by adamw