我定义了两个使用这个组件的集成流。一个从 ftp 读取,一个从磁盘读取文件。
@Bean
public IntegrationFlow csvLineFlowDefinition() {
return IntegrationFlows.from(CHANNEL_NAME)
.filter(String.class, m -> {
// filter to remove column definition csv line
return !m.startsWith("ID");
})
.<String, MyPrettyObject>transform(csvLinePayload -> {
String[] array = csvLinePayload.split(",");
MyPrettyObject myPrettyObject = new MyPrettyObject();
myPrettyObject.setId(array[0]);
myPrettyObject.setType(array[1]);
return myPrettyObject;
})
.<MyPrettyObject, String>route(myPrettyObject -> myPrettyObject.getType(),
routeResult -> routeResult
.channelMapping("AA", "AA_CHANNEL")
.channelMapping("BB", "BB_CHANNEL")
.channelMapping("CC", "CC_CHANNEL"))
.get();
}
如果从 ftp 读取或从磁盘读取文件出现问题,我希望这两个 IntegrationFlows 失败。他们定义了自己的错误通道。我不希望在将 csv 行转换为 MyPrettyObject 以达到这两个 IntegrationFlow 时出现错误。
我曾考虑将原始 csv 行分派到消息队列,然后我可以在此消息队列的入站使用者上定义特定的错误通道。
然而,这似乎有点矫枉过正。
我试图为转换器插入一个 ExpressionEvaluatingRequestHandlerAdvice,但我不确定如何正确使用它,并且消息没有到达路由器或 ERROR_CHANNEL_NAME
@Bean
public ExpressionEvaluatingRequestHandlerAdvice csvLineTransformerAdvice() {
ExpressionEvaluatingRequestHandlerAdvice expressionEvaluatingRequestHandlerAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
expressionEvaluatingRequestHandlerAdvice.setFailureChannelName(ERROR_CHANNEL_NAME);
expressionEvaluatingRequestHandlerAdvice.setTrapException(true);
return expressionEvaluatingRequestHandlerAdvice;
}
.<String, MyPrettyObject>transform(csvLinePayload -> {
String[] array = csvLinePayload.split(",");
MyPrettyObject myPrettyObject = new MyPrettyObject();
myPrettyObject.setId(array[0]);
myPrettyObject.setType(array[1]);
return myPrettyObject;
}, t -> t.advice(csvLineTransformerAdvice()))