You can add headers to a message using gateway.
How to add headers using @MessagingGateway annotation
‘defaultHeaders’ attribute is used to custom message headers. These default headers are created for all methods on the service-interface.
Example
@MessagingGateway(defaultHeaders = {@GatewayHeader(name = "root-header", value = "root123")})
public interface CustomGateway {
}
How to add headers using @Gateway annotation?
'headers' attribute is used specify additional headers that will be added to the request message.
@Gateway(requestChannel = "printChannel", headers = {
@GatewayHeader(name = "secret", value = "my-secret123"),
@GatewayHeader(name = "service-name", value = "my-service") })
public String sendToPrintChannel(String message);
Find the below working application.
Step 1: Create new maven project ‘gateway-headers-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>gateway-headers-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 gateway.
CustomGateway.java
package com.sample.app.gateway;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.*;
@MessagingGateway(defaultHeaders = {@GatewayHeader(name = "root-header", value = "root123")})
public interface CustomGateway {
@Gateway(requestChannel = "printChannel", headers = {
@GatewayHeader(name = "secret", value = "my-secret123"),
@GatewayHeader(name = "service-name", value = "my-service") })
public String sendToPrintChannel(String message);
}
Step 4: Define service activator.
PrintService.java
package com.sample.app.service;
import java.util.Map;
import java.util.Set;
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) {
System.out.println("Received message from printChannel : " + message.getPayload());
Set<Map.Entry<String, Object>> messageHeaders = message.getHeaders().entrySet();
System.out.println("Headers are.....");
for(Map.Entry<String, Object> entry: messageHeaders) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
return "Success";
}
}
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 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) -> {
String reply = customGateway.sendToPrintChannel("Hello World");
System.out.println("reply : " + reply);
};
};
}
Total project structure looks like below.
Run App.java, you will see below messages in console.
Received message from printChannel : Hello World
Headers are.....
service-name -> my-service
replyChannel -> org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@64712be
errorChannel -> org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@64712be
root-header -> root123
secret -> my-secret123
id -> f3f45661-6f2e-b0c4-5503-04c302aeb937
timestamp -> 1617701164345
reply : Success
You can download complete working application from below link.
https://github.com/harikrishna553/springboot/tree/master/spring-integration/gateway-headers-demo
Previous Next Home
No comments:
Post a Comment