Skip to Main Content U.S. Department of Energy

Implementing a MifModule

A MIF Module represents the basic unit of work in a MIF pipeline. Every module has an implementation class, known as a MIF Processor. The processor class is specified as the first argument to the addMifModule factory method when you create your module:

MifModule myModule = pipeline.addMifModule(MyMifProcessor.class, "vm://in.endpoint", "vm://out.endpoint");

Each processor class must implement a Processor interface and an associated listen method which accepts an object representing the payload received on the module's inbound endpoint. The listen method also returns an object representing the payload which is sent via the module's outbound endpoint.

public class MyMifProcessor implements MifObjectProcessor {
 
    public Object listen(Object input) {
       // perform some kind of processing on input, which
       // results in a new Object, output
       return output;
    }
 
}

Processor Interfaces

There are a few different types of processor interfaces depending on whether you want to enforce the use of serialized objects and whether the processor needs to explicitly handle message properties, which are sent as a header on all messages that pass through a MIF pipeline. These interfaces are found in the package gov.pnnl.mif.user.

MifProcessor

Use the MifProcessor interface to implement your module if you want to enforce that the types sent and received by your module are Serializable.

public interface MifProcessor {
  public Serializable listen(Serializable input);
}

MifObjectProcessor

This is the most general type of interface, allowing any type of object to be received by the listen method. It is often desirable to check the type of object received (with the instanceof operator) to ensure that it matches the correct derived type.

public interface MifObjectProcessor {
    public Object listen(Object input);
}

MifMessageProcessor

This type of processor is used when it is necessary to have access to the message properties associated with a given message. Normally, a processor receives just the message payload, but this interface allows the module to receive both.

public interface MifMessageProcessor {
    public Object listen(Object input, MessageProperties messageProperties);
}

Module Properties

Since the processor class for a given MifModule is instantiated by the underlying service container, It is not possible to manually create and configure a processor class before adding it into a pipeline. Therefore, module properties are provided by the API to enable the user to pass any processor properties to MIF. MIF will then populate the properties on the processor when it is instantiated. The properties are set on the processor assuming that it is a Java Bean, meaning that it has a no-argument constructor and has standard setter and getter methods.

For example, the following is a MifProcessor with JavaBean-style setters:

public class Apple implements MifObjectProcessor {
 
    private String color;
    private String type;
 
    public Object listen(Object input) {
       // do stuff
       return output;
    }
    /* The apple's color */
    public setColor(String color) {
        this.color = color;
    }
    /* The type/variety of apple */
    public setType(String type) {
        this.type = type;
    }
}

Properties on a MIF module that is implemented by the above MIF processor can then be set with the following code:

MifModule appleModule = pipeline.addMifModule(Apple.class, "vm://in.endpoint", "vm://out.endpoint");
appleModule.setProperty("color", "red");
appleModule.setProperty("type", "Honeycrisp");
 
implementing_a_mifmodule.txt · Last modified: 2010/06/08 16:01 by adamw