Camel Processor没有设置标头

时间:2018-01-15 17:56:37

标签: apache-camel spring-camel

我无法使用下面的spring DSL配置在处理器中注入/修改标头。你能帮忙搞清楚我做错了吗?

    <routeContext xmlns="http://camel.apache.org/schema/spring"
    id="routes1">

    <route id="sdPoll" de:name="Polling"
        de:systemName="Polling" streamCache="true">
        <from uri="timer://sdPoll?fixedRate=true&amp;period=60000" />
        <process ref="refProcessor" />
        <to uri="http://dummyhost" />
        <to uri="log:DEBUG?showBody=true&amp;showHeaders=true" />
    </route>
</routeContext>
<bean id="refProcessor"
    class="com.abc.de.RefProcessor" />

处理器类

public class RefProcessor implements Processor {

private final Logger log = Logger.getLogger(RefProcessor.class);

@SuppressWarnings("unchecked")
@Override
public void process(Exchange exchange) throws Exception {

    exchange.getIn().setHeader("Authorization", "TODO");
    exchange.getIn().setHeader("CamelHttpMethod", "POST");
    exchange.getIn().setHeader("CamelHttpUri", "http://localhost:8280/api/check");
    exchange.getIn().setHeader("Content-Type", "application/json");
    exchange.getIn().setHeader("Accept", "application/json");
    exchange.getIn().setBody("TODO");

    //exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    //exchange.getOut().setHeader("Authorization", "TODO");
    //exchange.getOut().setBody("TODO");
 }
}

日志 - 消息历史 RouteId ProcessorId处理器已用完(ms) [sdPoll] [sdPoll] [计时器:// sdPoll?fixedRate = true&amp; period = 60000] [21176] [null] [onCompletion1] [onCompletion] [106] [sdPoll] [process7] [ref:refProcessor] [21067] [null] [process3] [ref:GenericErrorHandle] [21016]

交换[         Id ID-ABC-63143-1516034486954-0-2         ExchangePattern InOnly         标题{breadcrumbId = ID-ABC-63143-1516034486954-0-1,CamelRedelivered = false,CamelRedeliveryCounter = 0,firedTime = Mon Jan 15 11:41:31 EST 2018}         BodyType null         正文[正文为空] ]

Java DSL似乎可以工作!那么我的Spring DSL配置有什么问题

static RouteBuilder createRouteBuilder3() {
    return new RouteBuilder() {
        public void configure() {
            from("timer://timer1?period=60000").process(new Processor() {
                public void process(Exchange exchange) throws UnsupportedEncodingException {
                    exchange.getIn().setHeader("CamelHttpMethod", "POST");
                    exchange.getIn().setHeader("Content-Type", "application/json");
                    exchange.getIn().setHeader("Accept", "application/json");
                    exchange.getIn().setHeader("CamelHttpUri",
                            "http://localhost:8280/api/check");
                    exchange.getIn().setHeader("Authorization", "TODO");

                    exchange.getIn().setBody("TODO");
                }
            }).to("http://dummyhost").to("log:DEBUG?showBody=true&showHeaders=true");
        }
    };
}

消息历史记录 RouteId ProcessorId处理器已用完(ms) [route1] [route1] [timer:// timer1?period = 60000] [86] [route1] [process1] [RefProcessorCamel $ 3 $ 1 @ 258e2e41] [6] [route1] [to1] [http://dummyhost] [76]

交换[     Id ID-ABC-63940-1516036107063-0-2     ExchangePattern InOnly     标题{Accept = application / json,Authorization = TODO,breadcrumbId = ID-ABC-63994-1516036220042-0-1,CamelHttpMethod = POST,CamelHttpUri = http://localhost:8280/api/check,CamelRedelivered = false,CamelRedeliveryCounter = 0,内容-Type = application / json,firedTime = Mon Jan 15 12:10:21 EST 2018}     BodyType字符串     身体TODO ]

1 个答案:

答案 0 :(得分:0)

您的处理器看起来不错。

只是猜测,但您是否故意在XML配置中使用routeContext?如果没有,您可以尝试切换到camelContext吗?

有关routeContextcamelContext

之间的区别,请参阅http://people.apache.org/~dkulp/camel/configuring-camel.html