Friday 4 June 2021

Spring integration: PayloadTypeRouter: Route based on content type

PayloadTypeRouter resolves the message channel based on the message content type.

 

Example

@ServiceActivator(inputChannel = "payloadTypeRoutingChannel")
@Bean
public PayloadTypeRouter router() {
	PayloadTypeRouter router = new PayloadTypeRouter();
	router.setChannelMapping(String.class.getName(), "stringChannel");
	router.setChannelMapping(Integer.class.getName(), "integerChannel");
	return router;
}

 As per above definition, if the content type is string, then message is routed to stringChannel. If the content type is Integer, then message is routed to integerChannel.



Find the below working application.

 

Step 1: Create new maven project ‘payload-type-router-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>payload-type-router-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: Define custom gateway.

 

CustomGateway.java

package com.sample.app.gateway;

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

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

	@Gateway(requestChannel = "payloadTypeRoutingChannel")
	public void print(Message<?> message);

}
 

Step 4: Define endpoints.

 

ConsumerEndpoint1.java

 

package com.sample.app.endpoints;

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

@Component
public class ConsumerEndpoint1 {

	@ServiceActivator(inputChannel = "stringChannel")
	public void consumeStringMessage(Message<String> message) {
		System.out.println("ConsumerEndpoint1 -> Received message from stringChannel : " + message.getPayload());
	}

	@ServiceActivator(inputChannel = "integerChannel")
	public void consumeIntegerMessage(Message<String> message) {
		System.out.println("ConsumerEndpoint1 -> Received message from integerChannel : " + message.getPayload());
	}

}

 

Step 5: Define main application 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.annotation.ServiceActivator;
import org.springframework.integration.router.PayloadTypeRouter;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

import com.sample.app.gateway.CustomGateway;

@SpringBootApplication
@Configuration
public class App {
	@ServiceActivator(inputChannel = "payloadTypeRoutingChannel")
	@Bean
	public PayloadTypeRouter router() {
		PayloadTypeRouter router = new PayloadTypeRouter();
		router.setChannelMapping(String.class.getName(), "stringChannel");
		router.setChannelMapping(Integer.class.getName(), "integerChannel");
		return router;
	}

	@Autowired
	private CustomGateway customGateway;

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

	@Bean
	public CommandLineRunner demo() {
		return (args) -> {

			for (int i = 0; i < 5; i++) {
				Message<?> message = MessageBuilder.withPayload(i).build();
				customGateway.print(message);
			}

			for (int i = 0; i < 5; i++) {
				Message<?> message = MessageBuilder.withPayload("Secret message : " + i).build();
				customGateway.print(message);
			}

		};
	}

}

Total project structure looks like below.



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

 

ConsumerEndpoint1 -> Received message from integerChannel : 0
ConsumerEndpoint1 -> Received message from integerChannel : 1
ConsumerEndpoint1 -> Received message from integerChannel : 2
ConsumerEndpoint1 -> Received message from integerChannel : 3
ConsumerEndpoint1 -> Received message from integerChannel : 4

ConsumerEndpoint1 -> Received message from stringChannel : Secret message : 0
ConsumerEndpoint1 -> Received message from stringChannel : Secret message : 1
ConsumerEndpoint1 -> Received message from stringChannel : Secret message : 2
ConsumerEndpoint1 -> Received message from stringChannel : Secret message : 3
ConsumerEndpoint1 -> Received message from stringChannel : Secret message : 4

 

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/spring-integration/payload-type-router-demo

 

 

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment