亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Integration 任務執行器在測試過程中大約 1000 毫秒后毫無警告地終止

Spring Integration 任務執行器在測試過程中大約 1000 毫秒后毫無警告地終止

猛跑小豬 2023-10-19 21:04:22
我使用 Spring Integration 構建以下流程:輸入通道 -> 拆分器 -> 轉換器 -> 服務激活器 -> 聚合器Transformer 和 Service Activator 使用任務執行器鏈接并執行。在應用程序執行期間,沒有任何問題。但是,當我嘗試運行單元測試時,如果存在長時間運行的任務,則執行服務激活器的執行程序線程會神秘退出。為了演示這一點,我創建了一個具有以下配置的示例項目:<task:executor id="executor" pool-size="20" keep-alive="120" queue-capacity="100"/><jms:message-driven-channel-adapter id="helloWorldJMSAdapater" destination="helloWorldJMSQueue"    channel="helloWorldChannel"/>    <int:channel id="helloWorldChannel"/><int:splitter id="splitter" input-channel="helloWorldChannel" output-channel="execChannel">    <bean id="stringSplitter" class="hello.Splitter"></bean></int:splitter><int:channel id="execChannel">    <int:dispatcher task-executor="executor"></int:dispatcher></int:channel><int:chain input-channel="execChannel" output-channel="aggregatorChannel">    <int:transformer>        <bean id="stringTransformer" class="hello.Transformer"></bean>    </int:transformer>    <int:service-activator id="helloWorldServiceActivator" ref="helloWorldAmqService" method="processMsg"/></int:chain><int:aggregator input-channel="aggregatorChannel" output-channel="errorChannel">    <bean class="hello.ResponseAggregator"/></int:aggregator>這是 Splitter 類:public class Splitter {public List<String> splitMessage(Message message)  {    String msg = message.getPayload().toString();    return Arrays.asList(msg.split(","));}}這是 Transformer 類:public class Transformer {public String transform(Message message)  {    String msg = message.getPayload().toString();    return msg+"t";}}為了模擬長時間運行的任務,我Thread.sleep()在 processMsg 方法中添加了一個。
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

您的問題是您錯過了一個事實,即您的應用程序是一個async,但您的測試與處理多線程解決方案無關。


您在測試方法中發送一條消息,并且不執行任何操作來等待輸出消息。因此,啟動測試執行的主線程就存在,而將其他線程中的所有執行拋在后面。


您的想法是發送到而helloWorldChannel不是處理 JMS 目的地是一個不錯的選擇。唯一的問題是聚合后不等待流結果。


將端點的輸出放入 中也很奇怪errorChannel,但您可以在生成消息之前從測試用例中訂閱它:


@Autowired

private SubscribableChannel errorChannel;



@Test

public void test() {

    SettableListenableFuture<Message<?>> messageFuture = new SettableListenableFuture<>();

    this.errorChannel.subscribe((message) -> messageFuture.set(message));

    helloWorldChannel.send(MessageBuilder.withPayload("1,2,3,4,6").build());

    Message<?> messageToAssert = messageFuture.get(10, TimeUnit.SECONDS);

    ...

}

這樣,您的主 JUnit 線程將等待該 中的結果,而與流行為無關Future。


查看完整回答
反對 回復 2023-10-19
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號