简体   繁体   English

组件的Apache Camel默认数据格式

[英]Apache Camel default data format for a component

How to set default data format for a component in Apache Camel? 如何在Apache Camel中为组件设置默认数据格式?

I have a number of routes interacting with different ActiveMQ queues. 我有许多与不同的ActiveMQ队列交互的路由。 At the moment all of them look like 目前,他们所有人看起来像

from("...")
    .process(...)
    .marshal().json() // (1)
    .to("activemq:queue:...");

or 要么

from("activemq:queue:...")
    .unmarshal().json() // (2)
    .process(...)
    .to("...");
  1. I would like to replace lines (1) and (2) with either component or context level configuration. 我想用组件或上下文级别配置替换第(1)和(2)行。 Basically saying only once 'message payload going through ActiveMQ has to be a JSON string'. 基本上只说一次“通过ActiveMQ的消息有效负载必须是JSON字符串”。
  2. I don't like to add any additional routes, processors, headers, URI parameters, etc. 我不喜欢添加任何其他路由,处理器,标头,URI参数等。
  3. Ideally, it would be applicable for other components and formats besides ActiveMQ and JSON 理想情况下,它将适用于ActiveMQ和JSON之外的其他组件和格式

You could marshall/unmarshall using a named reference to a data format that you can define once (here as "myDefaultFormat") in your Camel Registry: 您可以使用对可在Camel注册表中定义一次的数据格式 (此处为“ myDefaultFormat”) 的命名引用进行编组/解组:

from("activemq:queue:...")
.unmarshal(myDefaultFormat)

This way, you dont have to repeat .json() everywhere (but ok, you have yet to repeat the named reference :-$ ) 这样,您不必到处重复.json()(但是,您还没有重复命名的引用:-$)

Using interceptors (Based on Claus Ibsen's comment) 使用拦截器(基于克劳斯·易卜生的评论)

new RouteBuilder() {
    @Override
    public void configure() throws Exception {

        // interceptors
        interceptSendToEndpoint("activemq:queue:*")
            .marshal()
            .json();

        interceptFrom("activemq:queue:*")
            .unmarshal()
            .json();

        // routes
        from("...")
            .process(...)
            .to("activemq:queue:...");

        from("activemq:queue:...")
            .process(...)
            .to("...");
    }
}

Notes: 笔记:

  1. interceptors have to be defined before any routes in RouteBuilder . 必须在RouteBuilder任何路由之前定义拦截器。 Otherwise IllegalArgumentException is thrown explaining situation. 否则, IllegalArgumentException来解释情况。
  2. interceptors are not shared and have to be defined in each RouteBuilder . 拦截器不是共享的,必须在每个RouteBuilder定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM