发送回复到先前的休息路线

时间:2019-04-26 09:29:28

标签: apache-camel

我有以下骆驼路线,目前可以正常使用。 reset-route路由正在由其他URL命中调用。

reset-route正常工作,但是如何发送适当的响应给休息服务。

我要发送到Rest URL的响应是发送到MQService的消息计数,如果路由进入doCatch,我想以字符串形式发送“ ERROR”。

我目前一无所知,因为在reset-route开始时,它正在调用Java bean并获取要拆分和并行处理的对象列表。 如何计算成功发送到MQ的消息? 拆分的终点在哪里? 拆分后是否应该使用事务进行后期处理?

@Override
public void configure() throws Exception {
    logger.info("ResetRoute.configure() : tp started configuring camel route.");

    restConfiguration()
            .enableCORS(false)
            .apiContextPath("/api-doc")
            .apiProperty("api.title", "REST Api to Trigger Swap Reset Tranformation.")
            .apiProperty("cors", "false")
            .apiContextRouteId("doc-api")
            .component("servlet")
            .dataFormatProperty("prettyPrint", "true");

    rest("/api/")
    .id("reset-api-route")
    .get("/reset")
    .to("direct:resetRoute");

    from("direct:resetRoute").routeId("reset-route")
            .log("Starting reset route")
            .doTry()
                .process(resetProcessor)
                .bean(transactionManager, "beginTransaction()")
                .bean(clientApi, "getResetTransactions(${body},${header.region})")
                .setHeader("transactionSize", simple("${body.size()}"))
                .choice()
                    .when(header("transactionSize").isLessThan(1))
                    .bean(transactionManager, "markSuccess")
                    .stop()
                .end()  //End of choose()
                    .wireTap("{{tp.reset.auditDir}}" + "${header.inXmlFileName}").onPrepare(jacksonProcessor)
                .split(simple("${body}"))
                    .parallelProcessing()
                    .bean(transformationService, "tranform")
                        .wireTap("{{tp.reset.auditDir}}" + "${header.outXmlFileName}").onPrepare(jacksonProcessor)
                    .process(jacksonProcessor).convertBodyTo(String.class)
                    .bean(mqService, "send")
                .end() //End of Split()
                .bean(transactionManager, "markSuccess")
                .transform().constant("SUCCESS")
            .endDoTry() //End of doTry()
            .doCatch(Exception.class)
                .bean(transactionManager, "markFailure").transform().constant("ERROR")
                .log(LoggingLevel.ERROR, "EXCEPTION: ${exception.stacktrace}")
            .end(); //End of doCatch()
}


This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
  

拆分结束

.split(simple("${body}"))
    .parallelProcessing()
    .bean(transformationService, "tranform")
        .wireTap("{{trade-publisher.reset.auditDir}}" + "${header.outXmlFileName}").onPrepare(jacksonProcessor)
    .process(jacksonProcessor).convertBodyTo(String.class)
    .bean(mqService, "send")
.end() //End of Split()
.bean(transactionManager, "markSuccess")
.transform().constant("SUCCESS")

我给人的印象是,在行.end() //End of Split()之后,无论我调用什么,都只会被调用一次,但就我而言,.bean(transactionManager, "markSuccess")被多次调用。

1 个答案:

答案 0 :(得分:1)

问题来自于窃听,窃听将您的SplitDefinition转换为WireTapDefinition。当您执行.end()时,您不会结束拆分而是窃听。这很令人困惑,但是骆驼经常发生这种情况。您必须检查什么是当前的“ xxxDefinition”。就您而言,将“ .end()”加倍应该可以解决问题。

您可以玩这个小例子:

package com.example.camel;

import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.language.tokenizer.TokenizeLanguage;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;


public class CamelApplicationTests extends CamelTestSupport {

    @Test
    public void endingSplitTest() throws InterruptedException {
        template().sendBody("direct:start", "1,2,3");
        getMockEndpoint("mock:inner").expectedMessageCount(3);
        getMockEndpoint("mock:outer").expectedMessageCount(1);
        getMockEndpoint("mock:outer").setAssertPeriod(1000);
        assertMockEndpointsSatisfied();
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                    .split(TokenizeLanguage.tokenize(","))
                    .wireTap("mock:wiretap")
                    .end() // Ending WireTapDefinition
                    .log("Nested route : ${body}")
                    .to("mock:inner")
                    .end() // Ending SplitDefinition
                    .log("Outer route : ${body}")
                    .to("mock:outer");
            }
        };
    }
}

您也可以连续地同时执行两个.end(),但对我来说看起来有点糟。

 @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                    .split(TokenizeLanguage.tokenize(","))
                    .wireTap("mock:wiretap")
                    .log("Nested route : ${body}")
                    .to("mock:inner")
                    .end()
                    .end()
                    .log("Outer route : ${body}")
                    .to("mock:outer");
            }
        };
    }