Going Bananas
Going Bananas

Reputation: 2537

Spring Integration Java DSL - @ServiceActivator method with @Header parameter annotations

I have a Spring Integration 4 bean method with the following signature:

@Component
public class AService {

    @ServiceActivator
    public Message<?> serviceMethod(
            Message<?> message,
            @Header(ServiceHeader.A_STATE) AState state,
            @Header(ServiceHeader.A_ID) String id) {

        ...
    }

    ...
}

At the moment, I call this service method from within a Spring Integration Java DSL (spring-integration-java-dsl:1.0.1.RELEASE) flow like this:

.handle("aService", "serviceMethod")

This works absolutely fine but, I am wondering whether it is possible to call the service method in the following sort of way:

.handle(Message.class, (m, h) -> aService.serviceMethod(m, h))

The reason I would like to call the service method in this sort of manner is so that, when someone is looking at the code using an IDE such as Eclipse, they can drill into that service's method by for example highlighting the method and pressing F3.

So my question is, is there an alternative way to calling a @ServiceActivator method (which includes @Header annotations) without using strings for service name/service method in a .handle()?

Upvotes: 4

Views: 2314

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

Not exactly, you can't pass the whole message and selected headers, but you can pass the payload and individual headers...

.handle(String.class, (p, h) -> aService().serviceMethod(p, 
     (AState) h.get(ServiceHeader.A_STATE),
     (String) h.get(ServiceHeader.A_ID)))

(assuming the payload is a String).

Note that the @Header annotations are meaningless in this scenario because you are directly pulling out the headers in the lambda expression.

It also doesn't need to be annotated as a @ServiceActivator; you can invoke any bean method that way.

public Message<?> serviceMethod(
        String payload, AState state, String id) {

    ...
}

The

.handle("aService", "serviceMethod")

variation is where all the magic happens (matching the message contents to the method parameters). Of course, we can't do any "magic" when you want to invoke the method directly in Java.

Upvotes: 5

Related Questions