Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set header in camel route with bean ref

 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{}

I want to inject the Shape implementation in route test(Demo,xxx)

  1. Can setHeader() help to add a Shape implementation in route.
  2. Can there be an alternative apart from setting header in camel route as it has its pros and cons

Pros and Cons of setting Lot of headers in Camel Exchange

like image 577
coder25 Avatar asked Nov 07 '22 00:11

coder25


1 Answers

Here is a solution to bypass Camel :

Since you are instanciating the bean yourself and not relying on spring to manage it, you could pass the Shape implementation through a constructor.

Add a Shape field in your DemoRoute class:

public class DemoRoute {

        private  final Shape shape;


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

        // test method that uses shape
    }

And in your Route config class, configure as follow :

@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();
    }
}
like image 157
Camille Vienot Avatar answered Nov 15 '22 11:11

Camille Vienot