简体   繁体   中英

Spring integration with multiple executor channel not processing in parallel

I have requirement where i need to pass message to multiple channels asyc. To make my flow asyc i am using all executor channel. But for some reason flow is still sequential. i can seen diff thread as i configured in task executor but in sequence. Here is the configuration I am using

<int:channel id="mainChannel">
        <int:interceptors>
            <int:wire-tap channel="channel1"/>
            <int:wire-tap channel="channel2"/>
            <int:wire-tap channel="channel3"/>
      
</int:interceptors>
    </int:channel> 
   <int:channel id="channel1">
        <int:dispatcher task-executor="exec1" />
   </int:channel>
  <int:channel id="channel2">
        <int:dispatcher task-executor="exec2" />
  </int:channel>
   <int:channel id="channel3">
        <int:dispatcher task-executor="exec3" />
  </int:channel>



As per my understanding all this will be asyc (in my case 3 thread should run in parallel)

from log i can see all sequential but with diff thread name.. I am assuming preSend/Postsend should have been called in random order.

am i missing anything to make multiple executor channel in parallel.

I will really appreciate any help.

You might need to call the async implementation bean as shown:

<beans:bean id="asyncExecutor"
            class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>

   <int:channel id="channel1">
        <int:dispatcher task-executor="asyncExecutor" />
   </int:channel>
  <int:channel id="channel2">
        <int:dispatcher task-executor="asyncExecutor" />
  </int:channel>
   <int:channel id="channel3">
        <int:dispatcher task-executor="asyncExecutor" />
  </int:channel>

Description of SimpleAsyncTaskExecutor:

public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator implements AsyncListenableTaskExecutor, Serializable

TaskExecutor implementation that fires up a new Thread for each task, executing it asynchronously.

Supports limiting concurrent threads through the "concurrencyLimit" bean property. By default, the number of concurrent threads is unlimited.

NOTE: This implementation does not reuse threads, Consider a thread-pooling TaskExecutor implementation instead. in particular for executing a large number of short-lived tasks.

Example Of Usage from Github:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             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">

    <channel id="taskExecutorOnly">
        <dispatcher task-executor="taskExecutor"/>
    </channel>

    <channel id="failoverFalse">
        <dispatcher failover="false"/>
    </channel>

    <channel id="failoverTrue">
        <dispatcher failover="true"/>
    </channel>

    <channel id="loadBalancerDisabled">
        <dispatcher load-balancer="none"/>
    </channel>

    <channel id="loadBalancerDisabledAndTaskExecutor">
        <dispatcher load-balancer="none" task-executor="taskExecutor"/>
    </channel>

    <channel id="roundRobinLoadBalancerAndTaskExecutor">
        <dispatcher load-balancer="round-robin" task-executor="taskExecutor"/>
    </channel>

    <channel id="lbRefChannel">
        <dispatcher load-balancer-ref="lb"/>
    </channel>

    <beans:bean id="taskExecutor"
                class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>

    <beans:bean id="lb"
                class="org.springframework.integration.channel.config.DispatchingChannelParserTests.SampleLoadBalancingStrategy"/>
</beans:beans>

from log i can see all sequential but with diff thread name

Because logs are just a single place where messages are printed and they really are printed by one writer even if from different thread. They are appear over there one by one. With a good load you would definitely see that messages are logged in an unexpected order.

I am assuming preSend/Postsend should have been called in random order.

That's not true. Interceptors are called in an order how they are added to the channel and if their order is the same, which is a case for you. It is already not an interceptor chain responsibility how those interceptors are implemented.

I think you just were not lucky to see logs in arbitrary order and probably just because consumers for those executor channels are plain loggers - no any loads to hold the thread and have an impression that work in other threads is done in parallel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM