【问题标题】:Camel return message from direct route for reuse骆驼从直接路由返回消息以供重用
【发布时间】:2016-08-30 07:49:27
【问题描述】:

我很困惑从骆驼中to() 调用的子路由返回一个“body”。从我对直接路由的发现来看,它们鼓励路由重用,并用于逻辑拆分过于复杂的路由。但我似乎无法做一个最简单的“拆分”:

    from("jms:createRequestQueue")
            .to("direct:createRequest")

            // here the processing of the message fails, see below

            .bean(processor)

            .to("...");

    from("direct:createRequest")
            .onException(Exception.class).bean(requestErrorHandler).stop()
            .unmarshal().json(JsonLibrary.Jackson, MyModelRequest.class);

处理器实例的类如下所示:

public class RequestProcessor {

    @Handler
    public void update(@Body MyModelRequest request) {
        // do stuff
    }

}

问题是解组请求的路由(第二个路由)的结果不会传播回调用路由。抛出异常,表示它无法将 String(进入队列的 JSON)转换为 MyModelRequest 类。因此,似乎第一个路由中的 JSON 正文没有被解组路由的结果替换。我希望这确实是一个不错的路线重用。

我偶然发现了 InOut 消息,但文档非常不清楚,我的实验也以同样的方式失败了。

我需要做什么才能真正将部分路由提取到另一条路由以供重用?

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    所以问题出在 onException 子句中(被排除在原始问题之外,我认为这不是问题)。我将end() 调用与stop() 调用混淆了,因此路由过早停止,返回字符串中未解析的JSON。正确的是:

    from("direct:createRequest")
            .onException(Exception.class).bean(requestErrorHandler).end()
            .unmarshal().json(JsonLibrary.Jackson, MyModelRequest.class);
    

    【讨论】:

    • 啊,就是这样,我知道它不应该是direct 路线:)
    • 是的,我也认为它应该返回正文。感谢您的帮助!
    • 你能接受这个答案,这样问题就不会显示为未回答吗?谢谢!
    • 是的。需要等几个小时然后我忘了。谢谢提醒
    【解决方案2】:

    如果没有可运行的示例来报告错误,很难判断出了什么问题,但它应该(并且确实!在 2.17.2 和 2.15.3 上测试过)工作:

    @Component
    public class DemoRouteBuilder extends RouteBuilder {   
    
      @Override
      public void configure() throws Exception {
        from("timer:sender?delay=10s&period=3s")
            .setBody(constant("{\"title\":\"Lord of the Rings\",\"author\":\"J.R.R. Tolkien\"}"))
            .to("direct:unmarshal")
            .bean(new RequestProcessor())
            .log("${body}!");
    
        from("direct:unmarshal")
            .unmarshal().json(JsonLibrary.Jackson, Book.class);
      }
    
      public static class RequestProcessor {
    
        @Handler
        public void update(@Body Book book) {
          System.out.println("Got " + book);
        }
    
      }
    
      public static class Book {
        private final String title;
    
        private final String author;
    
        private Book() {
          this(null, null);
        }
    
        public Book(String title, String author) {
          this.title = title;
          this.author = author;
        }
    
        public String getTitle() {
          return title;
        }
    
        public String getAuthor() {
          return author;
        }
        @Override
        public String toString() {
          return "title='" + title + "', author='" + author + '\'';
        }
    
      }
    }
    

    这是输出:

    2016-08-30 13:48:34.337 INFO 16240 --- [main] com.example.DemoApplication:在 5.859 秒内启动 DemoApplication(JVM 运行 6.587)

    得到标题='指环王',作者='J.R.R.托尔金

    2016-08-30 13:48:44.298 INFO 16240 --- [ timer://sender] route1 : title='指环王',作者='J.R.R.托尔金!

    得到标题='指环王',作者='J.R.R.托尔金

    2016-08-30 13:48:47.232 INFO 16240 --- [ timer://sender] route1 : title='指环王',作者='J.R.R.托尔金!

    【讨论】:

    • 我可能只是在json方法调用中误删了一个参数。更新的问题。奇怪的是,这对我不起作用。它尝试将带有 JSON 的 String 转换为 @Body MyModelRequest 类。版本是 2.15.3。
    • 没有机会使用更新的版本?这可能是 Camel 中的一个错误。
    • 在 2.15.3 上试过,效果很好。您能否发布一个可运行的代码示例并提供示例 JSON?
    • 所以问题出在其他地方,我认为它是正确的,所以我把它排除在外。看我的回答。谢谢你的时间。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2018-09-02
    • 2015-04-24
    • 2020-06-13
    • 2013-01-24
    相关资源
    最近更新 更多