Spring与Java Dsl集成时的ErrorHandling

时间:2015-05-28 11:43:22

标签: java spring spring-integration spring-dsl

我想知道在SI Java Dsl中使用serviceActivators来处理错误的正确方法是什么。 我有这样的Spring Integration xml配置:

<jms:message-driven-channel-adapter id="messageIn" auto-startup="false"
                                    container="messageNotificationJms" channel="messageChannel"
                                    error-channel="customErrorChannel"/>

<channel id="messageChannel"/>

<chain input-channel="messageChannel">
    <!-- some logic and service-activator -->
</chain>

<channel id="customErrorChannelTpm"/>

<chain input-channel="customErrorChannel" output-channel="nullChannel">
    <service-activator ref="errorService" method="stopEndpoints" />
</chain>

但是现在,我正在尝试使用SI Java DSL配置此上下文,因此我创建了这样的配置

@Configuration
@EnableIntegration
public class SIConfig {

    @Autowired
    AbstractMessageListenerContainer messageNotificationJms;

    @Autowired
    ErrorService errorService;

    @Bean
    public MessageChannel messageChannel() {
        return MessageChannels.direct("messageChannel").get();
    }

    @Bean
    public MessageChannel customErrorChannel() {
        return MessageChannels.direct("customErrorChannel").get();
    }

    @Bean
    public JmsMessageDrivenChannelAdapter messageIn() {
        return Jms.messageDriverChannelAdapter(messageNotificationJms)
                .id("messageIn")
                .autoStartup(false)
                .outputChannel(messageChannel())
                .errorChannel(customErrorChannel())
                .get();
    }


    @Bean
    public IntegrationFlow customErrorFlow() {
        return IntegrationFlows.from(customErrorChannel())
            //.handle () ?????????????????????????
        .get();
    }

    @Bean
    public IntegrationFlow messageFlow() {
        //some logic
    }

}

作为句柄方法参数,我尝试使用像这样的lambda表达式

 .handle(message -> errorService.stopEndpoints(message))

但由于参数类型和返回类型,存在编译错误。

我有一个没有编译错误的非显而易见的变种,但我不确定它是否能在环境中正常工作。这是正确的吗?:

  .<ErrorMessage>handle((payload, headers) -> {
     errorService.stopEndpoints(payload);
     return null;
})

另外,我的ErrorHandler类。

@Component
public class ErrorService implements ErrorHandler {

    @Override
    public void handleError(Throwable t) {
        stopEndpoints(t);
    }

    public void stopEndpoints(ErrorMessage errorMessage) {
        Throwable throwable = errorMessage.getPayload();
        stopEndpoints(throwable);

    }

    private void stopEndpoints(Throwable t) {
        //stoppingEndpoints
    }

}

编辑:我使用Spring Framework 4.1.6,Spring Integration 4.1.3和SI Java DSL 1.0.1

1 个答案:

答案 0 :(得分:3)

您可以使用handle(String beanName, String methodName)变体:

handle("errorService", "stopEndpoints");

handle("errorService", "handleError");

在第二种情况下,框架将负责解开有效载荷。