监控工具Camel框架的快速认识和使用

2023-10-30 18:58

本文主要是介绍监控工具Camel框架的快速认识和使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Camel流程框架是Apache下的一个开源项目,是较为成熟的流程框架。在web项目中也可以无缝地集成于Spring当中。

 

一、简单使用

 

引入camel相关的jar包:camel-core-2.10.4.jar。

1、经典的入门示例——文件移动

public class FileMoveWithCamel {public static void main(String[] args) {try{CamelContext camelCtx = new DefaultCamelContext();camelCtx.addRoutes(new RouteBuilder() {//此示例中,只能转移文件,而无法转移目录@Overridepublic void configure() throws Exception {from("file:f:/tmp/inbox?delay=30000").to("file:f:/tmp/outbox");}});camelCtx.start();boolean loop = true;while(loop) {Thread.sleep(25000);}System.out.println("循环完毕");camelCtx.stop();} catch(Exception ex) {ex.printStackTrace();}}

 其中file:类似于http://,是camel的协议组件。camel支持的组件还包括:bean browse dataset direct file log mock properties seda test timer stub validator vm xlst等。其中常用的有bean和direct以及file

运行上述程序,会发现file:f:/tmp/inbox下的文件被转移到file:f:/tmp/outbox了,并且在file:f:/tmp/inbox会生成一个.camel文件夹存放刚才被转移的文件。

 

2、入门示例二——带Processor处理

先定义一个处理器,实现org.apache.camel.Processor接口

public class FileConvertProcessor implements Processor {@Overridepublic void process(Exchange exchange) throws Exception {
//        Object obj = exchange.getIn().getBody(); //如果是getBody()则返回一个Object//如果是getBody(Class<T>)则返回T类型的实例InputStream body = exchange.getIn().getBody(InputStream.class);
//        System.out.println("进入:" + body);BufferedReader br = new BufferedReader(new InputStreamReader(body, "UTF-8"));StringBuilder sb = new StringBuilder("");String str;while((str = br.readLine()) != null) {System.out.println(str);sb.append(str + " ");}exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt");exchange.getOut().setBody(sb.toString());System.out.println("body:" + exchange.getOut().getBody());}

加上处理器后处理文件的程序

public class FileProcessWithCamel {public static void main(String[] args) {try{CamelContext camelCtx = new DefaultCamelContext();camelCtx.addRoutes(new RouteBuilder() {@Overridepublic void configure() throws Exception {FileConvertProcessor processor = new FileConvertProcessor();//noop表示等待、无操作from("file:f:/tmp/inbox?noop=true").process(processor).to("file:f:/tmp/outbox");}});camelCtx.start();boolean loop = true;
//死循环表示挂起camel上下文,以便持续监听while(loop) {Thread.sleep(25000);}camelCtx.stop();} catch(Exception ex) {ex.printStackTrace();}}
}


 运行程序后将用一行打印file:f:/tmp/inbox下的文件的多行内容,并在file:f:/tmp/outbox下生成名为converted.txt的文件。该文件的内容即为file:f:/tmp/inbox下的文件的多行内容显示成的一行。

 

这里要特别注意getIn(setIn),getOut(setOut)怎么用:先看下面一张图

 这张图表明了:假设有流程A->B->C->D->……则A在处理完毕之后给B的话,A必须setOut结果,然后B要取流程中的“上一个”节点(即A)的结果则必须getIn取结果再处理,以此类推……A不能setIn结果,否则B getIn的话会取不到A set的结果。

 

二、集成在Spring当中

 需引入camel-core-2.10.4.jar camel-spring-2.10.4.jar

 

对已第一部分的第一示例,若在Spring中配置,并设置它随着web项目的启动而启动,则可以这样写:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:drools="http://drools.org/schema/drools-spring"xmlns:camel="http://camel.apache.org/schema/spring"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"><bean id="fileConverter" class="com.xxxx.FileConvertProcessor" /><camelContext id="camel" autoStartup="true" xmlns="http://camel.apache.org/schema/spring">     <route>  <from uri="file:f:/tmp/inbox?delay=30000"/>  <process ref="fileConverter"/>  <to uri="file:f:/tmp/outbox"/> </route> </camelContext>
</beans>

除此之外,其实更为常见的是一个处理流程往往需要经过很多个bean类。而查看camel direct组件的用法:

In the route below we use the direct component to link the two routes together:

可知bean与bean之间的流程连接用的是direct,这里不妨用fileConverter和fileConverter2两个Processor测试(它们的具体定义省略,都得实现Processor接口),于是:

 <bean id="fileConverter" class="com.xxx.FileConvertProcessor" /><bean id="fileConverter2" class="com.xxx.FileConvertProcessor2" /><camelContext id="camel" autoStartup="true" xmlns="http://camel.apache.org/schema/spring"><template id="producerTemplate" /><threadPool id="pool" threadName="Thread-dataformat"poolSize="50" maxPoolSize="200" maxQueueSize="250" rejectedPolicy="CallerRuns" /><route>  <from uri="file:f:/tmp/inbox?delay=30000"/>  <process ref="fileConverter"/>  <to uri="file:f:/tmp/outbox"/> <to uri="direct://start"/>   </route> <route> <from uri="direct://start"/>  <threads executorServiceRef="pool"><process ref="fileConverter"/> <to uri="bean://fileConverter2"/>  </threads> </route></camelContext>

        注意到上面第二个路由<route  />中to的配置被放在一个线程池当中了,这也是比较常见的用法。这里表明流程经过fileConverter处理,流向fileConverter2继续处理。

 

另外,我们常常需要根据某一条件判断流程的“下一步”应该走向哪里,这时候就要用到类似el表达式的if else判断了。再定义一个fileConverter3,表明根据条件选择——流程在经过fileConverter时,根据配置条件选择“下一步”流向fileConverter2还是fileConverter3(fileConverter2和fileConverter3定义省略,它们都得实现Processor接口)

<bean id="fileConverter" class="com.xxxx.FileConvertProcessor" /><bean id="fileConverter2" class="com.xxx.FileConvertProcessor2" /><bean id="fileConverter3" class="com.xxx.FileConvertProcessor3" /><camelContext id="camel" autoStartup="true" xmlns="http://camel.apache.org/schema/spring"><template id="producerTemplate" /><threadPool id="pool" threadName="Thread-dataformat"poolSize="50" maxPoolSize="200" maxQueueSize="250" rejectedPolicy="CallerRuns" /><route>  <from uri="file:f:/tmp/inbox?delay=30000"/>  <process ref="fileConverter"/>  <to uri="file:f:/tmp/outbox"/> <to uri="direct://start"/>   </route> <route> <from uri="direct://start"/>  <threads executorServiceRef="pool"><choice><when><simple>${body.length} > 40</simple><process ref="fileConverter"/> <to uri="bean://fileConverter2"/>  </when><otherwise><process ref="fileConverter"/> <to uri="bean://fileConverter3"/></otherwise></choice></threads> </route>
</camelContext>


 <choice />和<otherwise />即表示选择分支,而<when />下的<simple />标签则用来放判断条件,写法和el表达式的条件判断很类似。

注意Camel流程的开始时,应该在Java代码中用ProducerTemplate.sendBody("direct://xxx",data)开始流程的源头。ProducerTemplate是从配置文件的bean获取(<template id="producerTemplate" />)的,然后ProducerTemplate.start()启动Camel流程。然后在配置文件中通过<from uri="direct://xxx"/>开始接收流程传过来的data数据。
--------------------- 

转自 https://blog.csdn.net/qq_18875541/article/details/69391267 

其它参考文档 http://www.uml.org.cn/zjjs/201801193.asp

这篇关于监控工具Camel框架的快速认识和使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/309943

相关文章

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、