Spring Integration- Chanels

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

PrintService.java

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());
}
System.out.println(message.getPayload());
}
}

MainApp.java

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;

@SpringBootApplication
@Configuration
@ImportResource("integration-context.xml")
public class SpringIntegrationApplication implements ApplicationRunner {

@Autowired
private DirectChannel channel;

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

@Override
public void run(ApplicationArguments arg0) {

// Subscribe to those message
channel.subscribe(new MessageHandler() {
@Override
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")
.build();
channel.send(message);
}
}

integration-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
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"/>

</beans>

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
PA

PA

Java Developer and enthusiast

More from Medium

Struts 2 with grid and forms

SPRING

JAVA /Super Keyword