[CONF] Apache Camel > Tutorial-Example-ReportIncident-Part5

2013-01-27 Thread confluence







Tutorial-Example-ReportIncident-Part5
Page edited by Claus Ibsen


 Changes (1)
 




...
- [Part 4|Tutorial-Example-ReportIncident-Part4] - [Part 5|Tutorial-Example-ReportIncident-Part5] 
- [Part 6|Tutorial-Example-ReportIncident-Part6] 


Full Content

Part 5

... Continued from Part 4
We continue from part 4 where we have the routing in place. However as you might have noticed we aren't quiet there yet with a nice solution, we are still coding to much. In this part we will look into to address these two concerns:

	Starting Camel automatically
	Using CXF directly



Starting Camel automatically
Our current deployment model is as a war and we have the web.xml to help start things. Well in fact we will leverage Spring to start the world . We use it's ContextListener


	
	
		org.springframework.web.context.ContextLoaderListener
	



Then we need a standard Spring XML file so we create a new file in src/main/resources and name it camel-config.xml. Before we start editing this XML file we need to link to it from the web.xml file. So we add this snippet to the web.xml:


	
	
	contextConfigLocation
classpath:camel-config.xml
	



Now we are ready to edit the camel-config.xml file that is a standard Spring XML bean file. So you can add standard spring beans and whatnot you like to do and can do with Spring.


"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-2.5.xsd">





Now we are nearly there, we just need to add Camel to the Spring XML file, so Spring knows Camel exists and can start it. First we need to add Camel to the schema location in the top of the XML file.


   ...
   xsi:schemaLocation="
http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">



Now we are ready to let Spring and Camel work together. What we need to do is adding a CamelContext to the Spring XML file. Camel ships with a CamelContextFactoryBean that is a Spring factory bean we should use for creating and initializing the SpringCamelContext. SpringCamelContext is extending CamelContext to be Spring aware so Camel and Spring can work nicely together. For instance the Registry will now use Spring bean lookup. So any spring bean can now easily be lookup and used from Camel. Well back to today's lesson. So we can create a SpringCamelContext using the factory bean as illustrated below:


   "camel" class="org.apache.camel.spring.CamelContextFactoryBean"/>



However this is not used very often as Spring has support for custom namespace, so Camel has a CamelNamespaceHandler so we can create Camel using nice XML syntax as:


"camel" xmlns="http://activemq.apache.org/camel/schema/spring">
   ...




Adding route builder
Now we have Camel integrated but we still need to add our route bulder that we did manually from the javacode as:


// append the routes to the context
context.addRoutes(new ReportIncidentRoutes());



There are two solutions to this

	using spring bean
	package scanning



Using a spring bean we just declare the route builder using a regular spring bean:


   "myrouter" class="org.apache.camel.example.reportincident.ReportIncidentRoutes"/>



And then we can refer to it from our CamelContext:


"camel" xmlns="http://activemq.apache.org/camel/schema/spring">
   "myrouter"/>




So now when Spring start's it will read the camel-context.xml file and thus also start Camel as well. As SpringCamelContext is spring lifecycle event aware, Camel will also shutdown when Spring is shutting down. So when you stop the web application Spring will notify this and Camel is also shutdown nice and properly. So as an end user no need to worry. 

The package scanning solution is for convenience to refer to a java package and Camel will scan all classes within this package for RouteBuilder classes. If using this then you dont need to declare your route builder as a Spring bean. So the XML can be reduced to.


"camel" xmlns="http://activemq.apache.org/camel/schema/spring">
   org.apache.camel.example.reportincident




Using CXF directly
Now we have seen how you can leverage Spring to start Camel, in fact it handles the lifecycle of Camel, so you can say Camel is embedded with Spring in your application.

From the very start of this tutorial we have used CXF as the webservice framework and we haven't integrated it directly with Camel as it can do out-of-the-box. Camel 

[CONF] Apache Camel > Tutorial-Example-ReportIncident-Part5

2012-08-29 Thread confluence







Tutorial-Example-ReportIncident-Part5
Page edited by Claus Ibsen


 Changes (1)
 




...
In the next part's look at using XML to create the route instead of Java code. Then it might be even more readable by non developers.   
h2. [#Resources] * {attachments:patterns=part-five.zip} 
 h2. Links 
...


Full Content

Part 5

... Continued from Part 4
We continue from part 4 where we have the routing in place. However as you might have noticed we aren't quiet there yet with a nice solution, we are still coding to much. In this part we will look into to address these two concerns:

	Starting Camel automatically
	Using CXF directly



Starting Camel automatically
Our current deployment model is as a war and we have the web.xml to help start things. Well in fact we will leverage Spring to start the world . We use it's ContextListener


	
	
		org.springframework.web.context.ContextLoaderListener
	



Then we need a standard Spring XML file so we create a new file in src/main/resources and name it camel-config.xml. Before we start editing this XML file we need to link to it from the web.xml file. So we add this snippet to the web.xml:


	
	
	contextConfigLocation
classpath:camel-config.xml
	



Now we are ready to edit the camel-config.xml file that is a standard Spring XML bean file. So you can add standard spring beans and whatnot you like to do and can do with Spring.


"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-2.5.xsd">





Now we are nearly there, we just need to add Camel to the Spring XML file, so Spring knows Camel exists and can start it. First we need to add Camel to the schema location in the top of the XML file.


   ...
   xsi:schemaLocation="
http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">



Now we are ready to let Spring and Camel work together. What we need to do is adding a CamelContext to the Spring XML file. Camel ships with a CamelContextFactoryBean that is a Spring factory bean we should use for creating and initializing the SpringCamelContext. SpringCamelContext is extending CamelContext to be Spring aware so Camel and Spring can work nicely together. For instance the Registry will now use Spring bean lookup. So any spring bean can now easily be lookup and used from Camel. Well back to today's lesson. So we can create a SpringCamelContext using the factory bean as illustrated below:


   "camel" class="org.apache.camel.spring.CamelContextFactoryBean"/>



However this is not used very often as Spring has support for custom namespace, so Camel has a CamelNamespaceHandler so we can create Camel using nice XML syntax as:


"camel" xmlns="http://activemq.apache.org/camel/schema/spring">
   ...




Adding route builder
Now we have Camel integrated but we still need to add our route bulder that we did manually from the javacode as:


// append the routes to the context
context.addRoutes(new ReportIncidentRoutes());



There are two solutions to this

	using spring bean
	package scanning



Using a spring bean we just declare the route builder using a regular spring bean:


   "myrouter" class="org.apache.camel.example.reportincident.ReportIncidentRoutes"/>



And then we can refer to it from our CamelContext:


"camel" xmlns="http://activemq.apache.org/camel/schema/spring">
   "myrouter"/>




So now when Spring start's it will read the camel-context.xml file and thus also start Camel as well. As SpringCamelContext is spring lifecycle event aware, Camel will also shutdown when Spring is shutting down. So when you stop the web application Spring will notify this and Camel is also shutdown nice and properly. So as an end user no need to worry. 

The package scanning solution is for convenience to refer to a java package and Camel will scan all classes within this package for RouteBuilder classes. If using this then you dont need to declare your route builder as a Spring bean. So the XML can be reduced to.


"camel" xmlns="http://activemq.apache.org/camel/schema/spring">
   org.apache.camel.example.reportincident




Using CXF directly
Now we have seen how you can leverage Spring to start Camel, in fact it handles the lifecycle of Camel, so you can say Camel is embedded with Spring in your application.

From the very start of this tutorial we have used CXF as the webservice framework

[CONF] Apache Camel: Tutorial-Example-ReportIncident-Part5 (attachment added)

2009-02-07 Thread confluence










New files attached to:
CAMEL :
Tutorial-Example-ReportIncident-Part5




Tutorial-Example-ReportIncident-Part5
by Claus Ibsen
.


Attached file(s):

part-five.zip (application/zip, 10 kb)

part-five












Powered by
Atlassian Confluence
(Version: 2.2.9 Build:#527 Sep 07, 2006)
-
Bug/feature request

Unsubscribe or edit your notifications preferences








[CONF] Apache Camel: Tutorial-Example-ReportIncident-Part5 (attachment added)

2009-01-26 Thread confluence










New files attached to:
CAMEL :
Tutorial-Example-ReportIncident-Part5




Tutorial-Example-ReportIncident-Part5
by Charles Moulliard
.


Attached file(s):

part-five.zip (application/x-zip, 7 kb)














Powered by
Atlassian Confluence
(Version: 2.2.9 Build:#527 Sep 07, 2006)
-
Bug/feature request

Unsubscribe or edit your notifications preferences