Esper入门简介:四、 由上篇三我们实现个简单的报警作用吧

/**
	 * Chapter 3. Processing Model
	 * 3.7. Aggregation and Grouping
	 * 订阅者实现监听
	 * 
	 * @see http://www.espertech.com/esper/release-5.2.0/esper-reference/html_single/index.html#processingmodel_aggregation
	 *      http://www.espertech.com/esper/release-5.2.0/esper-reference/html_single/index.html#config-variables
	 *      http://www.espertech.com/esper/release-5.2.0/esper-reference/html_single/index.html#variable_using
	 *      http://www.espertech.com/esper/release-5.2.0/esper-reference/html_single/index.html#config-engine-variables
	 * 
	 * @param eventBean
	 * @return
	 * @throws InterruptedException
	 */
	@Test
	public void test_() throws InterruptedException {
		HttpLog httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		TimeUnit.SECONDS.sleep(10);

		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
		httpLog = new HttpLog(1, UUID.randomUUID().toString(), "www.baidu.com/tieba", "www.baidu.com", "userAgent", LocalDateTime.now());
		esperTemplateBean.sendEvent(httpLog);
	}


在Esper的配置文件中我们定义了一个变量,其配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<esper-configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.espertech.com/schema/esper" xsi:schemaLocation="
http://www.espertech.com/schema/esper
http://www.espertech.com/schema/esper/esper-configuration-2.0.xsd">

	<!-- <event-type name="StockTick" class="com.espertech.esper.example.stockticker.event.StockTick" /> -->
	<!-- <event-type name="PriceLimit" class="com.espertech.esper.example.stockticker.event.PriceLimit" /> -->
	<event-type-auto-name package-name="com.doctor.esper.event" />
	<variable name="var_threshold" type="int" initialization-value="6" constant="true" />
	<engine-settings>
		<defaults>
			<stream-selection>
				<stream-selector value='ISTREAM' />
			</stream-selection>

			<execution fairlock="true" />

		</defaults>
		
	</engine-settings>
	
s</esper-configuration>

    <variable name="var_threshold" type="int" initialization-value="6" constant="true" /> 定义了一个变量。

esper-spring配置文件:

	<bean id="thresholdHttpLogStatement" class="com.doctor.esper.spring.EsperStatement"
  		  p:subscriber-ref="httpLogSubscriber"
  		  p:subscriberMethodName="update"
		  c:epl="
	      select log.*,count(*)
	      from HttpLog.win:time(10 sec) as log
	      group by id
	      having count(*)  >= var_threshold  " />

上面的EPL表达式表示:分析10秒内的HttpLog,如果这10秒呢,id出现6次,就触发监听器,这里用订阅者,起到报警,这里只是用日志输出。


输出内容:

06-20 18:03:11.027 main  WARN  c.d.e.s.HttpLogSubscriber -    报警阈值httplog{"id":1,"machineId":"2891816a-9272-4c11-b510-8481ea829778","referer":"www.baidu.com","requestPath":"www.baidu.com/tieba","time":"2015-06-20T18:03:11.026","userAgent":"userAgent"}超过访问次数6
06-20 18:03:21.132 main  WARN  c.d.e.s.HttpLogSubscriber -    报警阈值httplog{"id":1,"machineId":"830bf9ab-b625-4a38-9e61-3367aa1aa661","referer":"www.baidu.com","requestPath":"www.baidu.com/tieba","time":"2015-06-20T18:03:21.132","userAgent":"userAgent"}超过访问次数6
06-20 18:03:21.133 main  WARN  c.d.e.s.HttpLogSubscriber -    报警阈值httplog{"id":1,"machineId":"571c8281-7a1e-48bd-a10b-200b073468fe","referer":"www.baidu.com","requestPath":"www.baidu.com/tieba","time":"2015-06-20T18:03:21.133","userAgent":"userAgent"}超过访问次数7
 

分析:一开始发送6个HttpLog同样的事件,触发了订阅者,输出一条报警信息,然后休眠10秒。时间窗口内的事件过期了。然后我们继续发送7个同样的事件,最后两个肯定都会报警的(10秒内没老事件过期的)。


注:用了https://github.com/opencredo/opencredo-esper/tree/master/esper-template 中的esper-spring结合代码(并小做了部分调整,没用它的XML配置方式)。

在此,分享下与Esper相关的网站:

esper-2015

esper学习

http://www.espertech.com/esper/

http://josh-persistence.iteye.com/category/307677

https://github.com/opencredo/opencredo-esper

https://github.com/opencredo/esper-custom-aggregator

https://github.com/opencredo/opencredo-esper

https://github.com/pulsarIO/jetstream-esper

https://github.com/pulsarIO

https://github.com/tomdz/storm-esper

https://github.com/corsoft/esper-demo-nuclear

https://github.com/hellojinjie/storm-esper-example

http://esper.13850.n7.nabble.com/Esper-User-list-f3.html

(这些都保存在我的github上了)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值