java - 使用 bean ref 在 Camel route 设置 header

标签 java apache-camel spring-camel

 from("direct:myRoute1")
                .bean(new DemoRoute(), "test(Demo,xxx)")
                .end();


 from("direct:myRoute2")
                .bean(new DemoRoute(), "test(Demo,xxx)")
                .end(); 



public interface Shape

@Component
class Circle implements Shape{
}

@Component
class Square implements Shape{}

我想在路由 test(Demo,xxx) 中注入(inject) Shape 实现

  1. setHeader() 能否帮助在路由中添加 Shape 实现。
  2. 除了在 Camel route 设置 header 之外,还有其他选择吗,因为它有其优点和缺点

Pros and Cons of setting Lot of headers in Camel Exchange

最佳答案

这是一个绕过 Camel 的解决方案:

由于您是自己实例化 bean 而不是依赖 spring 来管理它,因此您可以通过构造函数传递 Shape 实现。

在您的 DemoRoute 类中添加一个 Shape 字段:

public class DemoRoute {

        private  final Shape shape;


        public DemoRoute(Shape shape) {
            this.shape = shape;
        }

        // test method that uses shape
    }

然后在您的路由配置类中,配置如下:

@Component
public class CustomRoute extends RouteBuilder {

    private final Square square;
    private final Circle circle;

    CustomRoute(Square square, Circle circle){
      this.square = square;
      this.circle = circle;
    }


    @Override
    public void configure() throws Exception {
        from("direct:myRoute1")
                .bean(new DemoRoute(circle), "test(Demo,xxx)")
                .end();


        from("direct:myRoute2")
                .bean(new DemoRoute(square), "test(Demo,xxx)")
                .end();
    }
}

关于java - 使用 bean ref 在 Camel route 设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52909179/

相关文章:

java - 通过 Apache Camel 调用外部 Web 服务

java - 如何在 Camel route 使用上下文路径?

java - 将 Elasticsearch 从 1.4.3 迁移到 2.4 java 代码

java - 什么是 PECS(生产者扩展消费者 super )?

java - PowerMockito final方法调用验证不起作用

Apache Camel 的 JsonMappingException

java - 如何使用 apache Camel dsl 删除 csv header

java - 如何创建一个为所有类创建对象的 setUp 方法?

java - Camel 是否为每条路线创建一个线程

java - 为什么运行 Camel 示例需要在主线程上 hibernate