Apache camel apachecamel以字符串形式获取结果

Apache camel apachecamel以字符串形式获取结果,apache-camel,Apache Camel,我有一个使用骆驼天气服务的简单应用程序。我可以在控制台中记录结果,但我想将结果保存在变量中,比如字符串,这样我就可以在JLabel或其他东西上显示它 我的路线是: from("weather:foo?location=Breda,Netherlands&appid=appid") .to("bean:outPutBean?method=printLn") .to("stream:out"); 此时outputBean只做一个简单的返回。我想在该函数中创建一个JSON解析

我有一个使用骆驼天气服务的简单应用程序。我可以在控制台中记录结果,但我想将结果保存在变量中,比如字符串,这样我就可以在JLabel或其他东西上显示它

我的路线是:

from("weather:foo?location=Breda,Netherlands&appid=appid")
    .to("bean:outPutBean?method=printLn")
    .to("stream:out");
此时outputBean只做一个简单的返回。我想在该函数中创建一个JSON解析器

public class OutputBean {
    public String printLn(String msg){
        return msg;
    }   
}
如何将路由结果保存在变量中,以便稍后使用数据

编辑: 我的代码现在看起来像:

camelContext.addRoutes(new RouteBuilder() {             
    @Override
    public void configure() throws Exception {
        from("weather:foo?location="+ txtCity.getText() + "&mode=xml&units=metric&appid=appid")
        .setHeader("temperature", XPathBuilder.xpath("//temperature/@value", String.class))
        .to("bean:outPutBean?method=printLn")
        .to("stream:out");
    }
});
[HERE]

public class OutputBean {
    public String printLn(@Header("temperature")String temperature){
        return "Output: " + temperature;
    }
}
如何使用[此处]位置的温度变量?

我想这样做

  • 将您想要的信息保存到邮件头中
  • 您可以从输入体或其他地方获取值

  • 在您的bean中,我将接收该头并保存它,然后执行您想要的任何操作
    • 向设置路由的类添加成员变量
    • 将内联处理器添加到路由
    • 在处理器中,从标头中获取所需的字符串并将其存储在变量中

    但是如何在like
    JOptionPane.showMessageDialog(null,value)中获取
    ?只需调用显示以值作为参数的对话框的调用。或者你是什么意思?你现在有了值,只需传递到对话框代码所在的位置。很好,我在bean中得到了地名。但我真正需要的是:
    camelContext.addRoutes(new RouteBuilder(){@Override public void configure()抛出异常{from(“weather:foo?location=“+txtCity.getText()+”&mode=xml&units=IMPERIAL&appid=appid”).setHeader(“cityName”,XPathBuilder.xpath(//city/@name,String.class)).to(“bean:outPutBean?method=printLn”).to(“stream:out”);});showMessageDialog(null,cityName)
    所以我想在路由之后使用cityName。然后你需要将Camel“嵌入”到你的普通java代码中,而不是使用context.addRoutes。。看这里,但你需要使用消费者/生产者模板。嗯,我现在没有得到它,但我会稍后再试。我在我的原始帖子中添加了一些额外的代码,因此它可能更具可读性。
    from("weather:foo?location=Breda,Netherlands&appid=appid")
    .setHeader("myHeader", "myHeaderValue");    
    .to("bean:outPutBean?method=printLn")
    .to("stream:out");
    
    public class OutputBean {
        public String printLn(@Header("myHeader")String header){
            String value = header;
            return value;
        }