Skip to Main Content U.S. Department of Energy

Hello Component Sample

In the Hello World Sample we showed how to create a basic MIF pipeline. In this document we will expand on that example to demonstrate MIF Components and JMS endpoints.

MIF Components

Definition

In MIF, a component encapsulates a number of modules which are working together to perform a given task. So, a component is actually a kind of sub-pipeline where the details of the component's inner workings are hidden. As such, the component is the unit of re-usability which the user interacts with. Once a set of components has been created, the end user can arrange, or orchestrate, the components to form a distributed application.

Adding a component to helloWorld

The helloComponent sample modifies the helloWorld sample by wrapping the two modules with a component. This example also uses JMS to communicate with the component in order to demonstrate using different endpoint types. The sample code for this example is included in MIF_HOME/samples.

A JMS publisher program is used to publish a string to a JMS topic which the HelloComponent is listening on. The HelloComponent uses two modules to build up the string as in the Hello World Example. The HelloComponent sends the final string to a JMS topic where a listener program is waiting to receive the string.

Diagram of HelloComponent Sample

The helloComponent example

This example requires you to start three processes in addition to the JMS server: A JMS publisher, a JMS listener, and the MIF pipeline containing the HelloComponent. The code for this sample can be found in MIF_HOME/sources/mif-samples in the package: gov.pnnl.mif.samples.hello.component

Prerequisites

  • See the sample code prerequisites on the MIF samples page

Running the example from Eclipse

Following these instructions will cause 3 consoles to be spawned within Eclipse. Notice that you can switch between running consoles by clicking the picture of the monitor in the console toolbar.

  1. In the package explorer, navigate to the package: gov.pnnl.mif.samples.hello.component
  2. Right click on HelloComponentDriver.java and choose: “Run As –> Java Application”. This starts the MIF pipeline.
  3. Run Listener.java in the same way as above
  4. Run Publisher.java

Running the example from the command line

These instructions explain how to run the sample from the command line.

  • Open three console windows and navigate to MIF_HOME in each.
  • In the first window, start the MIF example by running
bin/mif.sh -c gov.pnnl.mif.samples.hello.component.HelloComponentDriver
  • In the second shell window, start the JMS listener
bin/mif.sh -c gov.pnnl.mif.samples.hello.component.Listener
  • In the third window, run the JMS publisher
bin/mif.sh -c gov.pnnl.mif.samples.hello.component.Publisher

Program Output

The publisher will send the string “Dave” into HelloComponent. HelloComponent modifies the string twice (once per module) and sends it to the listener. So, after running the publisher you should see the following in the MIF window as it logs what it is doing:

2010-05-20 10:31:07,457 INFO  [MifManagerImpl] storing PID: /tmp/mif.pid
2010-05-20 10:31:07,464 INFO  [MifManagerImpl]  -- MIF has started --
HelloNameProcessor: Hey, Dave
HelloHalProcessor: Hey, Dave, what are you doing?

The listener should have received the final string and printed something similar to the following:

2010-05-20 10:35:33,062 INFO  [RunMif] Running on command line: RunMif -c gov.pnnl.mif.samples.hello.component.Listener 
Connecting to URL: failover://tcp://localhost:61616
Consuming topic: HalTopic
Using a non-durable subscription
2010-05-20 10:35:33,399 INFO  [FailoverTransport] Successfully connected to tcp://localhost:61616
listening
Received text: Hey, Dave, what are you doing?

Code Walkthrough

Building a pipeline using a pre-existing component

Let's take a look at the code needed to create a MIF pipeline using components.

First we create a pipeline object as in the hello world example.

MifPipeline pipeline = new MifPipeline();

Next, add a JMS connector, which allows us to use JMS endpoints from anywhere in our pipeline. The JMS connector allows us to configure the URL and other properties of the JMS connection which endpoints need in order to send messages to the JMS server. We give the connector the address of our server in the first argument. The second argument says that we are using ActiveMQ, so give us default settings appropriate for that provider.

pipeline.addMifJmsConnector("tcp://localhost:61616", JmsProvider.ACTIVEMQ);

Each endpoint type has its own connector, but most connectors are created automatically with a default configuration when you create an endpoint of a given type. So you usually don't need to create a connector unless you need special configuration. The exception to this is a JmsConnector which needs to be explicitly created so that the user can specify the URL where the JMS server is listening. For a detailed description of connectors see the page on connectors and endpoints

Next, create a new HelloWorldComponent and add it to the pipeline. The configuration of this component should be taken care of inside its respective class.

HelloWorldComponent hello = new HelloWorldComponent();
pipeline.addMifComponent(hello);

After the component has been created and added, set the inbound and outbound endpoints by specifying the type (JMS) and topic names.

hello.setInNameEndp("jms://topic:NameTopic")
hello.setOutHalEndp("jms://topic:HalTopic")

Finally, start the pipeline to begin receiving messages.

pipeline.start();

Creating the HelloWorldComponent

In the previous section we illustrated how to build a pipeline using an existing component. In this section we show how to create a MIF component. Basically, the modules used in the hello world example are re-used along with their implementation classes.

A component object is created by extending the java class AbstractMifComponent and implementing the configure() method. This method is responsible for creating and connecting all the modules required to implement the component and is called by MIF when the component is added to a pipeline. The following code snippet illustrates a fully functional MifComponent.

public class HelloWorldComponent extends AbstractMifComponent {
  
  public void configure(MifPipeline pipeline) throws MifException {
    pipeline.addMifModule(HelloNameProcessor.class.getName(), inNameEndp, "vm://hal.queue");
    pipeline.addMifModule(HelloHalProcessor.class.getName(), "vm://hal.queue", outHalEndp);
  }
   
  public void setInNameEndp(String inNameEndp) {
    this.inNameEndp = inNameEndp;
  }

  public void setOutHalEndp(String outHalEndp) {
    this.outHalEndp = outHalEndp;
  }
}

Summary

This document has shown how to use components to create MIF applications. Components consist of a sub-pipeline of modules which work together to perform a task.

 
hello_component_sample.txt · Last modified: 2010/05/20 11:12 by adamw