Tuesday, 18 May 2021

Spring integration: Exploring MessageChannel

In Spring integration, message channels are used to transfer message from one endpoint to other endpoint. Producers send message to a channel and consumers receive messages from a channel.

 

By combining multiple endpoints and channels we can form a complex workflow.

In spring integration, MessageChannel interface is defined like below.

@FunctionalInterface
public interface MessageChannel {
	long INDEFINITE_TIMEOUT = -1;
	
	default boolean send(Message<?> message) {
		return send(message, INDEFINITE_TIMEOUT);
	}

	boolean send(Message<?> message, long timeout);

}

 

‘send’ method return true if the message sent successfully, else false.

 

Buffering messages

Spring integration provides a ‘PollableChannel’ interface to buffer the messages.

 

public interface PollableChannel extends MessageChannel {
	@Nullable
	Message<?> receive();

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

 

Return value of ‘receive’ method is null in the case of a timeout or interrupt.

 

SubscribableChannel interface

Spring integration provides ‘SubscribableChannel’ interface to support pub-sub model. SubscribableChannel maintains a registry of subscribers and invokes them to handle messages sent through this channel.

public interface SubscribableChannel extends MessageChannel {

	boolean subscribe(MessageHandler handler);

	boolean unsubscribe(MessageHandler handler);

}

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment