Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing camel from Spring annotation config

I am trying to learn Spring and understand how it works. I have followed some tutorials in setting up Spring and Camel, and have had it working using default setups.

I am now attempting to convert as much as possible of my configuration XML-files to Java-classes. So far I have been successful in creating the camel-routes in a Java-class (extending SpringRouteBuilder and implementing configure() ), and all the beans from my spring-configuration file (Bean->Function with @Bean). The only part I am missing is the camelContext definition (?) that starts my camel routes (I think...):

<camel:camelContext id="camel5">
    <camel:package>net.krg.kneip.routing</camel:package>
</camel:camelContext>

What would the equivalent non-XML of this be?

Not sure if it will help, but here is my AppConfig class so far: http://pastebin.com/vsRAbpK1

Thanks!

SOLUTION:

@Bean
public CamelContext camel() throws Exception{   
  CamelContext camelContext = new DefaultCamelContext();    
  camelContext.addRoutes(new net.krg.kneip.routing.Routes());
  camelContext.start();
  return camelContext;      
}
like image 830
Solvemon Avatar asked Apr 19 '13 09:04

Solvemon


People also ask

What is Camel spring boot starter?

Spring Boot component provides auto-configuration for Apache Camel. Our opinionated auto-configuration of the Camel context auto-detects Camel routes available in the Spring context and registers the key Camel utilities (like producer template, consumer template and the type converter) as beans.

What is CamelContext in Apache Camel?

The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages. Contains the components used.

How do you get Camel context in spring boot?

Camel auto configuration collects all the RouteBuilder instances from the Spring context and automatically injects them into the CamelContext . It simplifies the process of creating new Camel route with the Spring Boot starter. You can create the routes by adding the @Component annotated class to your classpath.

How do you manually start a Camel route?

The autoStartup option on the <camelContext> is only used once, so you can manually start Camel later by invoking its start method in Java as shown below. For example when using Spring, you can get hold of the CamelContext via the Spring ApplicationContext : ApplicationContext ac = ... CamelContext camel = ac.


1 Answers

CamelContext context = new DefaultCamelContext();

I think this is what you're looking for.

Read more here

like image 118
U2one Avatar answered Nov 11 '22 18:11

U2one