Spring Integration- Chanels

In this example, we’ll use the DirectChannel to subscribe to those channels.


import java.util.Map.Entry;

import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

public class PrintService {

public void print(Message<String> message){
MessageHeaders headers = message.getHeaders();
for(Entry<String,Object> entry:headers.entrySet()){
System.out.println("Key >> "+ entry.getKey()+":: Value >> "+ entry.getValue());


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.MessageBuilder;

public class SpringIntegrationApplication implements ApplicationRunner {

private DirectChannel channel;

public static void main(String[] args) {
SpringApplication.run(SpringIntegrationApplication.class, args);

public void run(ApplicationArguments arg0) {

// Subscribe to those message
channel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println("Message subscribed are -- ");
new PrintService().print((Message<String>) message);

// Send the message!
Message<String> message = MessageBuilder
.withPayload("Hello World, from the builder pattern")
.setHeader("newHeader", "newHeaderValue")


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.2.xsd">

<int:channel id="messageChannel"/>


Output —

Message subscribed are —
Key >> newHeader:: Value >> newHeaderValue
Key >> id:: Value >> 491e23d3–7fd6-ac6e-23b7–58cf4710e32a
Key >> timestamp:: Value >> 1639325869520
Hello World, from the builder pattern




Java Developer and enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

5 Reasons to Join Upcoming Webinar

Will the Advent of AR/VR Dismantle the Office Building?

Desired Capabilities in Appium

MVI Libraries, ViewModel for Jetpack Compose Screens, Camera2 and more!

How to Receive a Phone Call in PHP with Laravel and Plivo

How to Make Jira Easier to Use with Integrated Visual UI Bug Tracking

Simplifying Subjects in RxDart

An image depicting subjects in RxDart

WVB Operating System(Third Stage)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Java Developer and enthusiast

More from Medium

Struts 2 with grid and forms


JAVA /Super Keyword