spring 发送对上一条休息路线的响应

t98cgbkg  于 2023-01-19  发布在  Spring
关注(0)|答案(1)|浏览(154)

我有下面的 Camel 路由,目前这是工作正常。reset-route路由正在调用的休息网址命中。
reset-route工作正常,但如何向rest服务发送正确的响应?
我想发送到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")会被调用多次。

yh2wf1be

yh2wf1be1#

这个问题来自于将你的SplitDefinition转换成WireTapDefinition的wiretap。当你执行.end()时,你不是结束分割,而是结束窃听。这很令人困惑,但它经常发生在camel中。你必须检查你当前的“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");
            }
        };
    }
}

您也可以连续执行both .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");
            }
        };
    }

相关问题