Spring Integration - 转换器和标题丰富器 [英] Spring Integration - transformer and header enricher

查看:35
本文介绍了Spring Integration - 转换器和标题丰富器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是这样的:我需要根据邮政编码将消息路由到三个不同的商店.

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.

这可能吗?

推荐答案

你说得对!您可以使用 SpEL:

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>

这篇关于Spring Integration - 转换器和标题丰富器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆