@Payload annotation binds a method parameter to the payload of a message.
Example
@ServiceActivator(inputChannel = "printChannel")
public void consumeStringMessage(@Payload String msg, @Header("msgId") String msgId) {
	System.out.println("Received message from printChannel : " + msg);
	System.out.println("Message id : " + msgId);
}
Find the below working application.
Step 1: Create new maven project ‘extract-payload-and-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.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>extract-payload-and-header-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 org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
@Component
public class PrintService {
	@ServiceActivator(inputChannel = "printChannel")
	public void consumeStringMessage(@Payload String msg, @Header("msgId") String msgId) {
		System.out.println("Received message from printChannel : " + msg);
		System.out.println("Message id : " + msgId);
	}
}
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.messaging.Message;
@MessagingGateway
public interface CustomGateway {
	@Gateway(requestChannel = "printChannel")
	public void print(Message<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.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<String> msg = MessageBuilder.withPayload("india is a beautiful country")
					.setHeader("msgId", "id-123").setHeader("tag", "test").build();
			customGateway.print(msg);
		};
	};
}
Total project structure looks like below.
Run App.java, you will see below messages in console.
Received message from printChannel : india is a beautiful country
Message id : id-123
You can download complete working application from below link.
Previous Next Home

No comments:
Post a Comment