Monday 7 June 2021

Spring integration: Custom router

You can define custom router by extending the class AbstractMessageRouter.

 

Example

@Component
public class CustomRouter extends AbstractMessageRouter {


	@Override
	protected Collection<MessageChannel> determineTargetChannels(Message<?> message) {
		......
		......
	}

}

Message is delivered to the channels returned from the method determineTargetChannels.

 

Find the below working application.

 

Step 1: Create new maven project ‘custom-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>custom-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 CustomRouter class.

 

CustomRouter.java

package com.sample.app.router;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.router.AbstractMessageRouter;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Component;

@Component
public class CustomRouter extends AbstractMessageRouter {

	@Autowired
	@Qualifier("secretChannel")
	private MessageChannel secretChannel;

	@Autowired
	@Qualifier("publicChannel")
	private MessageChannel publicChannel;

	@Autowired
	@Qualifier("defaultOutputChannel")
	private MessageChannel defaultOutputChannel;

	@Override
	protected Collection<MessageChannel> determineTargetChannels(Message<?> message) {

		Collection<MessageChannel> result = new ArrayList<>();

		String payload = message.getPayload().toString();

		if (payload.contains("secret")) {
			result.add(secretChannel);
		} else if (payload.contains("public")) {
			result.add(publicChannel);
		} else {
			result.add(defaultOutputChannel);
		}

		// TODO Auto-generated method stub
		return result;
	}

}


Step 4: Define 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")
public interface CustomGateway {

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

}


Step 5: Define endpoints.

 

ConsumerEndpoint.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 ConsumerEndpoint {

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

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

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


Step 6: 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.Router;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

import com.sample.app.gateway.CustomGateway;
import com.sample.app.router.CustomRouter;

@SpringBootApplication
@Configuration
public class App {
	@Router(inputChannel = "customRouterChannel")
	@Bean
	public CustomRouter router() {
		CustomRouter customRouter = new CustomRouter();
		return customRouter;
	}

	@Bean
	public MessageChannel secretChannel() {
		DirectChannel channel = new DirectChannel();
		return channel;
	}

	@Bean
	public MessageChannel publicChannel() {
		DirectChannel channel = new DirectChannel();
		return channel;
	}

	@Bean
	public MessageChannel defaultOutputChannel() {
		DirectChannel channel = new DirectChannel();
		return channel;
	}

	@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 < 10; i++) {
				Message<?> message = null;
				if (i % 2 == 0) {
					message = MessageBuilder.withPayload("secret message " + i).setHeader("messageType", "secret")
							.build();
				} else if (i % 3 == 0) {
					message = MessageBuilder.withPayload("public message " + i).setHeader("messageType", "public")
							.build();
				} else {
					message = MessageBuilder.withPayload("Generic message " + i).setHeader("messageType", "public")
							.build();
				}

				customGateway.print(message);
			}

		};
	}

}


Total project structure looks like below.




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

Received message from secretChannel : secret message 0
Received message from defaultOutputChannel : Generic message 1
Received message from secretChannel : secret message 2
Received message from publicChannel : public message 3
Received message from secretChannel : secret message 4
Received message from defaultOutputChannel : Generic message 5
Received message from secretChannel : secret message 6
Received message from defaultOutputChannel : Generic message 7
Received message from secretChannel : secret message 8
Received message from publicChannel : public message 9


You can download complete working application from below link.

 

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

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment