In this post, I am going to explain following scenario.
a. Producer send a message to consumer via ‘inputChannel’.
b. Consumer reads the message from inputChannel and send acknowledgment to producer via outputChannel.
Find the below working application.
Step 1: Create new maven project ‘endpoint-reply-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-reply-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 package 'com.sample.app.endpoints' and define ProducerEndpoint, ProducerAckEndpoint and ConsumerEndpoint classes.
ProducerEndpoint.java
package com.sample.app.endpoints;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.stereotype.Component;
@Component
@MessagingGateway(name = "myGateway", defaultRequestChannel = "inputChannel")
public interface ProducerEndpoint {
@Gateway(requestChannel = "inputChannel", replyTimeout = 2, requestTimeout = 200)
public void produceMessage(String message);
}
ProducerAckEndpoint.java
package com.sample.app.endpoints;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Component;
@Component
public class ProducerAckEndpoint {
@ServiceActivator(inputChannel = "ackChannel")
public void receiveAcknowledgement(String message) {
System.out.println("Received acknowledgement Consumer : " + message);
}
}
ConsumerEndpoint.java
package com.sample.app.endpoints;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
public class ConsumerEndpoint {
@ServiceActivator(inputChannel = "inputChannel", outputChannel = "ackChannel", requiresReply="true")
public Message<String> consumeMessage(Message<String> message) {
System.out.println("Received from Producer : " + message);
return MessageBuilder.withPayload("Message Received").build();
}
}
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 com.sample.app.endpoints.ProducerEndpoint;
@SpringBootApplication
@Configuration
public class App {
@Autowired
@Qualifier("inputChannel")
private DirectChannel inputChannel;
@Autowired
@Qualifier("ackChannel")
private DirectChannel ackChannel;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean(name = "inputChannel")
public DirectChannel channel1() {
DirectChannel channel = new DirectChannel();
return channel;
}
@Bean(name = "ackChannel")
public DirectChannel channel2() {
DirectChannel channel = new DirectChannel();
return channel;
}
@Bean
public CommandLineRunner demo(@Autowired ProducerEndpoint producerEndpoint) {
return (args) -> {
producerEndpoint.produceMessage("Hello World");
};
}
}
Total project structure looks like below.
Run App.java, you will see below messages in console.
Received from Producer : GenericMessage [payload=Hello World, headers={replyChannel=nullChannel, id=842f54f2-54d1-93a8-619b-a6ef3548e6db, timestamp=1617373206585}]
Received acknowledgement Consumer : Message Received
You can download complete working application from below link.
https://github.com/harikrishna553/springboot/tree/master/spring-integration/endpoint-reply-demo
No comments:
Post a Comment