liveBook · Manning

Handler

As explained in Spring in Action, Sixth Edition

A Spring Integration handler contract for processing an incoming message.

In a Spring Integration flow, a handler processes a message after it reaches a channel. A service activator can send that message to a MessageHandler. When processing should produce a payload for another channel, use a GenericHandler instead. section 10.2.6

Choose the contract by what the flow needs next:

  • MessageHandler receives the whole Message. A small lambda can read its payload and print it. section 10.2.6
  • GenericHandler receives a typed payload and message headers. It can return a saved order for an output channel; the example does not use the headers. section 10.2.6
  • Both can be supplied to IntegrationFlows.handle() instead of declared as a service activator bean. A MessageHandler lambda receives a message, not just its payload. section 10.2.6
@Bean
@ServiceActivator(inputChannel="orderChannel",
                  outputChannel="completeChannel")
public GenericHandler<EmailOrder> orderHandler(
                             OrderRepository orderRepo) {
  return (payload, headers) -> {
    return orderRepo.save(payload);
  };
}

Here, @ServiceActivator connects orderChannel to a GenericHandler<EmailOrder>. The handler saves the payload and returns the saved order for completeChannel. section 10.2.6

Finish an email-to-order flow

An email transformer produces a message carrying an EmailOrder. A GenericHandler implementation receives the order, uses an injected RestTemplate to POST it to a configured API URL, and returns null to mark the end of the flow. This contrasts with returning a saved order when another channel should receive the result. section 10.3 section 10.2.6

Listing 10.7 Posting orders to the Taco Cloud API via a message handler
package tacos.email;
 
import org.springframework.integration.handler.GenericHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
 
@Component
public class OrderSubmitMessageHandler
       implements GenericHandler<EmailOrder> {
 
  private RestTemplate rest;
  private ApiProperties apiProps;
 
  public OrderSubmitMessageHandler(ApiProperties apiProps, RestTemplate rest) {
    this.apiProps = apiProps;
    this.rest = rest;
  }
 
  @Override
  public Object handle(EmailOrder order, MessageHeaders headers) {
    rest.postForObject(apiProps.getUrl(), order, String.class);
    return null;
  }
}

The complete handler shows the handle(EmailOrder, MessageHeaders) method, constructor-injected dependencies, REST call, and null return. section 10.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Works through lambda and class implementations, channels, headers, and return values without examining framework internals.
Examples
Demonstrates payload logging, repository-backed order saving, and posting email-derived orders to an API.
What it assumes
Introduces the handler contracts through Java lambdas and Spring beans while assuming familiarity with basic Java syntax.
What you can do after
Apply it via @ServiceActivator or IntegrationFlows.handle() to process a message and optionally forward a result.
How it connects
Connects message channels and service activators to GenericHandler, transformers, REST submission, and Java DSL integration flows.

Also explained in

Compare how 22 books explain handler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.