martes, abril 29, 2008

At last entry i wrote about developing a Taget Adapter for SpringIntegration Module. In thi sentry i will talk about developing a SourceAdapter.

As my last entry, Jabber server will be used, and Smack API for connecting and receiving messages.

From previous entry server configuration, and some classes will be used, so strictly, only SourceAdapter and its configuration are going to be explain.

Firstly, but, a little explanation about two possible kind of communication for source adapters:

Poll: which means that every period of time the program are checking if new content is available. For example FTP Source Adapter is an example because ftp server doesn't throw an interruption when another user upload a file.

Driven-Event: which means that an asynchronous event is thrown and can be captured. Our IM Adapter is an example.

Each SourceAdapter must implements SourceAdapter interface. But as TargetAdapter, there are some classes that help developers in writting an Adapter. At least two classes are provided for developing a SourceAdapter that, of course, implements SourceAdapter interface:

AbstractSourceAdapter: A base class providing common behavior for source adapters. It has some methods for sending messages to a channel, set message mappers, ...

PollingSourceAdapter: A base class for polling if new message is available to be sent. It has some methods like period between polls, initial delay, ...

Our case requires extending AbstractSourceAdapter.

IMSourceAdapter extends AbstractSourceAdapter implements

InitializingBean

IMMessage is a class that represents a message sent through channel. IMMessage is a self developed class and its explanation can be found on previous post.

Because of event communication, Smack API requires that you subscribe to an event implementing an interface, so each time an event occurs, that interface is executed. This initialization is developed in initialize() Spring method and next piece of code is the business logic.

In this entry, but is supposed that some basic concepts about Spring Integration are known. What we are going to explain is the development of a new adapter.

But what is an Adapter? In Spring Integration, an adapter is a component that interacts with external systems (servers, other components ...). As its name suggests, an adapter adapts the input of an external component and it transforms to something that can be sent through spring-integration pipe, and also adapts what is received from the pipe for sending to external component.

That pipe is MessageChannel class. Spring Integration is built with different adapters, JMS, RMI, Files, Mail, ... That's good while you are working with systems that required adapters are already implemented, but, what's happens if your application requires communication with an external system which an adapter has not already developed? The solution is easy, implement yourself the adapter.

That's what I am going to explain, how to implements a TargetAdapter (adapter for output messaging). In next entry would be explain how to implement a SourceAdapter (adapter for incoming messages).

Only two classes are really important org.springframework.integration.handler.MessageHandler that is used for handle the messages, and org.springframework.integration.message.AbstractMessageMapper that transform:

From specific input to Spring Integration Message.

From Spring Integration Message to specific output.

In this example, integration to instant messaging system will be used. The basic idea is develop a system that sent a message from Spring Integration to a XMPP server (a.k.a Jabber) user account.

Full schema of application is:

For running, apart from Spring/Spring Integration jars, JDK ..., two programs and an API are required:

(2) Each component that is connected into MessageChannel must be a MessageEndpoint. DefaultMessageEndpoint is a helper class for this porpoise. Because TargetAdapter is not an Endpoint, it must be wrapped into one.

(3) Handler method is method that will be invoked by MessageEndpoint when a message is received. In this case, a MessageHandler (IMTargetAdapter) is provided, with required information (Facade for communicating with Smack API, and the receiver of messages (incidence@).

(4) MessageEndpoint must be subscribed into a channel.

Using Namehandler is out of scope of this document; of course it would be cleaner using Ext. XML authoring.

And that's all; you can download source code for trying, but remember to change receiver value.