Spring Integration — Message Template

In this example, we’ll make the use of MessagingTemplate to send and receive the message.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.oreilly.integration</groupId>
<artifactId>spring-integration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-integration</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

PrintService.java

package com.oreilly.integration;

import java.util.Map.Entry;

import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

public class PrintService {

public Message<?> 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());
return MessageBuilder.withPayload("New Message").build();
}
}

MainApp.java

package com.oreilly.integration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.integration.core.MessagingTemplate;
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
@Qualifier("inputChannel")
private DirectChannel inputChannel;

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

@Override
public void run(ApplicationArguments arg0) {
Message<String> message = MessageBuilder
.withPayload("Hello World, from the builder pattern")
.setHeader("newHeader", "newHeaderValue")
.build();

MessagingTemplate template = new MessagingTemplate();
Message returnMessage = template.sendAndReceive(inputChannel, message);
System.out.println(returnMessage.getPayload());
}
}

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="inputChannel"/>

<bean class="com.oreilly.integration.PrintService" id="printService"/>

<int:service-activator ref="printService" method="print"
input-channel="inputChannel" />
</beans>

Output —

Key >> newHeader:: Value >> newHeaderValue
Key >> replyChannel:: Value >> org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@61d9efe0
Key >> errorChannel:: Value >> org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@61d9efe0
Key >> id:: Value >> 6a07ba77–8043–33e6–1b07–1b29c62851c1
Key >> timestamp:: Value >> 1639327178688
Hello World, from the builder pattern
New Message

--

--

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