MessagingTemplate class provides lot of utility methods to work with requests and response over a channel.
Example
MessagingTemplate messaginTemplate = new MessagingTemplate();
Message<String> result = (Message<String>) messaginTemplate.sendAndReceive(inputChannel, message);
In the above snippet, I am using MessagingTemplate class to send a message over channel and receive the response.
Find the below working application.
Step 1: Create new maven project ‘messaging-template-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>messaging-template-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 ConsumerEndpoint class.
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 = "myChannel")
public Message<String> consumeMessage(Message<String> message) {
System.out.println("Received from Producer : " + message);
return MessageBuilder.withPayload("Message Received").build();
}
}
Step 4: Create a package ‘com.sample.app’ and define App class.
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.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
@SpringBootApplication
@Configuration
public class App {
@Autowired
@Qualifier("myChannel")
private DirectChannel inputChannel;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean(name = "myChannel")
public DirectChannel channel1() {
DirectChannel channel = new DirectChannel();
return channel;
}
@Bean
public CommandLineRunner demo() {
return (args) -> {
Message<String> message = MessageBuilder.withPayload("Hello World").build();
MessagingTemplate messaginTemplate = new MessagingTemplate();
Message<String> result = (Message<String>) messaginTemplate.sendAndReceive(inputChannel, message);
System.out.println("result = " + result);
};
}
}
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=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6a1d204a, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6a1d204a, id=62f4905f-8c2f-26a5-9c65-4b3e58bf5de9, timestamp=1617378622457}]
result = GenericMessage [payload=Message Received, headers={id=0d7525a2-fa88-7600-5e46-f18fb37a5ed8, timestamp=1617378622462}]
You can download complete working application from below link.
https://github.com/harikrishna553/springboot/tree/master/spring-integration/messaging-template-demo
No comments:
Post a Comment