0
votes

I am trying to configure Camel using Spring using only annotations and straight up Java. Here is a route I have created:

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

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

A camel context is needed, which I am trying to define in my AppConfig class:

My current version:

@Autowired
private ApplicationContext applicationContext;

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

        return springCamelContext;
    }

The Routes class is just an empty class that extends RouteBuilder.

The problem is that when I actually call the testCamel method, my producer is null. I know I can do the context in an xml configuration file, I was just wondering what I am doing wrong when not using xml.

2

2 Answers

1
votes

I believe you should follow instructions here: http://camel.apache.org/spring-java-config.html , so one @Configuration class extending CamelConfiguration with @ComponentScan.

Moreover your com.bigideas.routing.Routes should be w Spring Bean too (@Bean or @Component)

1
votes

Use camel 2.18 and spring-boot, camel context build-up and unit-testing has been made very easy.

take a look at this example https://github.com/RakeshBhat/rb-camelservlet218-xamples

The class that extends routebuilder should be annotated with @Component. Also in the Spring Controller class, you need to auto-wire Camel Context and then use create producer/consumer templates. make sure you call start/stop methods of template, its so resource are used efficiently.