Apache ServiceMix 初探

发布时间:2023-04-29 09:43:15

这篇文章是针对apache的笔记-servicemix-4.5.配套qucikstartart3.pdf文档。

基本操作

启动servicemix:运行 "apache-servicemix-4.5.3\bin文件夹下的servicemix.bat,弹出ServiceMix的控制台可以在这个控制台上添加或删除bundles,并安装各种可选特性。

检查已安装的bundles:运行 osgi:list 命令

查看日志:操作 log:display 命令

只检查异常日志:操作: log:display-exception 命令

设置日志级别:操作 log:set DEBUG 命令(以DEBUG为例)

查看一定级别的日志:操作 log:display | grep DEBUG(以DEBUG为例)

查看可用的功能组件:操作feature:list 命令

查看某一类别的可用功能组件:操作feature:list | grep camel (以camel为例)

安装功能组件:操作 feature:install webconsole (以webconsole为例)

webconsole是网页版的管理客户端,URL是:http://localhost:8181/system/console,用户名/密码:smx/smx

 

Apache ServiceMix 初探_apache

简单示例:使用Camel

Apache Camel是一个开源、功能丰富的应用集成框架,支持常见的EIP模式,是一个基于规则的强大路由引擎,可以轻松实现新闻路由和新闻转换,ServiceMix深度集成Camel,支持各种复杂的ESB功能。

本例实现了一个非常简单的路由功能:将文件从 camel/input 文件夹移到 camel/output 文件夹。

1、创建路由配置文件study_one.xml (camel支持两种风格的路由配置文件,一种是blueprint容器,另一种是spring容器,这里使用blueprint)

1. <?xml version="1.0" encoding="UTF-8"?>  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  3. xsi:schemaLocation="  4.       http://www.osgi.org/xmlns/blueprint/v1.0.0  5. >  6. <camelContext xmlns="http://camel.apache.org/schema/blueprint">  7. <route>  8. <!--camel用URI描述各种组件,如file:表示文件,http:表示web,activemq:表示消息队列-->  9. <from uri="file:camel/input"/><!--文件路由的起始文件夹-->  10. <log message="Moving ${file:name} to the output directory"/><!--路由时需要打印的日志内容-->  11. <to uri="file:camel/output"/><!--文件路由的终点文件夹-->  12. </route>  13. </camelContext>  14. </blueprint>

2、发布和启动路由

第一步:study_one.将xml移到ServiceMix下的deploy文件夹下,ServiceMix将加载并发布该文件。

步骤二:转到ServiceMix根目录,你会发现一个新的camel//input 文件夹。

第三步:将任何copy文件夹入camle/input文件中,然后查看camel/output,发现copy文件同时传输到ouput下,说明路由生效。

第四步:查看日志,运行log:display命令将发现路由文件的相关信息。

3、管理路由

第一步:搜索study_one.bundle对应xml路由文件。操作osgiii:list,搜索运行结果中的study_one.xml,截图如下:

 

Apache ServiceMix 初探_xml_02

从截图中可以看出路由对应的bundle id是181,Blueprint 创建container来启动这个路由(如果使用spring配置文件,spring将显示started)。

第二步:终止路由。操作osgii:stop 181.将路由终止,向camel/input下camel/output下camel/output下camel/output下查看文件,文件未移动,说明终止成功。

第三步:打开路由。操作osgi:start 181.去camel/output查看,第二步未移动的文件被移动到文件夹中,说明启动成功。

ActiveMQ使用ActiveMQ

1、创建第一条路由,将activemq/input文件夹下的文件路由夹到atvive/output文件夹下,创建的原理和步骤相同。

配置文件study_MQ1.xml的内容如下:

1. <?xml version="1.0" encoding="utf-8"?>  2. <blueprint  3. xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5. xsi:schemaLocation="  6.   http://www.osgi.org/xmlns/blueprint/v1.0.0  7. >  8. <camelContext xmlns="http://camel.apache.org/schema/blueprint">  9. <route>  10. <from uri="file:activemq/input"/>  11. <to uri="file:activemq/output"/>  12. <setBody>  13. <simple>  14.           FileMovedEvent(file: ${file:name}, timestamp: ${date:now:hh:MM:ss.SSS})  15. </simple>  16. </setBody>  17. <to uri="activemq://events" />  18. </route>  19. </camelContext>  20. </blueprint>

本文件及上述study_one.xml最大的区别在于,该文件对应的路由不再直接记录move日志,而是以消息的形式将待记录的日志内容发送到MQ。 2、创建第二个路由,负责从MQ中获取数据,然后将数据move访问日志。

配置文件study_MQ2.xml的内容如下:

1. <?xml version="1.0" encoding="utf-8"?>  2. <blueprint  3. xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5. xsi:schemaLocation="  6.   http://www.osgi.org/xmlns/blueprint/v1.0.0  7. >  8. <camelContext xmlns="http://camel.apache.org/schema/blueprint">  9. <route>  10. <from uri="activemq://events"/>  11. <to uri="log:events"/>  12. </route>  13. </camelContext>  14. </blueprint>

发布路由文件后,通过log:display命令可以查看activemq/input对应的日志信息。

MQ最大的特点是发送数据和获取数据的一方完全透明,两者之间不需要建立连接。当study_MQ2.xml路由关闭时,如果将ctivemq/input中的copy文件发送到activemq/input中,日志信息仍将发送到MQ中,MQ2启动时仍将获取这些信息。

上一篇 Spring Security教程-实现Form表单认证
下一篇 基于CUDA的GPU计算PI值

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题