Spring integration Spring-消息驱动通道适配器-停止一个特定的并发使用者

Spring integration Spring-消息驱动通道适配器-停止一个特定的并发使用者,spring-integration,Spring Integration,我使用spring集成组件连接我的流程 <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="destination" ref="requestQueue"/> <property name="concurrentConsumers" value="5"/> <p

我使用spring集成组件连接我的流程

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
  • 消息驱动通道适配器从weblogic队列中拾取消息
  • 使用消息转换器并将消息丢弃到通道

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • 来自通道的此消息由服务激活器拾取

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • ServiceActivator上的pojo从db执行一些数据处理,然后生成一个报告

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • 出于某些原因,我想停止此消费者处理此请求,原因如下(例如pojo处理时间太长)

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
我有什么选择

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
  • 我试过这些:
    • 通过JMX公开了spring的消息驱动器通道适配器
    • 在消息驱动通道适配器上使用了停止方法
    • 这会阻止消息驱动的通道适配器拾取新消息,这是很好的
    • 但不会阻止已拾取消息且当前正在处理的消费者
    • 我怎样才能停止这个在我的pojo中花费太长时间的过程
谢谢你的帮助

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />

更新-2

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
这是我的密码。

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />

  • 我看到交易如你所说正在进行中

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • 但我看到创建了5到10个threadPoolTaskExecutors,因为我有并发使用者=5-10(在适配器上)

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • 为什么要创建多个ThreadPoolTaskExecutor?它是否应该只使用一个threadpooltaskexecutor
  • 如果我将concurrent consumer设置为1,则自会话处理后,它将等待拾取每条消息
感谢您的回复。多谢各位

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />

更新-3 -当与适配器一起使用时,在executor服务中维护事务非常有效

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
  • 关于杀死特定线程,我向executor服务添加了一个名为interruptNamedThread(String threadName)的方法,并通过JMX公开了它
  • 触发它并设置中断标志
  • 我可以检查中断状态并通过返回来终止线程(因为我的测试代码在循环中运行,并且有机会检查中断状态)

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • 然而,这对我的情况没有帮助

  • <bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="requestQueue"/>
        <property name="concurrentConsumers" value="5"/>
        <property name="maxConcurrentConsumers" value="10"/>
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
        <property name="sessionTransacted" value="true"/>
    </bean> 
    
        <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="queueCapacity" value="10" />
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean> 
    
    <!-- Channel where service activator drops message to -->
    <si:channel id="jmsOutChannel" />
    
    <!-- This bean code calls DB to get some data and build a report/-->
    <bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>
    
    <!-- Initialize service activator -->
    <si:service-activator id="activator" input-channel="jmsInChannel"
        ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
    </si:service-activator>
    
    <!-- outbound adaptor for response Queue -->
    <jms:outbound-channel-adapter id="jmsout"
        channel="jmsOutChannel" destination="responseQueue" />
    
  • 因为我的线程不是在循环中运行的。它与DB对话并生成报告。线的末端。返回池
  • 我想在DB调用或第三方库调用(report generator)返回的时间过长时结束线程
  • 在这种情况下,它将没有机会检查中断标志,因为它卡在一个调用(DB或报告生成器)中
在我的用例中,重启组件是杀死线程的唯一方法吗

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />

感谢您的建议。

您可以插入一个
任务执行器
,并将其
等待任务完成设置为false;这将导致调用时,它的
shutdown()
方法调用底层
ExecutorService
上的
shutdownow()
(或注入
ExecutorService
并直接调用其
shutdownow()
方法)

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
这将取消(尝试中断)活动线程

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
但是,如果您的服务正在做一些不可中断的事情,例如

<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />
synchronize(foo) {}

lock.lock()

…或从插座读取数据等。

感谢您的快速支持。我在这里看到了两个问题。1.)ExecutorService正在关闭所有活动线程。我只需要关闭有问题的线程。2.)一旦jms事务模式使用executor服务生成线程,我就会丢失它。如果executorservice派生线程中存在错误,我希望看到它回滚消息。您在哪里注入
executorservice
?您是否要将其添加到频道?在侦听器容器上使用外部执行器(在适配器内,通过
task executor
属性)不会影响事务语义。如果只想杀死选定的线程,则需要实现自己的看门狗代码。可能是一个通道拦截器,它在
preSend()
中调度
Thread.interrupt()
,并在
postSend()
中取消调度的任务。只需添加我的问题和代码详细信息。您可以回顾一下吗?只有一个执行器,您看到的是该执行器创建的线程(它们有名称
-n
)。感谢您的输入。在我的问题中添加了更新-3。请给我一些建议。
<bean id="myListener" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="destination" ref="requestQueue"/>
    <property name="concurrentConsumers" value="5"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="taskExecutor" ref="threadPoolTaskExecutor"/>
    <property name="sessionTransacted" value="true"/>
</bean> 

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="queueCapacity" value="10" />
    <property name="waitForTasksToCompleteOnShutdown" value="false"/>
</bean> 

<!-- Channel where service activator drops message to -->
<si:channel id="jmsOutChannel" />

<!-- This bean code calls DB to get some data and build a report/-->
<bean id="simpleExecutor" class="com.poc.reports.executors.SimpleCode"/>

<!-- Initialize service activator -->
<si:service-activator id="activator" input-channel="jmsInChannel"
    ref="simpleExecutor" method="execute" output-channel="jmsOutChannel">
</si:service-activator>

<!-- outbound adaptor for response Queue -->
<jms:outbound-channel-adapter id="jmsout"
    channel="jmsOutChannel" destination="responseQueue" />