Skip to content

Custom Adapters

PhilZeppe edited this page Dec 10, 2014 · 1 revision

This page describes how the system can be extended with custom adapters. The purpose of the adapters is - in case of an OutputAdapter - to transform the internal messages to the technology-related format and - in case of an Input Adapter - from the technology-related format to the internal message format.

Custom Output Adapters

In order to create an Output Adapter that can send messages to the peers, the adapter has to implement the OutputAdapter interface. Note that the name of the adapter has to be unique in order to be able to resolve the address of peers and the preferred communication adapter. Stateful adapters are created per peer, meaning that every peer is associated with a separate adapter instance. Both parameters have to be added using the @Adapter annotation, which is required for every Output Adapter. Stateful adapters are created per peer, which means that much more resources are consumed, adapters should only be stateful if they are required to maintain a conversation state.

The following listing depicts the implementation of a Stateless Output Adapter:

@Adapter(name="StatelessAdapterId", stateful = false)
public class CustomStatelessAdapter implements OutputAdapter {

    public CustomStatelessAdapter() {
        //initialize the adapter
    }

    @Override
    public void push(Message message, PeerChannelAddress address) throws AdapterException {
        //send message to address
    }
}

The following listing depicts the implementation of a Stateful Output Adapter:

@Adapter(name="StatefulAdapterId", stateful = true)
public class CustomStatefulAdapter implements OutputAdapter {

    //conversational state

    public CustomStatefulAdapter() {
        //initialize the adapter
    }

    public CustomStatefulAdapter(PeerChannelAddress address) {
        //initialize the adapter with the given address
    }

    @Override
    public void push(Message message, PeerChannelAddress address) throws AdapterException {
        //send message to address
    }
}

Note that stateless adapters are required to have a default constructor (0 parameters), whereas stateful adapters are allowed to have a default constructor as well as a constructor with one parameter of type PeerChannelAddress.

Currently available Output Adapters:

Custom Input Adapters

There are two different types of Input Adapters available: Input Pull Adapters actively issue a pull in a certain time interval, whereas Input Push Adapters wait for push notifications over communication channels.Both adapter types have to be provided with the concrete parameters (e.g.., the url of the FTP server and credentials). This information can either be hard-coded or provided during the creation of the adapter by an application

The following Listing depicts how to implement a Input Pull Adapters that pulls data from an FTP server:

private class FTPAdapter implements InputPullAdapter {

    private FTPAdapter(String address, Credentials credentials) {
        //initialize adapter
    }

    @Override
    public Message pull() throws AdapterException {
        //perform pull and transform to message
        return msg;
    }
}

The following Listing depicts how to implement a Input Push Adapters that receives notifications from a mailing list:

private class MailinglistAdapter extends InputPushAdapterImpl {
    private MailinglistAdapter(...) {
        //initialize adapter
    }

    @Override
    public void init() {
        //initialize some handler

        //alternatively do the following:
        schedule(new PushTask() {
            @Override
            public void run() {
                //wait for message and transform to internal message
                publishMessage(msg);
            }
        });
    }

    @Override
    public void cleanUp() {
        //destroy resources
    }
}

Note that pull adapters have to implement an interface, whereas push adapters have to extend a class that provides some methods to publish a message or to register a task (can be used to save resources).

Currently available Input Adapters: