2016-12-14 44 views
0

我想用XML定義一個MessageHandlerChain,然後用它來創建多個獨立的流。我已評估ResequencingMessageHandler,但它不符合此要求。此外,彈簧整合流項目不適合,因爲它使用網關來調用子流。來自XML的MessageHandlerChain作爲模板

鏈模板:

<int:chain id="myChainTemplate"> 
    <int:transformer ref="inputParser"/> 
    <int:service-activator ref="calculator"/> 
    <int:service-activator ref="persister"/> 
    <int:service-activator ref="outputSerializer"/> 
    <int:service-activator ref="forwarder"/> 
</int:chain> 

與每個客戶專用的輪詢創建一個流程實例:

private final Map<String, QueueChannel> flows = new HashMap<>(); 
@Inject 
private BeanFactory factory; 
@Value("${internal.queue.capacity}") 
private int queueCapacity; 
@Value("${internal.poller.interval}") 
private long pollerInterval; 
@Autowired 
@Qualifier("myChainTemplate") 
private MessageHandlerChain chain; 

@ServiceActivator 
public void createFlowForCustomer(@Header("customer") String customer, Message<?> message) { 
    QueueChannel channel = flows.get(customer); 
    if (channel == null) { 
     channel = new QueueChannel(queueCapacity); 
     flows.put(customer, channel); 
     TaskScheduler taskScheduler = IntegrationContextUtils.getTaskScheduler(factory); 

     PollingConsumer poller = new PollingConsumer(channel, chain); 
     poller.setTaskScheduler(taskScheduler); 
     poller.setTrigger(new PeriodicTrigger(pollerInterval)); 
     poller.setBeanFactory(factory); 
    } 
    channel.send(message); 
} 

任何機會,這是可以做到?我是否在代碼中創建和包裝鏈每個處理器?如果我必須在代碼中完成它,是否有公共方法將pojo包裝到MessageHandler中?

回答

0

有關以編程方式創建上下文(在這種情況下適用於多個出站通道適配器)的技術,請參閱dynamic-ftp sample

請參閱dynamic-tcp-client示例(與ftp示例一起),以獲取使用Java DSL的更現代技術。