概述
storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中。
实际上,中间告警结果可能有重复告警、错误告警、无用告警,告警入库服务会过滤,压缩中间告警,把用户关心的告警存入数据库。过滤的步骤较多,并且客户关心的告警可能会随时变化,写死的告警过滤很快就无法满足应用场景,这种场景下使用过滤器模式则很好满足业务上的不确定性欲扩展性。
告警入库服务涉及消息过滤和告警过滤,下面我们以消息过滤器来讲一下过滤器模式。
1、消息过滤器接口
packagecom.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{public intpriority()
{return 0;
}public abstract booleandoFilter(Message msg);public intcompareTo(IMessageFilter arg0)
{return priority() -arg0.priority();
}
}
2、时间过滤器
packagecom.coshaho.learn.filter;importorg.springframework.stereotype.Component;/***
* TimeFilter.java Create on 2017年5月13日 上午12:44:25
*
* 类功能说明: 时间过滤
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho*/@Componentpublic class TimeFilter extendsIMessageFilter
{public intpriority()
{return 1;
}public booleandoFilter(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、级别过滤器
packagecom.coshaho.learn.filter;importorg.springframework.stereotype.Component;/***
* ServerityFilter.java Create on 2017年5月13日 上午12:44:13
*
* 类功能说明: 级别过滤
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho*/@Componentpublic class ServerityFilter extendsIMessageFilter
{public intpriority() {return 2;
}public booleandoFilter(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、类型过滤器
packagecom.coshaho.learn.filter;importorg.springframework.stereotype.Component;/***
* TypeFilter.java Create on 2017年5月13日 上午12:44:36
*
* 类功能说明: 类型过滤
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho*/@Componentpublic class TypeFilter extendsIMessageFilter{public intpriority()
{return 3;
}public booleandoFilter(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、消息监听服务
packagecom.coshaho.learn.filter;importjava.util.Collections;importjava.util.List;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.springframework.stereotype.Component;/***
* MessageListener.java Create on 2017年5月13日 上午12:44:49
*
* 类功能说明: 告警消息监听
*
* Copyright: Copyright(c) 2013
* Company: COSHAHO
* @Version 1.0
* @Author coshaho*/@Componentpublic classMessageListener
{public static voidmain(String[] args)
{
@SuppressWarnings("resource")
ApplicationContext context= new ClassPathXmlApplicationContext("classpath:spring.xml");
MessageListener listener= (MessageListener)context.getBean("messageListener");
Message msg= newMessage();
msg.setHour(12);
msg.setName("coshaho");
msg.setType(5);
msg.setSeverity(3);
listener.listen(msg);
}public voidlisten(Message msg)
{
excute(msg);
}private booleanexcute(Message msg)
{
@SuppressWarnings("unchecked")
List filters = (List)SpringUtils.getBeansOfType(IMessageFilter.class);
Collections.sort(filters);for(IMessageFilter filter : filters)
{if(!filter.doFilter(msg))
{return false;
}
}return true;
}
}
6、Spring工具类
packagecom.coshaho.learn.filter;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importorg.springframework.beans.BeansException;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ApplicationContextAware;importorg.springframework.stereotype.Service;
@Servicepublic class SpringUtils implementsApplicationContextAware
{private staticApplicationContext context;public voidsetApplicationContext(ApplicationContext context)throwsBeansException
{
SpringUtils.context=context;
}
@SuppressWarnings({"rawtypes", "unchecked"})public staticList getBeansOfType(Class clazz)
{
Map map=context.getBeansOfType(clazz);return newArrayList(map.values());
}
}
7、消息类
packagecom.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 classMessage
{privateString name;private inthour;private inttype;private intseverity;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetHour() {returnhour;
}public void setHour(inthour) {this.hour =hour;
}public intgetType() {returntype;
}public void setType(inttype) {this.type =type;
}public intgetSeverity() {returnseverity;
}public void setSeverity(intseverity) {this.severity =severity;
}
@OverridepublicString toString() {return "Message [name=" + name + ", hour=" + hour + ", type=" +type+ ", severity=" + severity + "]";
}
}
可以看到,过滤器模式可以很方便的扩展过滤业务。
最后
以上就是无语小霸王为你收集整理的Filter java模式_Java设计模式应用——过滤器模式的全部内容,希望文章能够帮你解决Filter java模式_Java设计模式应用——过滤器模式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复