Friday, 21 May 2021

Spring integration: endpoints example

Messaging endpoint is used to connect the application code to messaging framework. Messaging endpoints are mapped to channels, to isolate the business logic from the messaging infrastructure.

 

Example

@ServiceActivator(inputChannel = "echoChannel")

public void print(String message) {

         System.out.println(message);

}

 

In the above example, service activator endpoint is mapped to the input channel "echoChannel", whatever the message received by "echoChannel" is delivered to the print method.

 

 

 

 

Spring integration framework is built on lightweight message-driven architecture, where messages are exchanged using channels. In Spring integration framework, endpoints perform the operation and channels perform the routing.

 

There are different types of endpoints available in spring integration framework. In this post, I am going to create an endpoint using @ServiceActivator annotation.

 

@ServiceActivator annotation can be applied on a method and it indicates that a method is capable of handling a message or message payload.

 

Example

@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());
	}
}


In the above snippet, I defined two service activator methods

a.   Print -> It receives the messages from echoChannel

b.   reversePrint -> It receives the messages from reverseEchoChannel.

 

Let’s define the channels echoChannel and reverseEchoChannel.

@Bean(name = "echoChannel")
public DirectChannel channel1() {
	DirectChannel channel = new DirectChannel();
	return channel;
}

@Bean(name = "reverseEchoChannel")
public DirectChannel channel2() {
	DirectChannel channel = new DirectChannel();
	return channel;
}

 

Now you can autowire these beans and publish the messages.

@Autowired
@Qualifier("echoChannel")
private DirectChannel messageChannel;

@Autowired
@Qualifier("reverseEchoChannel")
private DirectChannel reverseMessageChannel;

messageChannel.send(message);
reverseMessageChannel.send(message);

 


 

Find the below working application.

 

Step 1: Create new maven project ‘endpoint-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>endpoint-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 a package ‘com.sample.app.endpoints’ and define PrintService class.

 

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.beans.factory.annotation.Qualifier;
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.channel.DirectChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

@SpringBootApplication
@Configuration
public class App {

	@Autowired
	@Qualifier("echoChannel")
	private DirectChannel messageChannel;

	@Autowired
	@Qualifier("reverseEchoChannel")
	private DirectChannel reverseMessageChannel;

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

	@Bean(name = "echoChannel")
	public DirectChannel channel1() {
		DirectChannel channel = new DirectChannel();
		return channel;
	}

	@Bean(name = "reverseEchoChannel")
	public DirectChannel channel2() {
		DirectChannel channel = new DirectChannel();
		return channel;
	}

	@Bean
	public CommandLineRunner demo() {
		return (args) -> {
			Message<String> message = MessageBuilder.withPayload("Hello World").build();

			messageChannel.send(message);
			reverseMessageChannel.send(message);

		};
	}

}


Total project structure looks like below.





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

 

Hello World

dlroW olleH

 

You can download the complete working application from below link.

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

 

 

 

 

 

 

 

 



Previous                                                    Next                                                    Home

No comments:

Post a Comment