【问题标题】:Getting a Return Value from a Bean in Camel (Java)从 Camel (Java) 中的 Bean 获取返回值
【发布时间】:2017-04-03 22:46:27
【问题描述】:

我在从骆驼的 bean 中获取返回值并在我的路线中使用它时遇到了一些麻烦。

我的路线如下所示:

from(file:test/?delete=true)
.unmarshal(jaxb)
.bean(testBean, "testMethod")
.to(direct:nextRoute);

bean 看起来像这样:

public void testBean (PojoName pojoInstance){
  //do stuff 
  int i= 75; //a number I generate within the bean after I've started this method
}

我想使用我在 bean 内部和路由中生成的数字。像这样的:

from(file:test/?delete=true)
    .unmarshal(jaxb)
    .bean(testBean, "testMethod")
    .log(integer generated from testBean)
    .to(direct:nextRoute); 

我尝试了什么:
因此,我没有在我的 bean 中返回 void,而是将返回类型更改为 int 并返回整数。然后,我希望在我的路线中做这样的事情:

.log("${body.intFromBean}")

我的想法是,一旦我从 bean 返回值,它应该将该值存储在交换体中(至少这是我从 Camel 文档中得到的)。然后,我可以在我的路由中访问它。

问题:
但是,当我将 testBean 返回类型更改为 int 时,出现以下错误:

org.apache.camel.CamelExecutionException: Execution occurred during execution on the exchange 
Caused by: org.apache.camel.InvalidPayloadException: No body available of type: PojoName but has value: numberIGenerated of type java.lang.Integer

(抱歉,我没有完整的堆栈跟踪。我正在使用 s.o 移动应用)

我的问题:
从阅读其他一些s.o.提交,我想我理解这个问题。消息体是一种类型,返回类型是另一种。但是,即使我尝试使用 .

.convertTo(Integer.class)

在调用 bean 之前,但这也不起作用。 (从概念上讲,这也行不通,因为如果我在解组后立即将其转换为 int,我将无法使用解组后的数据。但我想我还是会尝试一下)。

有人可以帮助我了解如何正确返回整数并在我的路由中使用它吗?

我已阅读有关 bean 绑定和交换的文档,并且我认为我对它的理解足以做到这一点。但我一定是错过了什么。

【问题讨论】:

  • 完整的堆栈跟踪和 bean 的完整主体(包括 testMethod 实现)等会有所帮助,完整的路线也会有所帮助,testBean 定义在哪里等等。这对我来说也很不确定: ${body.intFromBean} 来自您的日志语句,但需要更多信息...
  • 你试过用 .log("${body}") 代替 ${body.intFromBean} 吗?骆驼将结果存储为您的消息正文。

标签: java variables apache-camel


【解决方案1】:

我认为更简单的解决方案是:

public class TestBean {
     public int testMethod() {
        return 75;
    }
}

是否要将返回结果存储在 header 或 body 中,应该由路由定义决定。

正如您在Camel documentation 中所读到的,默认行为是在正文中设置返回值:

TestBean testBean = new TestBean();

from("file:test/?delete=true")
    .unmarshal(jaxb)
    .bean(testBean, "testMethod")
    .log("${body}")
    .to("direct:nextRoute"); 

如果你想要它在标题中:

TestBean testBean = new TestBean();

from("file:test/?delete=true")
    .unmarshal(jaxb)
    .setHeader("MyMagicNumber", method(testBean, "testMethod"))
    .log("${header.MyMagicNumber}")
    .to("direct:nextRoute"); 

请注意,如果您使用早于 2.10 的 Camel 版本,则需要使用(现已弃用)“bean”方法而不是“method”方法:-)

【讨论】:

    【解决方案2】:

    根据您需要使用它,您可以将其添加到标题中,也可以将其设为正文。

    要将其添加到标题(键/值),请执行以下操作:

    public class TestBean
    {
         @Handler 
         public void testMethod
         (
            @Body Message inMessage,
            @Headers Map hdr,
            Exchange exch
         ) throws Exception
        {
            int i= 75;
            hdr.put("MyMagicNumber", i);
    
        }
    
    }
    

    您的“返回”结果现在存储在标题中,您可以通过以下步骤从那里读取它。

    对于正文,请执行以下操作:

    public class TestBean
    {
         @Handler 
         public void testMethod
         (
            @Body Message inMessage,
            @Headers Map hdr,
            Exchange exch
         ) throws Exception
        {
            int i= 75;
            inMessage.setBody(i);
    
        }
    
    }
    

    消息的正文现在将包含 i。

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多