订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
JAVA技术

事件监听器分发通知设计

在CJW BLOG系统(http://blog.chinajavaworld.com/)中,全文搜索管理器定时对最新日志进行文件索引。
问题:当用户删除一篇日志时,在全文搜索时,该文章仍然计算到搜索的总数中。

解决方案:
通过加入事件监听分发机制,当用户删除日志时,通知到全文搜索管理器。


1.建立日志事件类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class EntryEvent
    implements CJWEvent
{
 
    public static final int ENTRY_ADDED = 100;
    public static final int ENTRY_DELETED = 101;
    public static final int ENTRY_MODIFIED = 102;
    public static final int ENTRY_MODERATION_MODIFIED = 103;
    public static final int ENTRY_RATED = 104;
    public static final int ENTRY_MOVED = 105;
    private int eventType;
    private Entry entry;
    private Date date;
    private Map params;
 
    public EntryEvent(int eventType, Entry entry, Map params)
    {
        this.eventType = eventType;
        this.entry = entry;
        this.params = params != null ? Collections.unmodifiableMap(params) : null;
        date = new Date();
    }
 
    public int getEventType()
    {
        return eventType;
    }
 
    public Entry getEntry()
    {
        return entry;
    }
 
    public Map getParams()
    {
        return params;
    }
 
    public Date getDate()
    {
        return date;
    }


2.建立日志监听接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public interface EntryListener
{
 
    public abstract void entryAdded(EntryEvent entryevent);
 
    public abstract void entryDeleted(EntryEvent entryevent);
 
    public abstract void entryMoved(EntryEvent entryevent);
 
    public abstract void entryModified(EntryEvent entryevent);
 
    public abstract void entryModerationModified(EntryEvent entryevent);
 
    public abstract void entryRated(EntryEvent entryevent);
}


3.建立事件分发处理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
public class EntryEventDispatcher
{
    static Logger logger = Logger.getLogger(EntryEventDispatcher.class.getName());
    private static EntryEventDispatcher instance = new EntryEventDispatcher();
    private ArrayList listeners;
 
    public static EntryEventDispatcher getInstance()
    {
        return instance;
    }
 
    private EntryEventDispatcher()
    {
        listeners = new ArrayList();
        List listenerList = CJWGlobals.getCJWProperties("eventListeners.MessageListener");
        for(int i = 0; i < listenerList.size(); i++)
        {
            String listenerStr = (String)listenerList.get(i);
            try
            {
                EntryListener listener = (EntryListener)ClassUtils.forName(listenerStr).newInstance();
                listeners.add(listener);
            }
            catch(Exception e)
            {
                logger.error("Error loading MessageListener", e);
            }
        }
 
    }
 
    public synchronized void addListener(EntryListener listener)
    {
        if(listener == null)
        {
            throw new NullPointerException();
        } else
        {
            listeners.add(listener);
            return;
        }
    }
 
    public synchronized void removeListener(EntryListener listener)
    {
        listeners.remove(listener);
    }
 
    public void dispatchEvent(EntryEvent event)
    {
        Profiler.begin("msgDispatchEvent");
        int eventType = event.getEventType();
        for(int i = 0; i < listeners.size(); i++)
            try
            {
                EntryListener listener = (EntryListener)listeners.get(i);
                String name = listener.getClass().getName();
                name = name.substring(name.lastIndexOf('.') + 1);
                Profiler.begin(name);
                switch(eventType)
                {
                case EntryEvent.ENTRY_ADDED: 
                    listener.entryAdded(event);
                    break;
 
                case EntryEvent.ENTRY_DELETED: 
                    listener.entryDeleted(event);
                    break;
 
                case EntryEvent.ENTRY_MODIFIED: 
                    listener.entryModified(event);
                    break;
 
                case EntryEvent.ENTRY_MODERATION_MODIFIED: 
                    listener.entryModerationModified(event);
                    break;
 
                case EntryEvent.ENTRY_RATED: 
                    listener.entryRated(event);
                    break;
 
                case EntryEvent.ENTRY_MOVED: 
                    listener.entryMoved(event);
                    break;
                }
                Profiler.end(name);
            }
            catch(Exception e)
            {
                logger.error(e);
            }
 
        Profiler.end("msgDispatchEvent");
    }


4.全文搜索管理器实现日志监听接口EntryListener,同时在初始化时将该监听器加入日志事件分发处理器中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DbSearchManager
    implements SearchManager, CJWManager, EntryListener, TagListener{
...
    public void entryDeleted(EntryEvent event)
    {
        Entry message = event.getEntry();
        removeFromIndex(message);
    }
 
    public synchronized void initialize(){
...
        EntryEventDispatcher.getInstance().addListener(this);
...
}
...
 
}


5.在删除日志时,分发事件,这样所有监听日志的管理器都能收到通知进行处理

1
2
3
4
5
6
7
8
9
...
public void deleteEntry(Entry entry){
...
        //事件分发,通知全文索引管理器进行删除
     EntryEvent event = new EntryEvent(EntryEvent.ENTRY_DELETED,entry,null);
       EntryEventDispatcher.getInstance().dispatchEvent(event);
...
}
...


平均得分
(0 次评分)





文章来自: 本站原创
标签: 事件 event 
评论: 12 | 查看次数: 2295
  • 共有 12 条评论
游客 [2008-08-25 22:58:09]
游客 [2008-08-22 15:26:47]
游客 [2008-08-11 13:19:21]
游客 [2008-08-11 13:18:55]
游客 [2008-08-11 13:18:26]
For those who are inexperienced with purchasing the lotro gold,lotro accountsand lotro powerleveling from lotro goldsellers,there are many lotro gold sellers that are frauds and scams that can not be removed from the internet.Firstly.reviewsof the lord of the rings online gold sellers and hearing out what other customers have to see can be very helpful.
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena

lotro gold
lotro gold for sale
lord of the rings online gold
游客 [2008-08-07 13:31:28]
游客 [2008-08-07 13:31:02]
For those who are inexperienced with purchasing the lotro gold,lotro accountsand lotro powerleveling from lotro goldsellers,there are many lotro gold sellers that are frauds and scams that can not be removed from the internet.Firstly.reviewsof the lord of the rings online gold sellers and hearing out what other customers have to see can be very helpful.
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena
wow gold
buy wow gold
lotro gold
aoc gold
lineage 2 adena

lotro gold
lotro gold for sale
lord of the rings online gold
游客 [2008-08-06 14:16:47]
游客 [2008-08-06 09:37:41]
精油
论文发表
上海翻译公司
上海翻译
英语培训
英语口语
神经性皮炎
皮炎
湿疹
荨麻疹
慢性荨麻疹
藏獒
液压缸
油缸
破碎机
北京旅游
北京旅行社
条码机
条码打印机
条形码打印机
阴茎增大
伟哥
发酵罐
肠炎
结肠炎
直肠炎
慢性肠炎
慢性结肠炎
结肠炎的治疗
溃疡性结肠炎
慢性结肠炎的治疗
大豆床上用品
保健内衣
羊绒内衣
大豆纤维面料
团购礼品
移民
热电偶插头
测温线
热电阻
煤气发生炉
两段式煤气发生炉
环保节能型煤气发生炉
硅碳棒
吸塑机
纸管机
无缝管
合金管
无缝管
无缝钢管
高血压
产品设计
无纸记录仪
红外测温仪
无纸记录仪
韩国服装
韩版服装
韩国服饰
丝锥
挤压丝锥
非标丝锥
梯形丝锥
螺纹环规
节能胶带机
胶带机价格
太阳能
太阳能热水器
分体式太阳能
二手车
北京二手车
北京二手车交易
北京二手汽车
二手车汽车
山特ups电源
山特ups
山特
山特ups报价
ups电源
ups
全自动表面张力仪/界面张力仪
inflatable
bouncer
men spa beijing
men massage beijing
pearl jewelry
Beijing Tour
china Tour
beijing Tour
china Tour
beijing Tour
China Necklace Wholesale
China Bracelet Wholesale
China Ring wholesale
China gemstone beads wholesale
China Jewelry Accessories wholesale
China Semiprecious beads wholesale
replica handbag
replica tiffany
replica watches
louis vuitton replica
chanel replica
gucci replica
Chinese language
Chinese learn
learning Chinese
learn mandarin
ecosway
gasifier
coal gas
coal gasification
pro dj cases
beijing tour
beijing tours
beijing travel
beijing tours
china tour
beijing
china tours
china travel
beijing china
china beijing
beijing hotel
beijing hotels
China Flights
carved fireplace
stone bathtub
marble fountain
marble bench
marble fireplace
marble sculpture
marble columns
marble lions
marble doorway
marble gazebo
marble pillar
marble fireplace surround
marble statue
marble bathtub
韩版服装
时尚衣橱
韩派服装
韩国流行服装
韩国流行服饰
流行服饰
流行服装
时尚起义
游客 [2008-07-25 13:55:48]
移民
投资移民
加拿大移民
技术移民
移民加拿大
澳洲技术移民
德国移民
移民澳洲
澳洲移民
出国移民
移民出国
英国移民
澳大利亚移民
加拿大投资移民
加拿大技术移民
美国留学
法国留学
北欧留学
瑞典留学
芬兰留学
澳洲留学
除湿机
抽湿机
工业除湿机
空气净化器
空气净化机
步进电机
联轴器
真空泵
工作服
职业装
北京工作服
定做工作服
北京二手空调回收
空调维修
物资回收
防腐设备
风机
铠装热电偶
精密铸造
美术培训
美术高考
美术高考培训
画室
北京画室
谐波治理
无功补偿
防腐管道
英美制丝锥
继电保护测试仪
日语学校
日语培训
安装卫星电视
安装卫星天线
北京安装卫星电视
北京安装卫星天线
针孔摄像机
望远镜
夜视仪
探测狗
窃听器
无线耳机
屏蔽器
金属探测器
隔墙监听器
国标舞
拉丁舞
喷码机
针孔摄像机
烤瓷牙
除沫器
土壤水分速测仪
土壤水分测定仪
土壤水分测量仪
土壤墒情记录仪
农药残留速测仪
土壤化肥速测仪
土壤养分测试仪
信号隔离器
信号分配器
隔离器
温度变送器
电流变送器
配电器
隔离配电器
隔离模块
糖尿病足
煤气发生炉
高低温试验箱
振动试验台
恒温恒湿试验箱
恒温恒湿箱
恒温箱
振动台
盐雾箱
老化台
盐雾试验箱
高低温箱
低温试验箱
振动试验机
合同纠纷
房产纠纷
劳动纠纷
房地产律师
制氮机
在职研究生
液体壁纸
清水模板
冷弯型钢
roll forming
开口闪点仪
凝固点仪
闭口闪点仪
运动粘度仪
粘度仪
抗乳化测定仪
丝网除沫器
气液过滤网
除雾器
丝网除雾器
波纹填料
三菱电机空调
牛仔服
牛仔服装厂
牛仔休闲
牛仔裤
牛仔品牌
牛仔专卖店
虹吸
虹吸雨水
虹吸排水
有压流
同层排水
walk throught metal detector
恒温器
马达保护器
热保护器
温度开关
温控器
过流保护器
藏獒
Google左侧优化
舞台设计
铅丝笼
石笼网
烧烤网
振动筛网
拖链
光纤熔接机
光缆监测系统
光时域反射仪
OTDR
nike shoes
air jordan
游客 [2008-07-24 05:56:08]
游客 [2008-07-20 01:40:00]
提示:使用观察家模式即可完成此功能。
1
2
3
// 对应接口和类
java.util.Observer
java.util.Observable
  • 共有 12 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启