Apache Camel与Json Array分裂

时间:2017-12-13 15:03:48

标签: json apache-camel jbossfuse

我有一个camel应用程序从一个大小为13000的jms队列接收一个json数组请求,json数组请求的结构如下所示。我想用一组5来流式传输和分割json数组。 例如,如果我收到一个大小为100的json数组,我想将其分组为5并将其拆分为20个请求。 是否有内置的camel功能来分组和分割json数组,还是需要编写自定义分割器?

我正在使用camel 2.17版本。

示例json数组:

[{
    "name": "Ram",
    "email": "ram@gmail.com",
    "age": 23
 }, {
    "name": "Shyam",
    "email": "shyam23@gmail.com",
    "age": 28
 }, {
    "name": "John",
    "email": "john@gmail.com",
    "age": 33
 }, {
    "name": "Bob",
    "email": "bob32@gmail.com",
    "age": 41
 }, {
    "name": "test1",
    "email": "test1@gmail.com",
    "age": 41
 }, {
    "name": "test2",
    "email": "test2@gmail.com",
    "age": 41
 }, {
    "name": "test3",
    "email": "test3@gmail.com",
    "age": 41
 }, {
    "name": "test4",
    "email": "test4@gmail.com",
    "age": 41
}]

4 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start")
                .split().jsonpath("$")
                    .streaming()
                    .aggregate(AggregationStrategies.groupedExchange())
                    .constant("true")
                    .completionSize(5)
                    .completionTimeout(1000)
                    .log("${body}")
                .to("mock:result");
        }
    };
}

如果消息的大小不是5,则路由应该在聚合之前等待1秒并继续。使用您的输入,结果将是两条消息,分别包含5个和3个项目:

INFO 5419 --- [           main] route1                                   : List<Exchange>(5 elements)
INFO 5419 --- [eTimeoutChecker] route1                                   : List<Exchange>(3 elements) 

可以查看完整的样本in here

编辑:

根据要求,Spring DSL示例:

<camel:route>
    <camel:from uri="direct:start" />
    <camel:split streaming="true">
        <camel:jsonpath>$</camel:jsonpath>
        <camel:aggregate completionSize="5"
            completionTimeout="1000" groupExchanges="true">
            <camel:correlationExpression>
                <camel:constant>true</camel:constant>
            </camel:correlationExpression>
            <camel:log message="${body}"></camel:log>
            <camel:to uri="mock:result"></camel:to>
        </camel:aggregate>
    </camel:split>
</camel:route>

答案 1 :(得分:0)

这将有效

@Autowired
@EndpointInject(uri = "direct://splitted-queue")
ProducerTemplate producerTemplate;

@Component
  class Router extends RouteBuilder {

    @Override
    public void configure() throws Exception {
      from("direct://direct-queue").split(ExpressionBuilder.languageExpression("jsonpath","$")).convertBodyTo(String.class).process(new Processor() {
        List<String> jsons = new ArrayList<>();

        @Override
        public void process(Exchange exchange) throws Exception {
          jsons.add(exchange.getIn().getBody().toString());
          if(jsons.size() == 5) {
            producerTemplate.sendBody(jsons);
            jsons.clear();
          }
        }
      });
    }  

你需要camel-jsonpath依赖

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jsonpath</artifactId>
        <version>2.19.0</version>
    </dependency>

答案 2 :(得分:0)

试试这个

从( “{{queue.endpoint}}”)

.split()。tokenize(“},”,5)

.log(“来电请求:$ {body}”)

答案 3 :(得分:0)

div {
  display: inline-block;
  position: relative;
}

div::after {
  position: absolute;
  top: 2px;
  right: .5em;
  transition: all .05s ease-in-out;
}

div:hover::after,
div:focus-within::after {
  right: 1.5em;
}

.ms::after {
  content: 'mm';
}

然后在您的路线上

<div class="ms">
  <input type="number" id="milliseconds" />
</div>

享受!