Routing/Mediation service
The routing/mediation between services/bundles will be created using Camel Spring DSL language. We will describe its creation/genesis step by step.
First, create the file camel-context.xml
in the directory src/main/resources/META-INF/spring
and add the lines :
<?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" xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:cxf="http://camel.apache.org/schema/cxf" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd"> </beans>
Compare to a simple camel project, the spring beans tag has been enriched with new namespaces :
- spring-osgi (http://www.springframework.org/schema/osgi) which is used by OSGI blueprint services
- camel-osgi (http://camel.apache.org/schema/osgi) who will allows to integrate camel with OSGI world
- cxf (http://camel.apache.org/schema/cxf) to integrate CXF architecture with Camel
Now, that the schema/namespaces are declared, we can start to create beans reference or import resources that our routing engine will use.
1) Webservice infrastructure : CXF
We will use the CXF framework to deploy the reportincident webservice and run it into the OSGI platform
To work with it, resources (= spring beans) declared in CXF project must be imported using the command import resource
.
... <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" /> <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml" /> ...
These imports will be used by spring at the bundle startup to instantiate the beans defined in these files. These beans are responsible in fact to deploy the architecture of the CXF bus top of the OSGI server and to provide a servlet that we will use to communicate with webservices engine of CXF.
Remark : for the purpose of this tutorial, we have packaged this configuration into the camel-spring file but it could be defined in a separate xml file with by example the component/endpoint that Camel will use to communicate with CXF bus. This allows you to separate routing from parameters to be provided to configure endpoints.
The camel CXF endpoint is configurated like this :
<!-- webservice endpoint --> <cxf:cxfEndpoint id="reportIncident" address="/camel-example/incident" serviceClass="org.apache.camel.example.reportincident.ReportIncidentEndpoint" xmlns:s="http://reportincident.example.camel.apache.org"> </cxf:cxfEndpoint>
Remarks :
- the address corresponds to the URI address of the web services,
- the serviceClass is the name of the class used work with the webservices and deployed in the bundle
reportincident.webservice
- xmlns is the namespace of the reportincident webservice
2) Queueing engine
No matter if the incidents come from a webservice or a files, but before to process and save them in the database, we will put
our messages in the queue of a queue manager. The queue manager used here is ActiveMQ.
Like CXF, we will use spring xml file to deploy ActiveMq into the server and configure it. This will be done in two steps
a) ActiveMQ
Like CXF, ActiveMq can be installed in the infrastructure using a spring.xml configuration file. So, create the file }} in the directory {{src/main/resources/META-INF/spring
and add the following lines.
At the bundle startup, Spring will instantiate the beans declared and in consequence start the queueing engine. We haven't changed the content of the file corresponding to what is proposed in the ServiceMix distribution but you can use here the same technic described for the Datasource and add properties that you configure through by example a org.apache.activemq.config.etc
file
The pom.xml file must be modified to add properties required by Spring blueprint. So add the following lines :
b) Camel ActiveMq component
To makes Camel independent of the JMS queue engine deployed in the OSGI server, we will implement a proxy using blueprint service between Camel component and the queuing engine used (Apache ActiveMq, IBM Websphere MQ, Oracle Advance Queue, TIBCO, ...)
First, create a spring DSL containing the following information :
<?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:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <bean id="active-mq" class="org.apache.activemq.camel.component.ActiveMQComponent" /> (1) <osgi:service id="osgiqueuingservice" ref="active-mq" interface="org.apache.camel.Component"/> (2) </beans>
Remarks:
(1) Spring will instantiate the ActiveMqComponent to work with the ActiveMq server. If you would like to use another JMS component, then switch this class to org.apache.camel.component.jms.JmsComponent
(2) Our camel component will be exposed on the OSGI registry as an org.apache.camel.Component
and has a reference to the ActiveMQComponent, JMSComponent
Adapt the POM.xml file like this
3) Beans reference
4) Routing
Web
Packaging and deployment
Links
- Part 1 : simple example
- Part 2a : real example, architecture, project setup, database creation
- Part 2b : transform projects in bundles
- Part 2c : add routing, web and deployment