Using filter with a discard channel in Spring Integration DSL

Viewed 2600

I don't know if this question is about spring-integration, spring-integration-dsl or both, so I just added the 2 tags...

I spend a considerable amount of time today, first doing a simple flow with a filter

StandardIntegrationFlow flow = IntegrationFlows.from(...)
                    .filter(messagingFilter)
                    .transform(transformer)
                    .handle((m) -> {
                        (...)
                    })
                    .get();

The messagingFilter being a very simple implementation of a MessageSelector. So far so good, no much time spent. But then I wanted to log a message in case the MessageSelector returned false, and here is where I got stuck.

After quite some time I ended up with this:

StandardIntegrationFlow flow = IntegrationFlows.from(...)
                    .filter(messagingFilters, fs -> fs.discardFlow( i -> i.channel(discardChannel()))
                    .transform(transformer)
                    .handle((m) -> {
                        (...)
                    })
                    .get();

(...)

public MessageChannel discardChannel() {
    MessageChannel channel = new MessageChannel(){
        @Override
        public boolean send(Message<?> message) {
            log.warn((String) message.getPayload().get("msg-failure"));
            return true;
        }
        @Override
        public boolean send(Message<?> message, long timeout) {
            return this.send(message);
        }
    };
    return channel;
}

This is both ugly and verbose, so the question is, what have I done wrong here and how should I have done it in a better, cleaner, more elegant solution?

Cheers.

1 Answers
Related