Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring integration: replyChannel header: Specify replychannel of message using header

You can specify the reply channel of a Message by setting replyChannel header.

 

Example

Message msg = MessageBuilder.withPayload("india is a beautiful country").setHeader("replyChannel", "myOutputChannel").build();

 

Step 1: Create new maven project ‘reply-channel-header-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.0modelVersion>
groupId>com.sample.appgroupId>
artifactId>reply-channel-header-demoartifactId>
version>1version>


parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.4.0version>
parent>

dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-integrationartifactId>
dependency>

dependencies>

project>

 

Step 3: Define gateway.

 

CustomGateway.java

package com.sample.app.gateway;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.messaging.Message;

@MessagingGateway
public interface CustomGateway {

@Gateway(requestChannel = "myInputChannel")
public void print(Message message);

}

 

Step 4: Define service activator endpoints.

 

ConsumerEndpoint.java

 

package com.sample.app.endpoints;

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

@Component
public class ConsumerEndpoint {

@ServiceActivator(inputChannel = "myOutputChannel")
public void consumeStringMessage(String message) {
System.out.println("Received message from myOutputChannel : " + message);
}

@ServiceActivator(inputChannel = "myInputChannel")
public String toUppercase(Message message) {
System.out.println("Received message from myInputChannel : " + message.getPayload());

return message.getPayload().toUpperCase();
}
}

 

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.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

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) -> {

Message msg = MessageBuilder.withPayload("india is a beautiful country")
.setHeader("replyChannel", "myOutputChannel").build();

customGateway.print(msg);
};

};

}

 

Total project structure looks like below.

 


 


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

 

Received message from myInputChannel : india is a beautiful country

Received message from myOutputChannel : INDIA IS A BEAUTIFUL COUNTRY

 

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/spring-integration/reply-channel-header-demo

 

Previous                                                    Next                                                    Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring integration: replyChannel header: Specify replychannel of message using header

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×