By returning a Future or ListenableFuture from gateway method, we can make the gateway method asynchronous.
Step 1: Define gateway with ListenableFuture as return type.
@MessagingGateway
public interface CustomGateway {
@Gateway(requestChannel = "printChannel")
public ListenableFuture<String> sendToPrintChannel(String message);
}
Step 2: Use callback method to capture the asynchronous response.
ListenableFuture<String> reply = customGateway.sendToPrintChannel("Msg " + i);
reply.addCallback(new ListenableFutureCallback<String>() {
@Override
public void onSuccess(String result) {
System.out.println(result);
}
@Override
public void onFailure(Throwable ex) {
ex.printStackTrace();
}
});
Find the below working application.
Step 1: Create new maven project ‘asynchronous-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>asynchronous-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 service activator.
PrintService.java
package com.sample.app.service;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
public class PrintService {
@ServiceActivator(inputChannel = "printChannel")
public String consumeStringMessage(Message<String> message) throws InterruptedException {
Random random = new Random();
int timeToSleep = random.nextInt(10);
TimeUnit.SECONDS.sleep(timeToSleep);
System.out.println("Received message from printChannel : " + message.getPayload());
return "Message " + message.getPayload() + " received";
}
}
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.util.concurrent.ListenableFuture;
@MessagingGateway
public interface CustomGateway {
@Gateway(requestChannel = "printChannel")
public ListenableFuture<String> sendToPrintChannel(String message);
}
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 org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
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) -> {
for (int i = 0; i < 5; i++) {
ListenableFuture<String> reply = customGateway.sendToPrintChannel("Msg " + i);
reply.addCallback(new ListenableFutureCallback<String>() {
@Override
public void onSuccess(String result) {
System.out.println(result);
}
@Override
public void onFailure(Throwable ex) {
ex.printStackTrace();
}
});
}
};
};
}
Total project structure looks like below.
Run App.java, you will see below kind of messages in console.
Received message from printChannel : Msg 1 Message Msg 1 received Received message from printChannel : Msg 4 Message Msg 4 received Received message from printChannel : Msg 0 Message Msg 0 received Received message from printChannel : Msg 2 Message Msg 2 received Received message from printChannel : Msg 3 Message Msg 3 received
You can download complete working application from below link.
No comments:
Post a Comment