Monday, 21 June 2021

Spring integration: why there is no outbound channel adapter

An outbound channel adapter endpoint connects a MessageChannel to a target system.

 


 

Outbound channel adapter is used to send the messages from internal messaging system to external service. @ServiceActivator annotation is capable of achieving this functionality.

 

Example

@ServiceActivator(inputChannel = "notifySecretMsg")
public void notifyExternalService(String msg) {
	System.out.println("Acknowledgement received for the message '" + msg + "'");
}


How can I configure Http requests to not worry about response from external service.

Set expect reply property to false, while delivering the message to external service.

@Bean
@ServiceActivator(inputChannel = "notifySecretMsg")
public HttpRequestExecutingMessageHandler httpRequestExecutingMessageHandler() {
	HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler();
	handler.setExpectReply(false)
    return handler;
}


Find the below working application.

 

Step 1: Create new maven project ‘outbound-adapter-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>outbound-adapter-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 service activators.

 

PrintService.java

package com.sample.app.service;

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

@Component
public class PrintService {

	@ServiceActivator(inputChannel = "secretMsgChannel", outputChannel = "notifySecretMsg")
	public String consumeStringMessage(Message<String> message) throws InterruptedException {
		System.out.println("Received message from secretMsgChannel : " + message.getPayload());

		return message.getPayload();
	}

	@ServiceActivator(inputChannel = "notifySecretMsg")
	public void notifyExternalService(String msg) {
		System.out.println("Acknowledgement received for the message '" + msg + "'");
	}
}


Step 4: Define main application.

 

App.java

package com.sample.app;

import java.util.UUID;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

@SpringBootApplication
@Configuration
public class App {

	@InboundChannelAdapter(channel = "secretMsgChannel", poller = {
			@Poller(maxMessagesPerPoll = "2", fixedRate = "5000") })
	Message<String> getSecretMessageFromExternalSystem() {
		UUID uuid = UUID.randomUUID();
		String secretMsg = uuid.toString();

		return MessageBuilder.withPayload(secretMsg).build();

	}

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

}


Total project structure looks like below.




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

Received message from secretMsgChannel : f319748c-dc72-4c80-8e50-0af114766cab
Acknowledgement received for the message 'f319748c-dc72-4c80-8e50-0af114766cab'
Received message from secretMsgChannel : fbc7851a-21d4-4922-94ec-ae5d02b418b7
Acknowledgement received for the message 'fbc7851a-21d4-4922-94ec-ae5d02b418b7'

 

You can download complete working application from below link.

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

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment