JBoss Fuse - Apache Camel



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

什么是 Apache Camel?

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

它是一种基于 EIP(企业集成模式)的方法,提供了许多开箱即用的模式实现,可用于解决企业集成问题。EIP 不过是针对企业集成中已记录且反复出现的问题的成熟解决方案。

Camel 也被称为路由和中介引擎,因为它可以有效地在端点之间路由数据,同时处理繁重的任务,例如数据格式转换、端点连接等等。

基本示例

使用 Apache Camel 的先决条件是:

  • Java
  • Maven
  • Redhat JBoss Fuse 6.1-GA-379

创建应用程序的基本框架

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 应用程序的基本框架。

编辑 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>

编辑 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

将项目安装到 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

测试您的项目是否正在运行

现在您的应用程序应该已安装在 Fuse 中。复制 **camel-first-app** 内部的 data 目录并将其放置在 **D:/src/** 中,它应该将城市为 London 的消息复制到 **D:/target/merssages/uk** 中。

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

输入

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>

输出

在 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>
广告

© . All rights reserved.