English 中文(简体)
Using Camel with Spring
  • 时间:2024-12-22

Apache Camel - Using with Spring


Previous Page Next Page  

We will now recreate the apppcation from the previous chapter using Spring. This will give us an idea of how to create Camel routing in XML rather than a DSL.

Creating New Project

Create a new Maven project and specify the following −

GroupId: BasketWithSpring
ArtifactId: BasketWithSpring

Select the default location for your project or if you prefer specify the directory of your choice.

Adding Dependencies

In addition to the core dependencies that you used in the earper apppcation, you need to add few more dependencies to use Spring. The dependencies are added in pom.xml. Now, open pom.xml and add the following dependencies −

<dependencies>
   ...
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.3.RELEASE</version>
   </dependency>
   
   <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.2</version>
   </dependency>
   
   <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.1</version>
   </dependency>
   
   <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>2.15.1</version>
   </dependency>
</dependencies>

Creating Java DSL for Spring

Let us now create a new Java class called DistributeOrderXML. Add the following code to it −

pubpc class DistributeOrderXML {
   pubpc static void main(String[] args) throws Exception {
      ApppcationContext appContext = new ClassPathXmlApppcationContext(
         "SpringRouteContext.xml");
      CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);
      try {
         camelContext.start();
         ProducerTemplate orderProducerTemplate = camelContext.createProducerTemplate();
         InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
            .getResource("order.xml").getFile());
         
         orderProducerTemplate.sendBody("direct:DistributeOrderXML", orderInputStream);
      } finally {
         camelContext.stop();
      }
   }
}

In the main method, first we create an instance of ApppcationContext, which is the central interface within a Spring apppcation. In its constructor, we specify the name of the XML file that contains our routing and filtering information.

ApppcationContext appContext = new ClassPathXmlApppcationContext(
   "SpringRouteContext.xml");

Next, we create CamelContext specifying the above created ApppcationContext in its parameter.

CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);

At this point, our routing and filtering is set up. Therefore, we start the CamelContext using its start method. As in the earper case, we define the Endpoint for loading the order.xml file and start the processing. Now, let us understand how routing is defined in XML.

Creating Apppcation Context

Add a new XML file to the project and call it SpringRouteContext.xml. Cut-n-paste the following contents to this file.

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "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.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 = "direct:DistributeOrderXML"/>
         <log message = "Sppt by Distribute Order"/>
         <sppt>
            <xpath>//order[@product =  Oil ]/items</xpath>
            <to uri = "file:src/main/resources/order/"/>
            <to uri = "stream:out"/>
         </sppt>
      </route>
   </camelContext>
</beans>

Here, we define the xpath query as follows, note that we now select all orders for “oil”.

<xpath>//order[@product =  Oil ]/items</xpath>

The output Endpoints are multiple. The first endpoint specifies the order folder and the second one specifies the console.

<to uri = "file:src/main/resources/order/"/>
<to uri = "stream:out"/>

Run the apppcation.

Test Results

When you run the apppcation, you will see the following output on screen.

<items>
   <item>
      <Brand>Cinthol</Brand>
      <Type>Original</Type>
      <Quantity>4</Quantity>
      <Price>25</Price>
   </item>
   <item>
      <Brand>Cinthol</Brand>
      <Type>Lime</Type>
      <Quantity>6</Quantity>
      <Price>30</Price>
   </item>
</items>

Check out the order folder in the path specified by you. You will find a newly created file that contains the above XML code.

Conclusion

Camel provides a ready-to-use framework that implements EIPs to ease your integration projects. It supports coding in domain-specific languages and also the use of XML.

Advertisements