【问题标题】:Configuring Camel With Spring No Xml使用 Spring No Xml 配置 Camel
【发布时间】:2015-12-29 17:51:36
【问题描述】:

我正在尝试使用 Spring 仅使用注释和直接使用 Java 来配置 Camel。这是我创建的路线:

@Produce(uri = "activemq:my.route")
ProducerTemplate producer;

@RequestMapping(value = "/test", method = RequestMethod.POST)
public void testCamel()
{
    producer.sendBody("TEST");
}

需要一个骆驼上下文,我试图在我的 AppConfig 类中定义它:

我目前的版本:

@Autowired
private ApplicationContext applicationContext;

@Bean
    public CamelContext camelContext() throws Exception {
        SpringCamelContext springCamelContext = new SpringCamelContext(applicationContext);
        springCamelContext.addRoutes(new com.bigideas.routing.Routes());

        return springCamelContext;
    }

Routes 类只是一个扩展 RouteBuilder 的空类。

问题是当我实际调用 testCamel 方法时,我的生产者为空。我知道我可以在 xml 配置文件中做上下文,我只是想知道我在不使用 xml 时做错了什么。

【问题讨论】:

    标签: java spring apache-camel


    【解决方案1】:

    使用 camel 2.18 和 spring-boot,camel 上下文构建和单元测试变得非常容易。

    看看这个例子 https://github.com/RakeshBhat/rb-camelservlet218-xamples

    扩展路由构建器的类应该用@Component注解。同样在 Spring Controller 类中,您需要自动连接 Camel 上下文,然后使用创建生产者/消费者模板。确保调用模板的启动/停止方法,以便有效地使用资源。

    【讨论】:

      【解决方案2】:

      我相信你应该按照这里的说明进行操作:http://camel.apache.org/spring-java-config.html,所以一个 @Configuration 类扩展了 CamelConfiguration@ComponentScan

      此外,您的 com.bigideas.routing.Routes 也应该是 Spring Bean(@Bean@Component

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-10
        • 2019-10-06
        相关资源
        最近更新 更多