Spring Integration - transformer and header enricher

戏子无情 提交于 2019-12-22 18:42:03

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/19999528/spring-integration-transformer-and-header-enricher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!