What I learned on August 6th,

Add Camel and ActiveMQ to a Spring Boot application

Dependencies

From version 2.22.0 Camel supports Spring Boot 2.0.x. With a camel-spring-boot-starter dependency, when you write a route, it allows you to annotate your class with a @Component annotation. Spring will auto scan and instantiate the route and add it to a CamelContext.

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-spring-boot-starter</artifactId>
	<version>${camel-version}</version>
</dependency>

Add Camel’s features for ActiveMQ to your Camel project. activemq-broker.jar is excluded since we’re going to connect to a standalone ActiveMQ broker.

<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-camel</artifactId>
	<!-- version>5.14.0</version -->
	<exclusions>
		<exclusion>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
		</exclusion>
	</exclusions>
</dependency>

Add Spring’s supports and infrastructure to connect to ActiveMQ broker.

 <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
		</exclusion>
	</exclusions>
</dependency>

activemq-broker.jar is provided at runtime when connecting to a standalone ActiveMQ broker.

<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-broker</artifactId>
	<scope>provided</scope>
</dependency>

Create a Camel route

@Component
public class MyCamelRoute extends RouteBuilder {
	@Override
	public void configure() throws Exception {
		from("activemq:foo")
			.to("log:sample");
		
		from("timer:bar")
			.setBody(constant("Hello from Camel"))
			.to("activemq:foo");		
	}
}

Now you’re good to run the application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: