Monday 24 May 2021

Spring integration: Exploring channels

Channels are used to pass messages between endpoints.

 

Spring integration framework is built on lightweight message-driven architecture, where messages are exchanged using channels. In Spring integration framework, endpoints perform the operation and channels perform the routing.

 

Different types of channels

Depending on the needs of our application, we may need to send message to single endpoint (point-to-point communication), or we may need to send the message to multiple endpoints.

 

There are two types of channels supported in spring integration framework.

a.   PollableChannel

b.   SubscribableChannel

 

PollableChannel

PollableChannel buffer the messages and wait the consumer to get the message. In general, whatever the messages produced by producer are stored in channel buffer and these are delivered when consumer poll the channel.

public interface PollableChannel extends MessageChannel {

	@Nullable
	Message<?> receive();

	@Nullable
	Message<?> receive(long timeout);

}

 

Endpoints/consumers poll the messages from the pollable channel. If the message is not available in the channel, then the call blocks indefinitely (if you use receive method). You can specify the maximum blocking time using receive(long timeout) method.

 

PollableChannel implementations

a.   QueueChannel

b.   PriorityQueueChannel

 

QueueChannel

Queue channel implements the PollableChannel interface. In QueueChannel, message is placed in a blocking queue (capacity of the blocking queue may be specified upon construction).

 

PriorityChannel

PriorityChannel extends QueueChannel, in PriorityChannel messages are prioritized based on a comparator.

 

Subscribable channel

Multiple consumers are subscribed to this channel. Whenever a message is arrived, it is delivered to all the subscribers. It do not buffer the messages.

public interface SubscribableChannel extends MessageChannel {

	boolean subscribe(MessageHandler handler);

	boolean unsubscribe(MessageHandler handler);

}

 

An endpoint can subscribe to a subscribable channel using ‘subscribe’ method and unsubscribe to it using unsubscribe method.

 

Subscribable channel implementaitons

a.   DirectChannel (1 producer and 1 consumer)

b.   PublishSubscribeChannel(messages send to multiple consumers)

 

DirectChannel

DirectChannel invokes a single subscriber for each sent Message. The invocation will occur in the sender's thread.

 

PublishSubscribeChannel

PublishSubscribeChannel send messages to all of its subscribers.

 

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment