java camel <from uri_java – 如何将Camel属性加载到Bean中?

我一直在阅读关于Camel属性的以下页面:

http://camel.apache.org/using-propertyplaceholder.html并且还阅读了“Camel In Action”一书.

我发现“Camel In Action”的第6章在定义Camel属性方面非常有用,我可以从config.properties加载以下三个属性:

config.timeout=10000

config.numSamples=1000

config.defaultViz=a

当我运行我的Java代码时,我能够在applicationContext.xml中的camel路由中看到以下三个值,如下面的线程#0消息所示:

14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - printing values read from config.properties file

14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.timeout= 10000

14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.numSamples= 1000

14670 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO route1 - config.defaultViz= a

但是,当我尝试将变量{{config.defaultViz}}传递给我的SensorGenerator Java类中名为defaultViz的String,并打印该字符串时,我在控制台上获得“{{config.defaultViz}}”而不是包含的值在{{config.defaultViz}}内.

换句话说,这就是我在屏幕上看到的内容:

Returning List

defaultViz= {{config.defaultViz}}

但我真的想在屏幕上看到这个:

Returning List

defaultViz=a

那么我在applicationContext.xml中做错了什么?

更新:问题是我需要在Spring和Camel之间添加一个Bridge,如上面引用的链接中所述.

这是我的UPDATED applicationContext.xml和桥:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd

http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

uri="timer://hello.world.request.timer?fixedRate=true&period={{config.timeout}}" />

class="org.apache.camel.component.properties.PropertiesComponent">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 使用 Apache Camel,你可以通过编写一个 CamelConfiguration 类来配置和启动你的 CamelContext。在这个类,你可以定义路由器、处理器和其他相关组件,并将它们添加到 CamelContext 。 如果你想动态启停 Spring XML 路由,你可以使用 CamelContext 的 addRoutes 和 removeRoute 方法来添加和删除路由。下面是一个简单的示例,演示如何在 Spring Boot 动态启停 Spring XML 路由。 首先,你需要在你的 Spring Boot 项目添加 Apache Camel 的相关依赖,比如 Maven 依赖: ``` <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>x.x.x</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>x.x.x</version> </dependency> ``` 接下来,你需要编写一个 CamelConfiguration 类,来配置和启动你的 CamelContext。这里是一个简单的示例: ``` @Configuration public class CamelConfiguration { @Autowired private ApplicationContext applicationContext; @Bean public CamelContext camelContext() throws Exception { DefaultCamelContext context = new DefaultCamelContext(); context.addRoutes(new SpringRouteBuilder() { public void configure() throws Exception { from("file:inbox").to("file:outbox"); } }); context.start(); return context; } @Bean public CamelSpringBootApplicationController camelController() { return new CamelSpringBootApplicationController(applicationContext); } } ``` 在这个示例,我们定义了一个 CamelContext,并添加了一个从 inbox 目录读取消息并将其写入到 outbox 目录的路由。我们还定义了一个 CamelSpringBootApplicationController,它用于动态启停路由。 接下来,你需要编写一个 Spring XML 路由,比如 routes.xml。在这个文件,你可以定义更多的路由和处理器。这里是一个简单的示例: ``` <?xml version="1.0" encoding="UTF-8"?> <routes xmlns="http://camel.apache.org/schema/spring"> <route id="file-route"> <from uri="file:inbox"/> <to uri="file:outbox"/> </route> </routes> ``` 在这个示例,我们定义了一个从 inbox 目录读取消息并将其写入到 outbox 目录的路由。 最后,你需要在你的启动类添加 @ImportResource 注解,并指定 Spring XML 路由的路径。这样,Spring Boot 就会自动加载这个 XML 路由,并将其添加到 CamelContext 。这里是一个简单的启动类示例: ``` @SpringBootApplication @ImportResource("classpath:routes.xml") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 现在,你可以使用 CamelSpringBootApplicationController 的 addRoute 和 removeRoute 方法来动态启停 Spring XML 路由了。比如,你可以在任何时候添加一个新的路由: ``` @Autowired private CamelSpringBootApplicationController camelController; camelController.addRouteBuilder(new SpringRouteBuilder() { public void configure() throws Exception { from("timer:new-route").to("file:new-route"); } }); ``` 或者删除一个已有的路由: ``` camelController.stopRoute("file-route"); camelController.removeRoute("file-route"); ``` 这样,你就可以在 Spring Boot 动态启停 Spring XML 路由了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值