Jboss Fuse 简明教程

JBoss Fuse - Apache Camel

在本章中,我们将讨论 Apache Camel 是什么,以及它如何有效地在端点之间路由数据,其中包括一些示例。

What is Apache Camel?

Apache Camel 是一个开源集成框架,始于 2007 年初。

它是一种基于 EIP(企业集成模式)的方法,提供可用于解决企业集成问题的几种开箱即用的模式实现。EIP 是针对企业集成中记录良好且经常出现的问题的久经考验的解决方案。

Camel 也称为路由和中介引擎,因为它能够有效地在端点之间路由数据,同时承担诸如数据格式转换、端点连接等繁重负载。

Basic Example

使用 Apache Camel 的前提条件如下−

  1. Java

  2. Maven

  3. Redhat JBoss Fuse 6.1-GA-379

Create basic skeleton of Application

mvn:archetype generate
–DgroupId = com.tutorialpoint.app
–DartifactId = camel-first-app
–DarchetypeGroupId = org.apache.camel.archetypes
–DarchetypeArtifactId = camel-archetype-spring
–DinteractiveMode = false -X

这将生成以下目录结构。

directory structure

这是我们正在生成的 Camel 应用程序的基本框架。

Edit camel-context.xml

编辑 camel-first-app → src → main → resources → META-INF\spring\camel-context .xml 以匹配如下内容

<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<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">
      <!-- here is a sample which processes the input file
         (leaving them in place - see the 'noop' flag)
         then performs content based routing on the message using XPath -->

      <route>
         <from uri = "file:///d:/src/data?noop=false"/>
         <choice>
            <when>
               <xpath>/person/city = 'London'</xpath>
               <log message = "UK message"/>
               <to uri = "file:///d:/target/messages/uk"/>
            </when>

            <otherwise>
               <log message = "Other message"/>
               <to uri = "file:///d:/target/messages/others"/>
            </otherwise>

         </choice>

      </route>
   </camelContext>
</beans>

Edit pom.xml

在 <plugins></plugins> 内添加以下代码

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactId>
   <version>2.3.4</version>
   <extensions>true</extensions>

   <configuration>
      <instructions>
         <Bundle-SymbolicName>
            ${project.artifactId}
         </Bundle-SymbolicName>
         <Import-Package>*</Import-Package>
      </instructions>
   </configuration>

</plugin>

jar → bundle 更改打包类型。

<packaging>bundle</packaging>

使用以下命令构建项目−

mvn clean install

Install Project into Fuse

使用 Fuse.bat/start.bat 启动 Fuse。如果你使用 start.bat 启动 Fuse,请使用 client.bat 连接到 Fuse。你应该获得如下屏幕截图中所示的 UI。

install project in fuse

这是用于访问 Karaf 和 Fuse 命令的 CLI。

install –s mvn:com.tutorialpoint.app/camel-firt-app/1.0-SNAPSHOT

Test if your Project is Running

现在,你的应用程序应安装在 Fuse 中。复制 camel-first-app 内的数据目录并将其放在 D:/src/ 中,它应将城市 = 伦敦的消息复制到 D:/target/merssages/uk 中。

将输入文件放在 D:/src/data

Input

Message1.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "james">
   <firstName>James</firstName>
   <lastName>Strachan</lastName>
   <city>London</city>
</person>

Message2.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "hiram">
   <firstName>Hiram</firstName>
   <lastName>Chirino</lastName>
   <city>Tampa</city>
</person>

Output

在 D:/target/messages/uk 中

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "james">
   <firstName>James</firstName>
   <lastName>Strachan</lastName>
   <city>London</city>
</person>

在 D:/target/messages/others 中

<?xml version = "1.0" encoding = "UTF-8"?>
<person user = "hiram">
   <firstName>Hiram</firstName>
   <lastName>Chirino</lastName>
   <city>Tampa</city>
</person>