Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create MessageSelector filter in Spring Integration Java DSL?

I tried to create a message filter via lambda expression to filter messages on a base of header value evaluation

IntegrationFlows.from(inputChannel())
    .filter((Message<?> m) -> { return m.getHeaders().get(...)...; })
    .transform(...)
    .channel(outputChannel())
    .get();

but got exception

Caused by: java.lang.ClassCastException: com.<...skipped...>.BusinessPayloadData cannot be cast to org.springframework.messaging.Message

Only this definition works for me

IntegrationFlows.from(inputChannel())
    .filter(new MessageSelector(){
        @Override
        public boolean accept(Message<?>message){
            return ...;
        }
    })
    .transform(...)
    .channel(outputChannel())
    .get();

Is it possible to create MessageSelector instance via lambda expression?

like image 948
Andriy Kryvtsun Avatar asked Feb 08 '23 05:02

Andriy Kryvtsun


1 Answers

Like this:

.filter(Message.class, m -> m.getHeaders().get(...)...)

There is one more overloaded method to infer the object type for the target method invocation.

See LambdaMessageProcessor source code. Since we can't infer the argument type from the Lambda directly Java: get actual type of generic method with lambda parameter, we have to do that like with one more top level type parameter.

Otherwise it ends up only like Object, which the target method invocation treats like a type for the payload.

like image 200
Artem Bilan Avatar answered Feb 16 '23 00:02

Artem Bilan