Search code examples
spring-integrationspring-integration-dsl

Spring Integration - Service Activator in Java DSL


I have a service activator configured in a simple POJO and I want to convert it into Java DSL.

Right now, my Java DSL looks like this,

public IntegrationFlow inputFlow() {
    return IntegrationFlows.from(inputChannel())
            .log(LoggingHandler.Level.DEBUG, "com.dash.messages")
            .transform(Transformers.fromJson(MessageWrapper.class, customObjectMapper()))
            .channel(theOtherChannel()))
            .get();
  }

There is a POJO with a service activator in it,

public class MessageProcessor {

  private static final Logger logger = LoggerFactory.getLogger(MessageProcessor.class);

  ....

  @ServiceActivator
  public void handle(MessageWrapper message, @Headers Map<String, Object> headers) {
    logger.debug("Message received: " + message);
    
    // Send message to another system
    ....
  }

}

In XML, the corresponding configuration is as shown below,

<int:service-activator input-channel="theOtherChannel"
        ref="MessageProcessor" output-channel="nullChannel" />
  1. How do I invoke the ServiceActivator method in Java DSL? I am thinking of using .handle(), but what should be the argument(s)?
  2. Is there any concept of a null channel when using Java DSL? If yes, how do we specify it?

Solution

  • This this handle() variant:

    /**
     * Populate a {@link ServiceActivatingHandler} for the
     * {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
     * to invoke the discovered {@code method} for provided {@code service} at runtime.
     * @param service the service object to use.
     * @return the current {@link BaseIntegrationFlowDefinition}.
     */
    public B handle(Object service) {
    

    So, you just need to autowire your MessageProcessor into the class with that IntegrationFlow definition or directly to that inputFlow() bean definition. then you do just this:

    .channel(theOtherChannel()))
    .handle(messageProcessor)
    .get();
    

    There is no need in nullChannel for service activator with void return type. The flow is just going to stop at that point anyway when it encounters with void or null reply.

    See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-handle