Maven Project Setup for Camel
This blog is the second blog in the Apache-Camel blog series. Before we get started, make sure you have, java jdk, java jre and tomcat installed on your machine.
Steps for setting up maven3 development environment.
- Download maven from here (version 3.0.3 is the latest at the time of writing this blog). Unzip it to a local directory.
- Add maven to the path of your OS (append the maven/bin to the path variable of your os)
- Download camel distribution from here (version 2.8.3 is the version I am using throughout this blog). Unzip it to a local directory.
- Open a command line (bash in linux) and type mvn and press Enter, you should get an error on the command line with "BUILD FAILURE" at the end of it.
- If you have reached so far that means your maven is setup and all the necessary environmental variables are perfectly set. Even though the above screen is an error, its maven telling us that there was no pom.xml or other project definitions found in the current directory.
Possible errors you might encounter are before getting here are -
i. mvn command not found - maven bin directory is not in the os path.
ii. java command not found - java bin directory is not in the os path
iii. JAVA_HOME not set - Environment variable JAVA_HOME is not set, (use export command in linux).
iv. MVN_HOME not set - Environment variable MVN_HOME is not set, (use export command in linux).
v. JAVA_HOME not pointing to jdk
- Copy one of the sample programs that came along with the camel distribution (you can find it under apache-camel-2.8.3/examples/camel-example-servlet-tomcat) to a working directory.
- Using command line, navigate to the copied example directory and type mvn install and hit Enter. This time the chances of going wrong is less than 10% and hopefully you should be able to see "BUILD SUCCESS" message on the command line screen.
If you have prior experience with Maven then this is pretty much simple to understand. In case you have not used Maven, it is pretty much like Ant except the fact that maven manages the library dependencies and versions and keeps a copy of them in a local repository. If you look at the contents inside the pom.xml file within the project root directory you can see that it contains description of libraries and components we need maven to download. The example projects contains necessary libraries including logging, spring web, camel-core and servlet artifacts . As we progress in this series we will be mostly using the camel-servlet component as the incoming or ‘from' end-points. I will explain what's ‘from' and ‘to' in the next section.
Once you see the "BUILD SUCCESS" message in the command prompt you can find a new target directory created within the project root directory. Inside the target directory you can see the camel-example-servlet-tomcat-2.8.3.war file. This is a generic servlet war file and can be deployed in any Java servlet container.
‘camel-config.xml' and web.xml
As I mentioned earlier in this article there is always a ‘from' and ‘to' inside a route. A route is nothing but a channel of information flow with a ‘from' and one or more ‘to' s. Let's take a look at a sample Route. The example camel project we used earlier contains a route configuration file under camel-example-servlet-tomcat/src/main/resources/camel-config.xml . Let's see how the sample project is able to execute camel routes.
Under the camel-example-servlet-tomcat/src/main/webapp/WEB-INF/ you can find the web.xml file. Since this is a war deployment target based on servlet you can see that the Camel Servlet is the front controller which intercepts all the requests of the form /camel/*. And the camel-config.xml is passed as a context param to the servlet, so it reads it on startup and initializes the camel config (which ultimately contains the routes)
The web.xml for the camel-example-servlet-tomcat project
|
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:camel-config.xml</param-value>
</context-param>
<!-- the listener that kick-starts Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Camel servlet -->
<servlet>
<servlet-name>CamelServlet</servlet-name>
<servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Camel servlet mapping -->
<servlet-mapping>
<servlet-name>CamelServlet</servlet-name>
<url-pattern>/camel/*</url-pattern>
</servlet-mapping> |
The camel-config.xml for the camel-example-servlet-tomcat project
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
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 xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="servlet:///hello"/>
<choice>
<when>
<header>name</header>
<transform>
<simple>Hello ${header.name} how are you?</simple>
</transform>
</when>
<otherwise>
<transform>
<constant>Add a name parameter to uri, eg ?name=foo</constant>
</transform>
</otherwise>
</choice>
</route>
</camelContext>
</beans> |
The Servlet 'from' Component
The above camel-config contains only one route. This is a very simple route which demostrates the camel servlet component in action.The starting end point 'from' of the above route is hello servlet. It simply tells us that once you deploy the war file generated by the maven install command you can type the server address:port/camel-example-servlet-tomcat/hello and this route will get executed. In-order to see this route in action, deploy the generated war file under the target directory named camel-example-servlet-tomcat-2.8.3.war to tomcat server. Type the path http://localhost:8080/camel-example-servlet-tomcat/hello into your HTTP Rest Client (for Mozilla Firefox ) or another HTTP client or a web browser. And set an HTTP header with name="Your Name".
Simple is 'simple'
In-order for the logic within the example route to work you need to supply the header with name="your name" format. The route filters the header called 'name' and the value of the header will be printed back along with a greeting of the form Hello ‘name' how are you?. The language used to achieve the name replacement within the message is called "simple". As the name sounds the simple expressions are simple and they are native to camel. Another blog about the simple expressions and their advanced usage in header manipulation is coming in my next blog, so till then namasthe.
"The opinions expressed here are my personal opinions. Content published here is not read or approved in advance by EMC and does not necessarily reflect the views or opinions of EMC."