Bujaka
Bujaka

Reputation: 105

ErrorHandling in Spring Integration with Java Dsl

I'm wonderring what is the correct way to use serviceActivators in SI Java Dsl to handle errors. I have such Spring Integration xml config:

<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>

But now, I'm trying to use SI Java DSL to configure this context, so I create such config

@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
    }

}

As a handle method parameters I tried to use lambda expressions like this

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

but there was compilation error because of parameter types and return type.

I have one non-obvious variant without compilation errors, but I'm not sure that it will work correctly on environment. Is it correct?:

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

Also, my ErrorHandler class.

@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
    }

}

EDIT: I use Spring Framework 4.1.6, Spring Integration 4.1.3 and SI Java DSL 1.0.1

Upvotes: 4

Views: 2924

Answers (1)

Gary Russell
Gary Russell

Reputation: 174484

You can use the handle(String beanName, String methodName) variant:

handle("errorService", "stopEndpoints");

or

handle("errorService", "handleError");

In the second case, the framework will take care of unwrapping the payload.

Upvotes: 4

Related Questions