Java Reference
In-Depth Information
Modifying a Message's Headers
Sometimes changing a message's payload isn't enough. Sometimes you want to update the
payload as well as the headers. Doing this is slightly more interesting because it involves using the
MessageBuilder class, which allows you to create new Message objects with any specified payload and
any specified header data. The XML configuration is identical in this case.
package com.apress.springenterpriserecipes.springintegration;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageBuilder;
import java.util.Map;
public class InboundJMSMessageToCustomerWithExtraMetadataTransformer {
public Message<Customer> transformJMSMapToCustomer(
Message<Map<String, Object>> inboundSpringIntegrationMessage) {
Map<String, Object> jmsMessagePayload =
Customer customer = new Customer();
customer.setFirstName((String) jmsMessagePayload.get("firstName"));
customer.setLastName((String) jmsMessagePayload.get("lastName"));
customer.setId((Long) jmsMessagePayload.get("id"));
return MessageBuilder.withPayload(customer)
.copyHeadersIfAbsent( inboundSpringIntegrationMessage.getHeaders())
.setHeaderIfAbsent("randomlySelectedForSurvey", Math.random() > .5)
As before, this code is simply a method with an input and an output. The output is constructed
dynamically using MessageBuilder to create a message that has the same payload as the input message
as well as copy the existing headers, and adds an extra header: randomlySelectedForSurvey .
8-7. Error Handling Using Spring Integration
Spring Integration brings together systems distributed across different nodes, computers, and
services/protocol/language stacks. Indeed, a Spring Integration solution might not even finish in
remotely the same time period as when it started. Exception handling, then, can never be as simple as a
language-level try/catch block in a single thread for any component with asynchronous behavior. This
implies that many of the kinds of solutions you're likely to build, with channels and queues of any kind,
need a way of signaling an error that is distributed and natural to the component that created the error.
Thus, an error might get sent over a JMS queue on a different continent, or in process, on a queue in a
different thread.
Search WWH ::

Custom Search