【问题标题】:What is the use of setHeader and recipientList in Apache Camel?Apache Camel 中的 setHeader 和 recipientList 有什么用?
【发布时间】:2018-07-27 07:59:32
【问题描述】:
from("file:src/data?noop=true").to("jms:incomingOrders");

            // content-based router
            from("jms:incomingOrders")
            .choice()
                .when(header("CamelFileName").endsWith(".xml"))
                    .to("jms:xmlOrders")  
                .when(header("CamelFileName").regex("^.*(csv|csl)$"))
                    .to("jms:csvOrders")
                .otherwise()
                    .to("jms:badOrders");        

            from("jms:xmlOrders")
            .setHeader("customer", xpath("/order/@customer"))
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    String recipients = "jms:accounting";
                    //System.out.println("hii1"+recipients);
                    String customer = exchange.getIn().getHeader("customer", String.class);
                    //System.out.println("hii2"+customer);
                    if (customer.equals("honda")) {
                        recipients += ",jms:production";
                        //System.out.println("hii3"+recipients);
                    }
                    exchange.getIn().setHeader("recipients", recipients);
                }
            })
            .recipientList(header("recipients"));

            // test that our route is working
            from("jms:accounting").process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    System.out.println("Accounting received order: " 
                            + exchange.getIn().getHeader("CamelFileName"));   
                }
            });                
            from("jms:production").process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    System.out.println("Production received order: " 
                            + exchange.getIn().getHeader("CamelFileName"));   
                }
            });
        }
    });

输入 XML 格式

<?xml version="1.0" encoding="UTF-8"?>
<order name="motor" amount="1000" customer="honda"/>

我是 Apache Camel 的新手,因此请帮助我在此处指导代码。此代码从 Camel in Action 复制而来,并解释了基于相同 EIP 的收件人列表的使用。因此,在基于 xml 和 csv 进行划分后,它被传递给我正在努力使用

的接收者列表

设置标题

.recipientList(header("recipients"));

因此,如果有人能解释一下流程,将会有所帮助。

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    如果您需要将您的消息广播给多个收件人

    • multicast 是用于一组静态收件人的模式:

    • recipientList 是用于一组动态收件人的模式。在这种情况下,您需要告诉 Camel 在哪里可以找到/如何评估 !在运行时!此收件人列表。有多种表达方式,其中一种是说:“请查看包含确切列表的消息头名称'foo'”。但是当然,您的骆驼路线必须准备/填充(在运行时)标题内容 (在您的示例中完成的人口 进程(新处理器(){...}步骤)

    【讨论】:

    • 但是setHeader到底有什么用??为什么他要添加带有客户名称的标题?它在哪里进一步使用在链中????除此之外 "exchange.getIn().setHeader("recipients", recipients);"做?为什么还要设置标题?
    • @smitshah 标头customer 在Java DSL .setHeader("customer", xpath("/order/@customer")) 中设置并在处理器exchange.getIn().getHeader("customer", String.class) 中使用。而标头recipients 在处理器中设置并在recipientList 中使用。 . 我不明白你有什么困惑。
    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 2017-08-31
    • 2022-11-29
    • 2017-05-19
    • 2013-11-11
    相关资源
    最近更新 更多