Apache Servicemix入门之二

ServiceMix正常运行后,我们尝试编写一个Blueprint XML文档:
<?xml version="1.0" encoding="UTF-8"?>   
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"   
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0;http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"   
default-timeout="0">   
  
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
 <route>
	<from uri="file:c:/inbox"/>
	<to uri="file:c:/outbox"/>
 </route>
</camelContext>  
  
</blueprint>   


将以上内容保存成example1.xml,并将此文件拷贝到<ServiceMix Home>/deploy/文件夹下。

到ServiceMix的console里输入命令list,可以看到:




可以看到最后一行那个ID为178的项,名字为example1.xml,它的Blueprint容器状态为Created,证明这个文档已被正常解析了。

再检查一下电脑的C盘,是否多了个inbox的文件夹?

现在,尝试随意复制任意一个文件(假设为log.txt)到C:\inbox文件夹下。

再看看,C盘是否又多了一个C:\outbox文件夹,而且文件夹有一个文件(log.txt)?

这是怎么发生的呢?

我们看回刚才的example1.xml,里面有个<camelContext>,里面嵌套一个<route>,<route>标签里又嵌套一个<from>和<to>,这就定义了一个camel路由。

这个Camel路由从<from>标签取出信息,然后交由<to>标签消费。那么这个信息是什么东西呢?这就要看<from>和<to>标签中指定的uri属性了。

在这里,uri分别是file:c:/inbox和file:c:/outbox,它就是代表当有<from>的uri所代表的file endpoint的数据产生(将log.txt文件拷入inbox文件夹)后,camel路由就开始工作:endpoint从inbox文件夹里取出log.txt,交由<to>的uri(file:c:/outbox)代表的file endpoint处理(将log.txt写入c:\outbox文件夹内)。


那么endpoint在这里是什么概念呢?这个endpoint是属于camel的概念,首先camel项目定义了一系列针对不同功能的Component,这些Component用不同格式的URI来代表,比如:
file:directoryName[?options]


当我们将这些Component用于定义Camel路由时,Component就相当于一个实例工厂,在有信息需要处理时,这个工厂就产生一个实例来处理这些信息,而这个实例就是叫做一个endpoint。

如果理解了Camel的Component、URI、Endpoint的概念和他们之间的关系,那么恭喜你,你基本上就算入了Camel的门了。


猜你喜欢

转载自killko.iteye.com/blog/1825952