Filters are used to accept certain messages based on criteria you define. Messages which do not meet this criteria are discarded. This is a good way to reduce the number of messages passing through the pipeline or to route certain types of messages in one direction and other types in another.

To use a filter in MIF, you extend the class AbstractMifFilter and implement a single method to determine if the incoming message should be passed on:

public abstract boolean shouldAccept(Object obj);

When implementing this method, if the object received should be passed on to the next step in the pipeline, return true, otherwise return false. The resulting filter object is attached to an endpoint on a module, so when a message passes through the endpoint, MIF calls shouldAccept. If this method returns true, the message is passed on to the module, if not it is discarded.

The following code shows how the duplicate-removing filter DupeFilter was implemented. First, the abstract filter class is extended. We create a field called prev to keep track of the previous object we received.

Then we implement the the abstract method. The first thing we do is make sure we are receiving the expected type which in this case is a byte array. If the type is not expected, we cannot accept the message.

To use the filter in a pipeline, we instantiate it, add it to a MIF pipeline in the configure() method of a component, and attach it to the inbound endpoint of the module we wish to spare the trouble of receiving duplicates.

Here is an excerpt from configure() where we are pulling out our inbound endpoint from the endpoint mapping.

MifEndpoint catIn = endpoints.get("bytes-in");

Then we instantiate the filter, add it to the pipeline and set it on the endpoint.