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的配置文件中我们定义了一个变量,其配置文件如下:




	
	
	
	
	
		
			
				
			

			

		
		
	
	
s

    定义了一个变量。

esper-spring配置文件:

	

上面的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上了)


你可能感兴趣的:(Esper-cep)