Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Integration - transformer and header enricher

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?

like image 809
Emil C Avatar asked Feb 14 '23 16:02

Emil C


1 Answers

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>
like image 122
Artem Bilan Avatar answered Feb 17 '23 21:02

Artem Bilan