Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
8a4b56d4
C
cat
项目概览
梦中观雨
/
cat
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cat
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8a4b56d4
编写于
8月 24, 2012
作者:
F
Frankie Wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add event and rule to dog-home
上级
d559839e
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
386 addition
and
0 deletion
+386
-0
dog-home/src/main/java/com/dianping/dog/alarm/rule/DefaultRuleContext.java
.../java/com/dianping/dog/alarm/rule/DefaultRuleContext.java
+31
-0
dog-home/src/main/java/com/dianping/dog/alarm/rule/Rule.java
dog-home/src/main/java/com/dianping/dog/alarm/rule/Rule.java
+7
-0
dog-home/src/main/java/com/dianping/dog/alarm/rule/RuleContext.java
...rc/main/java/com/dianping/dog/alarm/rule/RuleContext.java
+9
-0
dog-home/src/main/java/com/dianping/dog/alarm/rule/reactor/AbstractRuleReactor.java
.../dianping/dog/alarm/rule/reactor/AbstractRuleReactor.java
+49
-0
dog-home/src/main/java/com/dianping/dog/alarm/rule/reactor/ReactorRuleContext.java
...m/dianping/dog/alarm/rule/reactor/ReactorRuleContext.java
+16
-0
dog-home/src/main/java/com/dianping/dog/event/DefaultEventDispatcher.java
...n/java/com/dianping/dog/event/DefaultEventDispatcher.java
+29
-0
dog-home/src/main/java/com/dianping/dog/event/DefaultEventListenerRegistry.java
.../com/dianping/dog/event/DefaultEventListenerRegistry.java
+36
-0
dog-home/src/main/java/com/dianping/dog/event/Event.java
dog-home/src/main/java/com/dianping/dog/event/Event.java
+5
-0
dog-home/src/main/java/com/dianping/dog/event/EventDispatcher.java
...src/main/java/com/dianping/dog/event/EventDispatcher.java
+5
-0
dog-home/src/main/java/com/dianping/dog/event/EventListener.java
...e/src/main/java/com/dianping/dog/event/EventListener.java
+5
-0
dog-home/src/main/java/com/dianping/dog/event/EventListenerRegistry.java
...in/java/com/dianping/dog/event/EventListenerRegistry.java
+9
-0
dog-home/src/main/java/com/dianping/dog/event/EventType.java
dog-home/src/main/java/com/dianping/dog/event/EventType.java
+5
-0
dog-home/src/test/java/com/dianping/dog/event/EventTest.java
dog-home/src/test/java/com/dianping/dog/event/EventTest.java
+61
-0
dog-home/src/test/java/com/dianping/dog/rule/RuleTest.java
dog-home/src/test/java/com/dianping/dog/rule/RuleTest.java
+119
-0
未找到文件。
dog-home/src/main/java/com/dianping/dog/alarm/rule/DefaultRuleContext.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.alarm.rule
;
import
java.util.HashMap
;
import
java.util.Map
;
public
class
DefaultRuleContext
implements
RuleContext
{
private
Map
<
String
,
Object
>
m_attributes
=
new
HashMap
<
String
,
Object
>();
@SuppressWarnings
(
"unchecked"
)
@Override
public
<
T
>
T
getAttribute
(
String
name
)
{
return
(
T
)
m_attributes
.
get
(
name
);
}
@SuppressWarnings
(
"unchecked"
)
@Override
public
<
T
>
T
getAttribute
(
String
name
,
T
defaultValue
)
{
Object
value
=
m_attributes
.
get
(
name
);
if
(
value
==
null
)
{
return
defaultValue
;
}
else
{
return
(
T
)
value
;
}
}
@Override
public
void
setAttribute
(
String
name
,
Object
value
)
{
m_attributes
.
put
(
name
,
value
);
}
}
\ No newline at end of file
dog-home/src/main/java/com/dianping/dog/alarm/rule/Rule.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.alarm.rule
;
public
interface
Rule
{
public
String
getName
();
public
boolean
apply
(
RuleContext
ctx
);
}
dog-home/src/main/java/com/dianping/dog/alarm/rule/RuleContext.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.alarm.rule
;
public
interface
RuleContext
{
public
<
T
>
T
getAttribute
(
String
name
);
public
<
T
>
T
getAttribute
(
String
name
,
T
defaultValue
);
public
void
setAttribute
(
String
name
,
Object
value
);
}
dog-home/src/main/java/com/dianping/dog/alarm/rule/reactor/AbstractRuleReactor.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.alarm.rule.reactor
;
import
com.dianping.dog.alarm.rule.DefaultRuleContext
;
import
com.dianping.dog.alarm.rule.Rule
;
import
com.dianping.dog.alarm.rule.RuleContext
;
import
com.dianping.dog.event.Event
;
import
com.dianping.dog.event.EventDispatcher
;
import
com.dianping.dog.event.EventListener
;
import
com.site.lookup.annotation.Inject
;
public
abstract
class
AbstractRuleReactor
<
T
extends
Event
>
implements
EventListener
<
T
>
{
@Inject
private
EventDispatcher
m_dispatcher
;
@Inject
private
Rule
m_rule
;
protected
abstract
Event
createNextEvent
(
RuleContext
ctx
);
protected
T
getEvent
(
RuleContext
ctx
)
{
return
ctx
.
getAttribute
(
"event"
);
}
@Override
public
void
onEvent
(
T
event
)
{
DefaultRuleContext
ctx
=
new
DefaultRuleContext
();
prepare
(
ctx
,
event
);
ctx
.
setAttribute
(
"event"
,
event
);
if
(
m_rule
.
apply
(
ctx
))
{
Event
nextEvent
=
createNextEvent
(
ctx
);
if
(
nextEvent
!=
null
)
{
m_dispatcher
.
dispatch
(
nextEvent
);
}
}
}
protected
abstract
void
prepare
(
RuleContext
ctx
,
T
event
);
protected
void
setDispatcher
(
EventDispatcher
dispatcher
)
{
m_dispatcher
=
dispatcher
;
}
protected
void
setRule
(
Rule
rule
)
{
m_rule
=
rule
;
}
}
dog-home/src/main/java/com/dianping/dog/alarm/rule/reactor/ReactorRuleContext.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.alarm.rule.reactor
;
import
com.dianping.dog.alarm.rule.DefaultRuleContext
;
import
com.dianping.dog.event.Event
;
public
class
ReactorRuleContext
<
T
extends
Event
>
extends
DefaultRuleContext
{
private
T
m_event
;
public
ReactorRuleContext
(
T
event
)
{
m_event
=
event
;
}
public
T
getEvent
()
{
return
m_event
;
}
}
dog-home/src/main/java/com/dianping/dog/event/DefaultEventDispatcher.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
import
java.util.List
;
import
com.dianping.cat.Cat
;
import
com.site.lookup.annotation.Inject
;
public
class
DefaultEventDispatcher
implements
EventDispatcher
{
@Inject
private
EventListenerRegistry
m_registry
;
@Override
public
void
dispatch
(
Event
event
)
{
List
<
EventListener
<
Event
>>
listeners
=
m_registry
.
getListeners
(
event
.
getType
());
if
(
listeners
!=
null
&&
!
listeners
.
isEmpty
())
{
for
(
EventListener
<
Event
>
listener
:
listeners
)
{
try
{
listener
.
onEvent
(
event
);
}
catch
(
Exception
e
)
{
// ignore it
Cat
.
getProducer
().
logError
(
e
);
}
}
}
else
{
Cat
.
getProducer
().
logEvent
(
"UnhandledEvent"
,
event
.
getType
().
getClass
().
getName
(),
"0"
,
null
);
}
}
}
dog-home/src/main/java/com/dianping/dog/event/DefaultEventListenerRegistry.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
DefaultEventListenerRegistry
implements
EventListenerRegistry
{
private
Map
<
EventType
,
List
<
EventListener
<
Event
>>>
m_map
=
new
HashMap
<
EventType
,
List
<
EventListener
<
Event
>>>();
@Override
public
List
<
EventListener
<
Event
>>
getListeners
(
EventType
type
)
{
return
m_map
.
get
(
type
);
}
@SuppressWarnings
(
"unchecked"
)
@Override
public
void
register
(
EventType
type
,
EventListener
<?>
listener
)
{
List
<
EventListener
<
Event
>>
listeners
=
m_map
.
get
(
type
);
if
(
listeners
==
null
)
{
synchronized
(
m_map
)
{
listeners
=
m_map
.
get
(
type
);
if
(
listeners
==
null
)
{
listeners
=
new
ArrayList
<
EventListener
<
Event
>>();
m_map
.
put
(
type
,
listeners
);
}
}
}
synchronized
(
listeners
)
{
listeners
.
add
((
EventListener
<
Event
>)
listener
);
}
}
}
dog-home/src/main/java/com/dianping/dog/event/Event.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
public
interface
Event
{
public
EventType
getType
();
}
dog-home/src/main/java/com/dianping/dog/event/EventDispatcher.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
public
interface
EventDispatcher
{
public
void
dispatch
(
Event
event
);
}
dog-home/src/main/java/com/dianping/dog/event/EventListener.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
public
interface
EventListener
<
T
extends
Event
>
{
public
void
onEvent
(
T
event
);
}
dog-home/src/main/java/com/dianping/dog/event/EventListenerRegistry.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
import
java.util.List
;
public
interface
EventListenerRegistry
{
public
List
<
EventListener
<
Event
>>
getListeners
(
EventType
type
);
public
void
register
(
EventType
type
,
EventListener
<?>
listener
);
}
dog-home/src/main/java/com/dianping/dog/event/EventType.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
public
interface
EventType
{
}
dog-home/src/test/java/com/dianping/dog/event/EventTest.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.event
;
import
junit.framework.Assert
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.JUnit4
;
import
com.site.lookup.ComponentTestCase
;
@RunWith
(
JUnit4
.
class
)
public
class
EventTest
extends
ComponentTestCase
{
private
static
StringBuilder
s_result
=
new
StringBuilder
();
@Test
public
void
test
()
throws
Exception
{
EventListenerRegistry
registry
=
lookup
(
EventListenerRegistry
.
class
);
registry
.
register
(
MockEventType
.
TYPE1
,
MockEventListener
.
INSTANCE
);
EventDispatcher
dispatcher
=
lookup
(
EventDispatcher
.
class
);
s_result
.
setLength
(
0
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
dispatcher
.
dispatch
(
new
MockEvent
(
i
));
}
Assert
.
assertEquals
(
"0:1:2:3:4:5:6:7:8:9:"
,
s_result
.
toString
());
}
static
enum
MockEventType
implements
EventType
{
TYPE1
;
}
static
enum
MockEventListener
implements
EventListener
<
MockEvent
>
{
INSTANCE
;
@Override
public
void
onEvent
(
MockEvent
event
)
{
s_result
.
append
(
event
.
getIndex
()).
append
(
':'
);
}
}
static
class
MockEvent
implements
Event
{
private
int
m_index
;
public
MockEvent
(
int
index
)
{
m_index
=
index
;
}
public
int
getIndex
()
{
return
m_index
;
}
@Override
public
EventType
getType
()
{
return
MockEventType
.
TYPE1
;
}
}
}
dog-home/src/test/java/com/dianping/dog/rule/RuleTest.java
0 → 100644
浏览文件 @
8a4b56d4
package
com.dianping.dog.rule
;
import
junit.framework.Assert
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.JUnit4
;
import
com.dianping.dog.alarm.rule.Rule
;
import
com.dianping.dog.alarm.rule.RuleContext
;
import
com.dianping.dog.alarm.rule.reactor.AbstractRuleReactor
;
import
com.dianping.dog.event.Event
;
import
com.dianping.dog.event.EventDispatcher
;
import
com.dianping.dog.event.EventListener
;
import
com.dianping.dog.event.EventListenerRegistry
;
import
com.dianping.dog.event.EventType
;
import
com.site.lookup.ComponentTestCase
;
@RunWith
(
JUnit4
.
class
)
public
class
RuleTest
extends
ComponentTestCase
{
private
static
StringBuilder
s_result
=
new
StringBuilder
();
@Test
public
void
test
()
throws
Exception
{
EventListenerRegistry
registry
=
lookup
(
EventListenerRegistry
.
class
);
EventDispatcher
dispatcher
=
lookup
(
EventDispatcher
.
class
);
registry
.
register
(
MockEventType
.
CONNECTION
,
new
ConnectionEventListener
(
dispatcher
));
registry
.
register
(
MockEventType
.
ALARM
,
new
AlarmEventListener
());
s_result
.
setLength
(
0
);
dispatcher
.
dispatch
(
new
ConnectionEvent
(
10
));
dispatcher
.
dispatch
(
new
ConnectionEvent
(
2
));
dispatcher
.
dispatch
(
new
ConnectionEvent
(
100
));
dispatcher
.
dispatch
(
new
ConnectionEvent
(
25
));
dispatcher
.
dispatch
(
new
ConnectionEvent
(
70
));
dispatcher
.
dispatch
(
new
ConnectionEvent
(
20
));
Assert
.
assertEquals
(
"2:100:70:"
,
s_result
.
toString
());
}
static
class
AlarmEvent
implements
Event
{
private
int
m_connections
;
public
AlarmEvent
(
int
connections
)
{
m_connections
=
connections
;
}
public
int
getConnections
()
{
return
m_connections
;
}
@Override
public
EventType
getType
()
{
return
MockEventType
.
ALARM
;
}
}
static
class
AlarmEventListener
implements
EventListener
<
AlarmEvent
>
{
@Override
public
void
onEvent
(
AlarmEvent
event
)
{
s_result
.
append
(
event
.
getConnections
()).
append
(
":"
);
}
}
static
class
ConnectionEvent
implements
Event
{
private
int
m_connections
;
public
ConnectionEvent
(
int
connections
)
{
m_connections
=
connections
;
}
public
int
getConnections
()
{
return
m_connections
;
}
@Override
public
EventType
getType
()
{
return
MockEventType
.
CONNECTION
;
}
}
static
class
ConnectionEventListener
extends
AbstractRuleReactor
<
ConnectionEvent
>
{
public
ConnectionEventListener
(
EventDispatcher
dispatcher
)
{
setDispatcher
(
dispatcher
);
setRule
(
ConnectionRule
.
INSTANCE
);
}
@Override
protected
Event
createNextEvent
(
RuleContext
ctx
)
{
return
new
AlarmEvent
(
getEvent
(
ctx
).
getConnections
());
}
@Override
protected
void
prepare
(
RuleContext
ctx
,
ConnectionEvent
event
)
{
}
}
static
enum
ConnectionRule
implements
Rule
{
INSTANCE
;
@Override
public
boolean
apply
(
RuleContext
ctx
)
{
int
connections
=
ctx
.
getAttribute
(
"connections"
);
return
connections
<
3
||
connections
>
30
;
}
@Override
public
String
getName
()
{
return
getClass
().
getSimpleName
();
}
}
static
enum
MockEventType
implements
EventType
{
CONNECTION
,
ALARM
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录