Sunday, 16 May 2021

Spring integration: Hello World application

In this post, I am going to explain how to send a message from custom gateway to another endpoint via a channel.

 

In brief, we are going to develop a simple messaging system.



Integration1

Whatever the message we write to printGateway, should be send to echoChannel and to printActivator.

 

Step 1:

Whatever the message we write to reversePrintGateway, should be send to reverseEchoChannel and to reversePrintActivator.

 

Define Gateways

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

	@Gateway(requestChannel = "echoChannel", replyTimeout = 2, requestTimeout = 200)
	public void print(String message);
	
	@Gateway(requestChannel = "reverseEchoChannel", replyTimeout = 2, requestTimeout = 200)
	public void reverse(String message);

}


'requestChannel' attribute specifies the channel to which messages will be sent.

 

'replyTimeout' specify the time (ms) that the thread sending the request will wait for a reply.

 

'requestTimeout' specify the timeout (ms) when sending to the request channel - only applies if the send might block (such as a bounded QueueChannel) that is currently full.

                          

Define service activator

@Component
public class PrintService {

	@ServiceActivator(inputChannel = "echoChannel")
	public void print(String message) {
		System.out.println(message);
	}
	
	@ServiceActivator(inputChannel = "reverseEchoChannel")
	public void reversePrint(String message) {
		System.out.println(new StringBuilder(message).reverse().toString());
	}
}


'inputChannel' specify the channel from which this service activator will consume messages.

 

Find the below working application.

 

Step 1: Create new maven project ‘hello-world’.

 

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>hello-world</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 a package 'com.sample.app.endpoints' and define CustomGateway, PrintService classes.

 

CustomGateway.java

package com.sample.app.endpoints;

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

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

	@Gateway(requestChannel = "echoChannel", replyTimeout = 2, requestTimeout = 200)
	public void print(String message);
	
	@Gateway(requestChannel = "reverseEchoChannel", replyTimeout = 2, requestTimeout = 200)
	public void reverse(String message);

}


PrintService.java

package com.sample.app.endpoints;

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

@Component
public class PrintService {

	@ServiceActivator(inputChannel = "echoChannel")
	public void print(String message) {
		System.out.println(message);
	}
	
	@ServiceActivator(inputChannel = "reverseEchoChannel")
	public void reversePrint(String message) {
		System.out.println(new StringBuilder(message).reverse().toString());
	}
}


Step 4: Define App class in com.sample.app package.

 

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.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) -> {
			gateway.print("Hello World");
			gateway.reverse("Reverse Me!!!!");
		};
	}

}


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

 

Hello World

!!!!eM esreveR

 

Total project structure looks like below.






You can download complete working application from this link.

https://github.com/harikrishna553/springboot/tree/master/spring-integration/hello-world




Previous                                                    Next                                                    Home

No comments:

Post a Comment