Spring Integration Study memorandum ~ Understanding Spring Integration Sample 1. Hello World ~

Purpose

In order to learn Spring Integration, I understood the sample provided by Spring Project and reimplemented it + arranged it. This article is a memorandum. The sample is registered in the following Git. spring-integration-sample

First of all, basic hello world. By the way, the version of Spring Integration is `` `4.3.10.RELEASE```.

Overview

Two functions are implemented in helloworld.

  1. Hello World
  2. Poller Application

First of all, I tried to implement it according to the sample, but since it was not understood enough, as the third I also implemented a combination of the two functions.

Implementation

(1) hello world

Overall picture

As a README, hello world implements a simple messaging flow as follows. message -> channel -> serviceactivator -> queuechannel

We will look inside the implementation from now on.

helloWorldDemo.xml 1-3 are easy to understand, just define the Channel and Service Avtivator components as beans. 4 defines the entire flow and is defined by the `service-activator``` tag. With this definition, when you call the send``` method of ```inputChannel (MessageChannel) , the process is delegated to the ``` ServiceActivator (HelloWorldService.seyHello method) `` . To. Then, the return result of the `` `sayHello``` method enters the outputChannel.


	<channel id="inputChannel"/> <!-- 1 -->
	<channel id="outputChannel"> <!-- 2 -->
		<queue capacity="10"/>
	</channel>


	<beans:bean id="sampleService" 
    class="org.ek.sample.helloworld.HelloWorldService"/> <!-- 3 -->
  
	<service-activator input-channel="inputChannel"
	                   output-channel="outputChannel"
	                   ref="sampleService"
	                   method="sayHello"/> <!-- 4 -->

Main class

The code for the Main class is as follows. Read the xml file in step 1 and create a ApplicationContext installation. After that, Channel is getBean (2,3), and as mentioned above, `inputChannel.send``` is ``` ServiceActivator``` sayHelloDelegate processing to the method (4). When the process is finished, the value will be entered in outputChannel, so you can get the _return_ value withouputChannel.receive.getPayload. Here, the acquired contents are passed to the logger and output as standard. (Five) In addition, it seems that there are those that can get the value by Channel and those that are only for sending, and `` `PollableChannel inherits MessageChannel and then `receive You can get the value because you define the method, and `MessageChannel``` seems to be a send specialty.

	public static void main(String[] args) {

		@SuppressWarnings("resource")
		AbstractApplicationContext context = new ClassPathXmlApplicationContext(
				"/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldMain.class); // 1
		MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class); // 2
		PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class); // 3

		inputChannel.send(new GenericMessage<String>("World")); // 4
		logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload()); // 5

ServiceActivator is omitted, but the `` `sayHello``` method was as simple as adding a small string of the argument String and returning. By the way, it is POJO, and it does not inherit or implement anything.

(2) Polling

Overall picture

An application that polls and acquires the system time twice every 20 seconds (20000 milliseconds) and passes it to the logger for standard output.

delay.xml This time, I wasn't quite sure what the inbound-channel-adapter tag can do, and why the executor tag needs to be defined, so it's a little bit. I saw xsd. I only know how to read xsd in a simple way, but is it roughly in the following form? (If you make a mistake, please point out someone)

<int:inbound-channel-adapter expression="T(java.lang.System).currentTimeMillis()" channel="logger">
		<int:poller fixed-delay="20000" max-messages-per-poll="2" />
	</int:inbound-channel-adapter> <!-- 1 -->

	<int:logging-channel-adapter id="logger" logger-name="org.springframework.integration.samples.helloworld"/> 

	<task:executor id="executor" queue-capacity="20" pool-size="5-20"/> <!-- 2 -->

Main class

This time it's polling, so the Main class just needs to read the context. After that, it keeps outputting logs until the logger stops the application.


    public static void main(String[] args) throws Exception{
		new ClassPathXmlApplicationContext("META-INF/spring/integration/delay.xml");
	}

(3) polling + hello world

Overall picture

With this alone, I would only see the implementation of the sample, so I also implemented the form of polling and sending the obtained contents to ServiceActivator and outputting the last log (combining the two so far). Although it seems that there is a lot of waste in what I could implement, this is the limit with my knowledge so far. Let's remake it if we study more.

delay_hello_world.xml I made it by adding the two so far, but in fact it didn't work if I just added it, so I had to arrange it a little. First of all, the beans tag of 1. Polling and Hello World each have a slight difference in particle size (definition of xmlns), so it is necessary to match them. Along with that, `int:` is added to the `` `channeland service-activator``` tags used in the HelloWorld application.

Next, I defined a new `QueueChannel``` called bridgeChannel of 2. I wondered if the ```inbound-channel-adapter``` tag internally calls MessageChannel.send``` (I haven't checked that far), so I added a new channel to the outputChannel. I decided to define, take a value from it in the ``` Main``` class, and pass it to `` inputChannel.send```.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 1 -->
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.3.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:task="http://www.springframework.org/schema/task">

	<int:inbound-channel-adapter expression="'World! time is ' + T(java.lang.System).currentTimeMillis()" channel="bridgeChannel">
		<int:poller fixed-delay="2000" max-messages-per-poll="2" />
	</int:inbound-channel-adapter>

    <!-- 2 -->
	<int:channel id="bridgeChannel">
		<int:queue capacity="2"/>
	</int:channel>
	
	<int:channel id="inputChannel"/>
	<int:channel id="outputChannel">
		<int:queue capacity="10"/>
	</int:channel>

	<int:service-activator input-channel="inputChannel"
	                   output-channel="outputChannel"
	                   ref="sampleService"
	                   method="sayHello"/>

	<bean id="sampleService" class="org.ek.sample.helloworld.HelloWorldService"/>
	
</beans>

Main class

Main looks like this. It feels like I keep getting what I received on the brideChannel with ``` while (true)` `` to keep polling. Well, it works as expected, so this is the first understanding of Spring Integration.

	private static Logger logger  = Logger.getLogger(PollingHelloWorld.class);

	public static void main(String[] args) {
		
		@SuppressWarnings("resource")
		AbstractApplicationContext context = 
				new ClassPathXmlApplicationContext("/META-INF/spring/integration/delay_hello_world.xml");
		
		PollableChannel bridgeChannel = context.getBean("bridgeChannel", PollableChannel.class);
		MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
		PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);

		while(true) {
			Message<?> msg = bridgeChannel.receive(0);			
			if(msg != null) {
				String str = (String) msg.getPayload() ;
				inputChannel.send(new GenericMessage<String>(str));
				logger.info("==> Demo: " + outputChannel.receive(0).getPayload());
			}
			
		}
	}

Recommended Posts

Spring Integration Study memorandum ~ Understanding Spring Integration Sample 1. Hello World ~
Spring Integration Study memorandum ~ Understanding Spring Integration Sample 3. Enricher ~
Spring Integration Study memorandum ~ Understanding Spring Integration Sample 2. JMS Gateway ~
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with Spring Boot
Spring Boot Hello World in Eclipse
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
[Practice! ] Display Hello World in Spring Boot
WebMvcConfigurer Memorandum of Understanding for Spring Boot 2.0 (Spring 5)
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Hello world! With Spring Boot (Marven + text editor)
Hello World at explosive speed with Spring Initializr! !! !!
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Try to display hello world with spring + gradle
Read "Hello world"
Java, Hello, world!
Java Hello World
Java study memorandum
Spring Boot Memorandum
Memorandum (Spring Web)
Hello World (REST API) with Apache Camel + Spring Boot 2
Hello World comparison between Spark Framework and Spring Boot
Hello World (console app) with Apache Camel + Spring Boot 2