|
Page Edited :
CAMEL :
Tutorial on using Camel in a Web Application
Tutorial on using Camel in a Web Application has been edited by James Strachan (Sep 11, 2008). Content:Tutorial on using Camel in a Web ApplicationCamel has been designed to work great with the Spring framework; so if you are already a Spring user you can think of Camel as just a framework for adding to your Spring XML files. So you can follow the usual Spring approach to working with web applications; namely to add the standard Spring hook to load a /WEB-INF/applicationContext.xml file. In that file you can include your usual Camel XML configuration. Step1: Edit your web.xmlTo enable spring add a context loader listener to your /WEB-INF/web.xml file <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> This will cause Spring to boot up and look for the /WEB-INF/applicationContext.xml file. Step 2: Create a /WEB-INF/applicationContext.xml fileNow you just need to create your Spring XML file and add your camel routes or configuration. For example <?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="seda:foo"/> <to uri="mock:results"/> </route> </camelContext> </beans> Then boot up your web application and you're good to go! Hints and TipsIf you use Maven src/main/webapp/WEB-INF web.xml applicationContext.xml To enable more rapid development we hightly recommend the jetty:run maven plugin Please refer to the help for more information on using jetty:run <build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
Then you can run your web application as follows mvn jetty:run Then Jetty will also monitor your target/classes directory and your src/main/webapp directory so that if you modify your spring XML, your web.xml or your java code the web application will be restarted, re-creating your Camel routes. |
Unsubscribe or edit your notifications preferences
