监控工具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架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

流媒体平台/视频监控/安防视频汇聚EasyCVR播放暂停后视频画面黑屏是什么原因?

视频智能分析/视频监控/安防监控综合管理系统EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。该平台以其强大的视频处理、汇聚与融合能力,在构建全栈视频监控系统中展现出了独特的优势。视频监控管理系统EasyCVR平台内置了强大的视频解码、转码、压缩等技术,能够处理多种视频流格式,并以多种格式(RTMP、RTSP、HTTP-FLV、WebS

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

每天认识几个maven依赖(ActiveMQ+activemq-jaxb+activesoap+activespace+adarwin)

八、ActiveMQ 1、是什么? ActiveMQ 是一个开源的消息中间件(Message Broker),由 Apache 软件基金会开发和维护。它实现了 Java 消息服务(Java Message Service, JMS)规范,并支持多种消息传递协议,包括 AMQP、MQTT 和 OpenWire 等。 2、有什么用? 可靠性:ActiveMQ 提供了消息持久性和事务支持,确保消

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖