Reputation: 23472
I'm reading the Camel in Action 2nd edition, which instructs to use Spring-Camel XML-namespace configuration to embed Camel in Spring, automatically discovering Components defined as Spring beans etc. Here is an example.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>org.apache.camel.example.spring</package>
</camelContext>
<!-- lets configure the default ActiveMQ broker URL -->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost?broker.persistent=false&broker.useJmx=true"/>
</bean>
</property>
</bean>
</beans>
How would I achieve this without using the XML configuration but using Spring Java configuration instead?
Upvotes: 2
Views: 1844
Reputation: 21381
In the GitHub repository for Apache Camel there is an examples directory.
Take a look at the Spring Java Config example in there that is a minimal example of what you need.
Upvotes: 1
Reputation: 362
If you want to use Java syntax and have Camel discover your beans, then you can first define your beans by returning them from a method, and use the @Bean
and @Configuration
annotations. For the XML sample you posted above, this would be something like this:
@Configuration
public class AppConfig {
@Bean
public JmsComponent jms() {
ActiveMQConnectionFactory amqcf = new ActiveMQConnectionFactory();
amqcf.setBrokerURL("vm://localhost?broker.persistent=false");
JmsComponent jms = new JmsComponent();
jms.setConnectionFactory(amqcf);
return jms;
}
}
You can use a similar approach with your routes (use the @Component
annotation):
@Component
public class MyRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:foo?period=5000")
.setBody(simple("Customer"))
.to("jms:queue:customers");
}
}
Then add @ComponentScan
to your main class, which might look something like this (assuming you're using plain Spring, not Spring Boot):
@Configuration
@ComponentScan
public class Application extends CamelConfiguration {
public static void main(String[] args) throws Exception {
//org.apache.camel.spring.javaconfig.Main
Main main = new Main();
main.setConfigClass(Application.class);
main.run();
}
//...
}
Upvotes: 0
Reputation: 4224
Refer this documentation from camel. And Refer this for Activemq configuration.
Adding just a snippet here:
public class MyRouteConfiguration extends CamelConfiguration {
@Autowire
private MyRouteBuilder myRouteBuilder;
@Autowire
private MyAnotherRouteBuilder myAnotherRouteBuilder;
@Override
public List<RouteBuilder> routes() {
return Arrays.asList(myRouteBuilder, myAnotherRouteBuilder);
}
}
Upvotes: 1