My case is like this: I need to route a message based on the zipcode to three different stores.
For that I need to look at the message header to find the customer's zipcode, and do the following calculation:
if(zip < 5000)
{
store = "SJ";
}
else if(zip >= 6000)
{
store = "JY";
}
else
{
store = "FY";
}
I have managed to get it done using the following custom Transformer which I use to enrich the message header:
public class HeaderEnricher {
public Message<?> transform(Message<?> message)
{
int zip = message.getHeaders().get("Customer Zip", Integer.class);
String store;
if (zip < 5000)
{
store = "SJ";
}
else if (zip >= 6000)
{
store = "JY";
}
else
{
store = "FY";
}
Message<?> messageOut = MessageBuilder
.withPayload(message.getPayload())
.copyHeadersIfAbsent(message.getHeaders())
.setHeaderIfAbsent("store", store).build();
return messageOut;
}
}
As I said this is working, but I was wondering how to do the same using a header-enricher. I'm asking because I would like my integration-graph to illustrate it as a header-enricher, because that is my intention with the above transformer-code.
Is that possible?
You are right! You can do it without any Java code using SpEL:
<int:header-enricher input-channel="inputChannel" output-channel="outputChannel">
<int:header name="store"
expression="headers['Customer Zip'] lt 5000 ? 'SJ' : headers['Customer Zip'] ge 6000 ? 'JY' : 'FY'"/>
</int:header-enricher>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With