Java设计模式应用——过滤器模式
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设计模式应用——过滤器模式的更多相关文章
- Java设计模式——装饰者模式
JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...
- 浅析JAVA设计模式之工厂模式(一)
1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...
- JAVA设计模式--装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- 折腾Java设计模式之建造者模式
博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...
- 折腾Java设计模式之备忘录模式
原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...
- 折腾Java设计模式之状态模式
原文地址 折腾Java设计模式之状态模式 状态模式 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.在状态模式中,我们创建表示各种状态的对象 ...
- 折腾Java设计模式之模板方法模式
博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...
- 折腾Java设计模式之访问者模式
博客原文地址:折腾Java设计模式之访问者模式 访问者模式 Represent an operation to be performed on the elements of an object st ...
- 折腾Java设计模式之命令模式
博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...
随机推荐
- node项目部署相关问题
process.env process.env属性返回一个对象,包含了当前Shell的所有环境变量. 通常的做法是,新建一个环境变量NODE_ENV,用它确定当前所处的开发阶段,生产阶段设为produ ...
- 《modern-php》 - 阅读笔记 - 最佳实践
过滤.验证和转义数据 过滤数据 不要相信任何外部数据! 常见的有以下几种数据需要过滤:HTML,SQL查询,用户提交的信息(邮件地址.电话号码.身份证) HTML htmlentities() HTM ...
- 关注被关注表设计-UserFollowMapping
-- ---------------------------- -- Table structure for UserFollowMapping -- ------------------------ ...
- XTU 1261 - Roads - [最小割][2017湘潭邀请赛B题(江苏省赛)]
之前在网上搜了一个下午没搜到这道题的题解,然后同时又对着叉姐写的两行字题解看了一个下午: 虽然基本上已经知道了这题的思路,但愣是因为自己代码实现起来太繁复,外加不确定正确性,没敢码…… 但是一道题肝了 ...
- Cloudrea manager5安装CDH5文档
一.主机规划.存储规划 服务器配置信息:CentOS6.5 最小化安装+development tools组包,其余组件yum安装即可. 二.系统设置如下: 1.服务器信息如下(/etc/hosts文 ...
- php:// — 访问各个输入/输出流(I/O streams)
PHP: php:// - Manual http://www.php.net/manual/zh/wrappers.php.php php:// php:// — 访问各个输入/输出流(I/O st ...
- 2018/03/27 每日一个Linux命令 之 cron
Cron 用于配置定时任务. -- 环境为 Ubuntu16-04 -- 先说说怎么配置一个简单的定时任务.直观的可以看到效果. 之前在网上查找资料,对Shell编程不熟悉的实在是很头疼,走了不少弯路 ...
- Rotate Image(二位数组顺时针旋转)
问题描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- 【python-opencv】15-图像阈值
[微语]立志要如山,行道要如水.不如山,不能坚定,不如水,不能曲达 import cv2 as cv import numpy as np from matplotlib import pyplot ...
- LeetCode 第 338 题 (Counting Bits)
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...