storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中。

实际上,中间告警结果可能有重复告警、错误告警、无用告警,告警入库服务会过滤,压缩中间告警,把用户关心的告警存入数据库。过滤的步骤较多,并且客户关心的告警可能会随时变化,写死的告警过滤很快就无法满足应用场景,这种场景下使用过滤器模式则很好满足业务上的不确定性欲扩展性。

告警入库服务涉及消息过滤和告警过滤,下面我们以消息过滤器来讲一下过滤器模式。

1、消息过滤器接口

package com.coshaho.learn.filter;

/**
*
* IMessageFilter.java Create on 2017年5月13日 上午12:43:56
*
* 类功能说明: 告警消息过滤器
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho
*/
public abstract class IMessageFilter implements Comparable<IMessageFilter>
{
public int priority()
{
return 0;
} public abstract boolean doFilter(Message msg); public int compareTo(IMessageFilter arg0)
{
return priority() - arg0.priority();
} }

2、时间过滤器

package com.coshaho.learn.filter;

import org.springframework.stereotype.Component;

/**
*
* TimeFilter.java Create on 2017年5月13日 上午12:44:25
*
* 类功能说明: 时间过滤
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho
*/
@Component
public class TimeFilter extends IMessageFilter
{ public int priority()
{
return 1;
} public boolean doFilter(Message msg)
{
if(msg.getHour() < 8 || msg.getHour() > 17)
{
System.out.println("Time filter false, message is " + msg);
return false;
}
System.out.println("Time filter true, message is " + msg);
return true;
} }

3、级别过滤器

package com.coshaho.learn.filter;

import org.springframework.stereotype.Component;

/**
*
* ServerityFilter.java Create on 2017年5月13日 上午12:44:13
*
* 类功能说明: 级别过滤
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho
*/
@Component
public class ServerityFilter extends IMessageFilter
{ public int priority() {
return 2;
} public boolean doFilter(Message msg) {
if(msg.getSeverity() == 0)
{
System.out.println("Severity filter false, message is " + msg);
return false;
}
System.out.println("Severity filter true, message is " + msg);
return true;
} }

4、类型过滤器

package com.coshaho.learn.filter;

import org.springframework.stereotype.Component;

/**
*
* TypeFilter.java Create on 2017年5月13日 上午12:44:36
*
* 类功能说明: 类型过滤
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho
*/
@Component
public class TypeFilter extends IMessageFilter{ public int priority()
{
return 3;
} public boolean doFilter(Message msg) { if(msg.getType() < 3)
{
System.out.println("Type filter false, message is " + msg);
return false;
}
System.out.println("Type filter true, message is " + msg);
return false;
} }

5、消息监听服务

package com.coshaho.learn.filter;

import java.util.Collections;
import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; /**
*
* MessageListener.java Create on 2017年5月13日 上午12:44:49
*
* 类功能说明: 告警消息监听
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho
*/
@Component
public class MessageListener
{
public static void main(String[] args)
{
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
MessageListener listener = (MessageListener)context.getBean("messageListener");
Message msg = new Message();
msg.setHour(12);
msg.setName("coshaho");
msg.setType(5);
msg.setSeverity(3);
listener.listen(msg);
} public void listen(Message msg)
{
excute(msg);
} private boolean excute(Message msg)
{
@SuppressWarnings("unchecked")
List<IMessageFilter> filters = (List<IMessageFilter>)SpringUtils.getBeansOfType(IMessageFilter.class);
Collections.sort(filters);
for(IMessageFilter filter : filters)
{
if(!filter.doFilter(msg))
{
return false;
}
}
return true;
}
}

6、Spring工具类

package com.coshaho.learn.filter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service; @Service
public class SpringUtils implements ApplicationContextAware
{
private static ApplicationContext context; public void setApplicationContext(ApplicationContext context)
throws BeansException
{
SpringUtils.context = context;
} @SuppressWarnings({ "rawtypes", "unchecked" })
public static List getBeansOfType(Class clazz)
{
Map map = context.getBeansOfType(clazz);
return new ArrayList(map.values());
}
}

7、消息类

package com.coshaho.learn.filter;

/**
*
* Message.java Create on 2017年5月13日 上午12:43:37
*
* 类功能说明: 告警消息
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho
*/
public class Message
{
private String name; private int hour; private int type; private int severity; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getHour() {
return hour;
} public void setHour(int hour) {
this.hour = hour;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public int getSeverity() {
return severity;
} public void setSeverity(int severity) {
this.severity = severity;
} @Override
public String toString() {
return "Message [name=" + name + ", hour=" + hour + ", type=" + type
+ ", severity=" + severity + "]";
}
}

可以看到,过滤器模式可以很方便的扩展过滤业务。

Java设计模式应用——过滤器模式的更多相关文章

  1. Java设计模式——装饰者模式

    JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...

  2. 浅析JAVA设计模式之工厂模式(一)

    1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...

  3. JAVA设计模式--装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

  4. 折腾Java设计模式之建造者模式

    博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...

  5. 折腾Java设计模式之备忘录模式

    原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...

  6. 折腾Java设计模式之状态模式

    原文地址 折腾Java设计模式之状态模式 状态模式 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.在状态模式中,我们创建表示各种状态的对象 ...

  7. 折腾Java设计模式之模板方法模式

    博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...

  8. 折腾Java设计模式之访问者模式

    博客原文地址:折腾Java设计模式之访问者模式 访问者模式 Represent an operation to be performed on the elements of an object st ...

  9. 折腾Java设计模式之命令模式

    博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...

随机推荐

  1. Yet another way to manage your NHibernate ISessionFactory

    So here is my current UnitOfWork implementation.  This one makes use of the somewhat new current_ses ...

  2. python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

    一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...

  3. 【Android】Android import和export使用说明 及 export报错:jarlist.cache: Resource is out of sync with the file syst解决

    在Android开发export项目时发现有时会报错,内容如下: Problems were encountered during export:  Error exporting PalmIdent ...

  4. tomcat+redis会话共享

    1.基础环境: jdk1. tomcat7 redis nginx 2.添加依赖的jar包到tomcat的lib目录(http://pan.baidu.com/s/1eRAwN0Q) 3.配置tomc ...

  5. cp命令取消提示的方法

    Linux默认cp命令带参数-i如果有重复的文件会提示覆盖 查看cp别名 在大量复制的时候这个提示不友好,在脚本写复制命令也无法使用交互式输入 解决办法 1,修改别名 vi ~/.bashrc 注释掉 ...

  6. htop详解

    一.Htop的使用简介 大家可能对top监控软件比较熟悉,今天我为大家介绍另外一个监控软件Htop,姑且称之为top的增强版,相比top其有着很多自身的优势.如下: 两者相比起来,top比较繁琐 默认 ...

  7. python 几个重要的概念

    转自:http://www.cnblogs.com/aylin/p/5601969.html

  8. Dungeon Master---2251(bfs)

    http://poj.org/problem?id=2251 有一个三维的牢房地图 求从S点走E点的最小时间: #include<stdio.h> #include<string.h ...

  9. Find The Multiple--POJ1426

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  10. 谷歌技术"三宝"之MapReduce(转)

    原文:http://blog.csdn.net/opennaive/article/details/7514146   目录 MapReduce是干啥的 例子统计词频 map函数和reduce函数 M ...