[JAVA] Call Chain from Chain in Spring Integration

Development environment

What is a chain?

In Spring Integration, processing is developed by connecting processing with channel, but if you want to combine multiple processing as one processing, it is convenient to use chain.

I will omit the specific bean definition, but when the flow definition is as follows

Before applying chain


<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:int="http://www.springframework.org/schema/integration"

  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.xsd
   ">

  <!--Messaging gateway-->
  <int:gateway id="gateway" default-request-channel="wwwChannel"
    service-interface="com.example.WwwGateway">

  <int:filter input-channel="wwwChannel" output-channel="xxxChannel"/>

  <int:transformer input-channel="xxxChannel" output-channel="yyyChannel" />

  <int:service-activator input-channel="yyyChannel" output-channel="zzzChannel" />

With chain ...

After applying the chain


<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:int="http://www.springframework.org/schema/integration"

  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.xsd
   ">

  <!--Messaging gateway-->
  <int:gateway id="gateway" default-request-channel="wwwChannel"
    service-interface="com.example.WwwGateway">

  <int:chain input-channel="wwwChannel" output-channel="zzzChannel">
    <int:filter/>
    <int:transformer />
    <int:service-activator />
  </chain>

Can be written! It also has the advantage of preventing the flow definition from becoming full of channels: blush :.

Call chain from chain

So, the main subject is from here. If you are developing a complicated application, you want a chain that puts together a chain that puts together multiple processes. Intuitively, I wrote it like ↓, considering how to write chain.

I tried to put together a chain


<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:int="http://www.springframework.org/schema/integration"

  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.xsd
   ">

  <!--Messaging gateway-->
  <int:gateway id="gateway" default-request-channel="wwwChannel"
    service-interface="com.example.WwwGateway">

  <!--Chain that puts process A and process B together-->
  <int:chain input-channel="wwwChannel" output-channel="zzzChannel">
    <int:chain id="aProcess"/>
    <int:chain id="bProcess"/>
  </chain>

  <!--Process A-->
  <int:chain id="aProcess">
    <int:filter/>
    <int:transformer />
    <int:service-activator />
  </chain>

  <!--Process B-->
  <int:chain id="bProcess">
    <int:filter/>
    <int:transformer />
    <int:service-activator />
  </chain>

So, as a result of execution, a runtime error ...: disappointed_relieved: Specify an appropriate ʻinput-channel for the chainof the process A and thechain of the process B, or write the chainof the process A in thechain` that combines the process A and the process B. Was no good.

Solved by sandwiching the Gateway

I was worried for a while, but when I read the document again, the method was written: sweat :.

At the bottom of 6.6.2 Configuring a Chain, under Calling a Chain from within a Chain,

Sometimes you need to make a nested call to another chain from within a chain and then come back and continue execution within the original chain. To accomplish this you can utilize a Messaging Gateway by including a element.

I see……

When I wrote the <int: gateway> tag in the chain that combines process A and process B and described the input-channel of process A and process B in the request-channel attribute, the expected behavior was achieved. Ta! You did it!

Revised


<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:int="http://www.springframework.org/schema/integration"

  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.xsd
   ">

  <!--Messaging gateway-->
  <int:gateway id="gateway" default-request-channel="wwwChannel"
    service-interface="com.example.WwwGateway">

  <!--Chain that puts process A and process B together-->
  <int:chain input-channel="wwwChannel" output-channel="zzzChannel">
    <int:gateway request-channel="aChannel" />
    <int:gateway request-channel="bChannel" />
  </chain>

  <!--Process A-->
  <int:chain id="aProcess" input-channel="aChannel">
    <int:filter/>
    <int:transformer />
    <int:service-activator />
  </chain>

  <!--Process B-->
  <int:chain id="bProcess" input-channel="bChannel">
    <int:filter/>
    <int:transformer />
    <int:service-activator />
  </chain>

Finally

Read the docs: joy:

Recommended Posts

Call Chain from Chain in Spring Integration
call spring management bean from pojo
Call Java method from JavaScript executed in Java
Java method call from RPG (method call in own class)
Inject Logger in Spring
Call Java from JRuby
1. Start Spring framework from 1
Use Interceptor in Spring
Microservices in Spring Cloud
Call a program written in Swift from Processing (Java)
Get cookies in Spring
Call your own method with PreAuthorize in Spring Security
Implement Java Interface in JRuby class and call it from Java
How to call and use API in Java (Spring Boot)
Try Spring Boot from 0 to 100.
Dynamically call methods in JSF
Spring Boot 2 multi-project in Gradle
Error in Spring database connection
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Loop step in Spring Batch
[Java] Get data from DB using singleton service in Spring (Boot)
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series