Monday, 17 May 2021

Spring integration: MessageBuilder example

Spring integration provides MessageBuilder class to create an immutable GenericMessages.

 

Example

Message<String> message = MessageBuilder.withPayload(payload).setHeader("my-content-type", "application/json")

.setHeader("my-origin", "localhost").build();

 

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project ‘message-builder-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>message-builder-demo</artifactId>
	<version>1</version>

	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
	</parent>

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

	</dependencies>

</project>

 

Step 3: Create new package 'com.sample.app.endpoints' and define CustomGateway and PrintService classes.

 

CustomGateway.java

 

package com.sample.app.endpoints;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.Message;

@MessagingGateway(name = "myGateway", defaultRequestChannel = "inputChannel")
public interface CustomGateway {

	@Gateway(requestChannel = "echoChannel", replyTimeout = 2, requestTimeout = 200)
	public void print(Message<String> message);


}

 

PrintService.java

package com.sample.app.endpoints;

import java.util.Map;
import java.util.Set;

import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

@Component
public class PrintService {

	@ServiceActivator(inputChannel = "echoChannel")
	public void print(Message<String> message) {
		String payload = message.getPayload();
		MessageHeaders messageHeaders = message.getHeaders();

		System.out.println("Headers");
		Set<Map.Entry<String, Object>> messageHeadersSet = messageHeaders.entrySet();

		for (Map.Entry<String, Object> headerEntry : messageHeadersSet) {
			System.out.println(headerEntry.getKey() + " -> " + headerEntry.getValue());
		}

		System.out.println("\nPayload");
		System.out.println(payload);
	}

}

 

Step 4: Define App class.

 

App.java

 

package com.sample.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

import com.sample.app.endpoints.CustomGateway;

@SpringBootApplication
@Configuration
public class App {

	@Autowired
	private CustomGateway gateway;

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

	@Bean
	public CommandLineRunner demo() {
		return (args) -> {
			String json = "{\"name\" : \"Krishna\"}";

			Message<String> message = MessageBuilder.withPayload(json).setHeader("my-content-type", "application/json")
					.setHeader("my-origin", "localhost").build();

			gateway.print(message);
		};
	}

}

 

Total project structure looks like below.

 


Run App.java, you will see below messages in console.

Headers
replyChannel -> nullChannel
my-content-type -> application/json
id -> bb4ab9d3-3709-0296-892d-28a64130b7cf
my-origin -> localhost
timestamp -> 1617350233133

Payload
{"name" : "Krishna"}


You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/spring-integration/message-builder-demo

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment