Saturday, 19 June 2021

Spring integration: Gateways

Gateways are used to send and receive messages.

 

Why gateway?

A gateway hides the messaging API provided by Spring Integration. It lets your application’s business logic be unaware of the Spring Integration API.

 

How to define a gateway?

You need to create an interface that can be used to interact with Spring Integration.

 

Example

@MessagingGateway
public interface CustomGateway {

	@Gateway(requestChannel = "myUpperCaseChannel")
	public String sendToUppercaseChannel(String message);

}

 

How to call sendToUppercaseChannel method?

Define ‘CustomGateway’ bean in any spring managed components and call  sendToUppercaseChannel method.

@Autowired
private CustomGateway customGateway;

customGateway.sendToUppercaseChannel("Hello World");

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

 

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

 

CustomGateway.java

package com.sample.app.gateway;

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

@MessagingGateway
public interface CustomGateway {

	@Gateway(requestChannel = "myUpperCaseChannel")
	public void sendToUppercaseChannel(String message);

}


Step 4: Define consumer endpoints

 

ConsumerEndpoint.java

package com.sample.app.endpoints;

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

@Component
public class ConsumerEndpoint {

	@ServiceActivator(inputChannel = "printChannel")
	public void consumeStringMessage(String message) {
		System.out.println("Received message from printChannel : " + message);
	}

	@ServiceActivator(inputChannel = "lengthCalcChannel", outputChannel = "printChannel")
	public String lenghtCalc(String str) {
		System.out.println("Received message from lengthCalcChannel : " + str);

		return "'" + str + "' has length " + str.length();
	}

	@ServiceActivator(inputChannel = "myUpperCaseChannel", outputChannel = "lengthCalcChannel")
	public String toUpper(String str) {
		System.out.println("Received message from myUpperCaseChannel : " + str);
		return str.toUpperCase();
	}
}


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 com.sample.app.gateway.CustomGateway;

@SpringBootApplication
@Configuration
public class App {

	@Autowired
	private CustomGateway customGateway;

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

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

			customGateway.sendToUppercaseChannel("Hello World");
		};

	};

}


Total project structure looks like below.


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

 

Received message from myUpperCaseChannel : Hello World

Received message from lengthCalcChannel : HELLO WORLD

Received message from printChannel : 'HELLO WORLD' has length 11

 

You can download complete working application from below link.

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


 


Previous                                                    Next                                                    Home

No comments:

Post a Comment