javascript
使用Spring Integration聚合异步结果
嗨,我遇到了一個問題,該問題使用Spring Integration具有非常好的解決方案。 很多時候,我們需要一種將消息分發到未知數量的目的地的方案。 為此,我們使用主題方法。 但是有時,我們還希望從收到消息的所有目的地接收答案,并將其匯總為單個結果答案。 為此,我們可以將通道與Aggregator和ReleaseStrategy接口結合使用。 在這篇文章中,我不會專注于“渠道實施”。
假設我們有一個生產者將其消息發送給主題。 現在我們有一個接收該消息的消費者。 使用網關和處理器接口,我們可以以任何所需的類型發送該消息:
public interface Processor {public void sendResponse(String response); }消費者代碼:
@Override public void onMessage(Message message) {String resultMessage = "";try{processor.sendResponse(resultMessage);}catch (Exception e){log.error("Error while processing message in channel consumer. errorMsg=" + e.getMessage(), e);} }現在,該消息將被傳遞到一個通道(“在通道中”)。我們可以向該消息添加額外的信息(如果我們有不同的消息組)。 添加了額外的信息后,我們將該消息發送到另一個通道(“ Out通道”),這就是魔術:我們創建了兩個pojo,稍后將使用XML配置將其綁定到接口。 發行策略:
public class ReleaseStrategy {public boolean canRelease(List results){// check if all 5 subscribers sent responsesif (results.size() == 5){return true;}return false;} }聚合器:
public class Aggregator {public String aggregate(List results){String finalResult= "SUCCESS_RESULT";for (String result: results) {if (result.equals("ERROR_RESULT")) {finalResult= "ERROR_RESULT";break;}}return finalResult;} }基本上,這里發生的事情是,在我們通過ReleaseStrategy接口的canRelease方法返回“ true”值之后,聚合器將能夠接收聚合的消息并將單個結果分派到最終目的地(可能是另一個消息使用者將獲得的隊列并處理結果)Xml配置:
< ?xml version="1.0" encoding="UTF-8"?> 參考: IdanFridman.com博客上的JCG合作伙伴 Idan Fridman 使用Spring Integration聚合異步結果 。翻譯自: https://www.javacodegeeks.com/2013/06/aggregating-async-results-using-spring-integration.html
總結
以上是生活随笔為你收集整理的使用Spring Integration聚合异步结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海电脑维修24小时(上海电脑维修信息网
- 下一篇: thinkpad电脑关不上怎么办(thi