Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
ca8ca845
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,发现更多精彩内容 >>
提交
ca8ca845
编写于
7月 11, 2012
作者:
Y
You Yong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify the cat
上级
4c2326f4
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
82 addition
and
71 deletion
+82
-71
cat-core/src/main/java/com/dianping/cat/Cat.java
cat-core/src/main/java/com/dianping/cat/Cat.java
+36
-54
cat-core/src/main/java/com/dianping/cat/configuration/ClientConfigValidator.java
...com/dianping/cat/configuration/ClientConfigValidator.java
+13
-6
cat-core/src/main/java/com/dianping/cat/message/internal/DefaultMessageManager.java
.../dianping/cat/message/internal/DefaultMessageManager.java
+7
-5
cat-core/src/main/resources/META-INF/dal/model/client-model.xml
...re/src/main/resources/META-INF/dal/model/client-model.xml
+2
-1
cat-core/src/main/resources/com/dianping/cat/configuration/client/config.xsd
...esources/com/dianping/cat/configuration/client/config.xsd
+2
-2
cat-core/src/test/java/com/dianping/cat/CatTest.java
cat-core/src/test/java/com/dianping/cat/CatTest.java
+20
-0
cat-core/src/test/java/com/dianping/cat/message/AllTests.java
...core/src/test/java/com/dianping/cat/message/AllTests.java
+2
-0
cat-core/src/test/resources/META-INF/cat/client.xml
cat-core/src/test/resources/META-INF/cat/client.xml
+0
-3
未找到文件。
cat-core/src/main/java/com/dianping/cat/Cat.java
浏览文件 @
ca8ca845
...
...
@@ -35,14 +35,26 @@ public class Cat {
private
static
Cat
s_instance
=
new
Cat
();
public
volatile
State
m_initialized
=
State
.
CREATED
;
private
MessageProducer
m_producer
;
private
MessageManager
m_manager
;
private
PlexusContainer
m_container
;
private
Cat
()
{
}
public
static
void
destroy
()
{
s_instance
.
m_container
.
dispose
();
s_instance
=
new
Cat
();
}
public
static
Cat
getInstance
(){
public
static
Cat
getInstance
()
{
return
s_instance
;
}
public
static
MessageManager
getManager
()
{
return
s_instance
.
m_manager
;
}
...
...
@@ -52,8 +64,8 @@ public class Cat {
synchronized
(
s_instance
)
{
initialize
(
new
File
(
CAT_GLOBAL_XML
));
if
(
s_instance
.
m_initialized
==
State
.
INITIALIZED
)
{
log
(
"WARN"
,
"Cat is lazy loading !"
);
s_instance
.
m_initialized
=
State
.
LAZY_INITIALIZED
;
log
(
"WARN"
,
"Cat is lazy initialized!"
);
}
}
}
...
...
@@ -87,15 +99,16 @@ public class Cat {
}
ClientConfig
config
=
loadClientConfig
(
configFile
);
Cat
instance
=
s_instance
;
log
(
"INFO"
,
"Current working directory is "
+
System
.
getProperty
(
"user.dir"
));
if
(
config
!=
null
)
{
config
.
accept
(
new
ClientConfigValidator
());
}
if
(
config
!=
null
)
{
instance
.
m_manager
.
initializeClient
(
config
);
log
(
"INFO"
,
String
.
format
(
"Cat client i
s initialized with config file(%s)!"
,
configFile
));
s_
instance
.
m_manager
.
initializeClient
(
config
);
log
(
"INFO"
,
String
.
format
(
"Cat client i
nitialization is done !"
));
}
else
{
instance
.
m_manager
.
initializeClient
(
null
);
s_
instance
.
m_manager
.
initializeClient
(
null
);
log
(
"WARN"
,
"Cat client is disabled due to no config file found!"
);
}
}
...
...
@@ -119,6 +132,7 @@ public class Cat {
String
xml
=
Files
.
forIO
().
readFrom
(
configFile
.
getCanonicalFile
(),
"utf-8"
);
globalConfig
=
new
DefaultDomParser
().
parse
(
xml
);
log
(
"INFO"
,
String
.
format
(
"Global config file(%s) found."
,
configFile
));
}
else
{
log
(
"WARN"
,
String
.
format
(
"Global config file(%s) not found, IGNORED."
,
configFile
));
}
...
...
@@ -147,34 +161,13 @@ public class Cat {
globalConfig
.
accept
(
new
ClientConfigMerger
(
clientConfig
));
}
// do validation
if
(
clientConfig
!=
null
)
{
clientConfig
.
accept
(
new
ClientConfigValidator
());
}
else
if
(
globalConfig
!=
null
)
{
// for test purpose
return
globalConfig
;
}
return
clientConfig
;
}
static
void
log
(
String
severity
,
String
message
)
{
Logger
logger
=
s_instance
.
m_logger
;
if
(
logger
!=
null
)
{
if
(
"INFO"
.
equals
(
severity
))
{
logger
.
info
(
message
);
}
else
if
(
"WARN"
.
equals
(
severity
))
{
logger
.
warn
(
message
);
}
else
if
(
"ERROR"
.
equals
(
severity
))
{
logger
.
error
(
message
);
}
else
{
throw
new
RuntimeException
(
"Unsupported log severity: "
+
severity
);
}
}
else
{
MessageFormat
format
=
new
MessageFormat
(
"[{0,date,MM-dd HH:mm:ss.sss}] [{1}] [{2}] {3}"
);
MessageFormat
format
=
new
MessageFormat
(
"[{0,date,MM-dd HH:mm:ss.sss}] [{1}] [{2}] {3}"
);
System
.
out
.
println
(
format
.
format
(
new
Object
[]
{
new
Date
(),
severity
,
"Cat"
,
message
}));
}
System
.
out
.
println
(
format
.
format
(
new
Object
[]
{
new
Date
(),
severity
,
"Cat"
,
message
}));
}
public
static
<
T
>
T
lookup
(
Class
<
T
>
role
)
throws
ComponentLookupException
{
...
...
@@ -200,22 +193,6 @@ public class Cat {
manager
.
getThreadLocalMessageTree
().
setSessionToken
(
sessionToken
);
}
public
enum
State
{
CREATED
,
INITIALIZED
,
LAZY_INITIALIZED
}
public
volatile
State
m_initialized
=
State
.
CREATED
;
private
MessageProducer
m_producer
;
private
MessageManager
m_manager
;
private
PlexusContainer
m_container
;
private
Logger
m_logger
;
private
Cat
()
{
}
private
boolean
isCatServerFound
(
PlexusContainer
container
)
{
try
{
...
...
@@ -226,10 +203,11 @@ public class Cat {
}
void
setContainer
(
PlexusContainer
container
)
{
final
Logger
logger
;
m_container
=
container
;
try
{
m_
logger
=
container
.
getLoggerManager
().
getLoggerForComponent
(
MessageManager
.
class
.
getName
());
logger
=
container
.
getLoggerManager
().
getLoggerForComponent
(
MessageManager
.
class
.
getName
());
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Unable to get instance of Logger, "
+
"please make sure the environment was setup correctly!"
,
e
);
...
...
@@ -239,27 +217,27 @@ public class Cat {
Threads
.
addListener
(
new
DefaultThreadListener
()
{
@Override
public
void
onThreadGroupCreated
(
ThreadGroup
group
,
String
name
)
{
m_
logger
.
info
(
String
.
format
(
"Thread group(%s) created."
,
name
));
logger
.
info
(
String
.
format
(
"Thread group(%s) created."
,
name
));
}
@Override
public
void
onThreadPoolCreated
(
ExecutorService
pool
,
String
name
)
{
m_
logger
.
info
(
String
.
format
(
"Thread pool(%s) created."
,
name
));
logger
.
info
(
String
.
format
(
"Thread pool(%s) created."
,
name
));
}
@Override
public
void
onThreadStarting
(
Thread
thread
,
String
name
)
{
m_
logger
.
info
(
String
.
format
(
"Starting thread(%s) ..."
,
name
));
logger
.
info
(
String
.
format
(
"Starting thread(%s) ..."
,
name
));
}
@Override
public
void
onThreadStopping
(
Thread
thread
,
String
name
)
{
m_
logger
.
info
(
String
.
format
(
"Stopping thread(%s)."
,
name
));
logger
.
info
(
String
.
format
(
"Stopping thread(%s)."
,
name
));
}
@Override
public
boolean
onUncaughtException
(
Thread
thread
,
Throwable
e
)
{
m_
logger
.
error
(
String
.
format
(
"Uncaught exception thrown out of thread(%s)"
,
thread
.
getName
()),
e
);
logger
.
error
(
String
.
format
(
"Uncaught exception thrown out of thread(%s)"
,
thread
.
getName
()),
e
);
return
true
;
}
});
...
...
@@ -279,4 +257,8 @@ public class Cat {
+
"please make sure the environment was setup correctly!"
,
e
);
}
}
public
enum
State
{
CREATED
,
INITIALIZED
,
LAZY_INITIALIZED
}
}
cat-core/src/main/java/com/dianping/cat/configuration/ClientConfigValidator.java
浏览文件 @
ca8ca845
package
com.dianping.cat.configuration
;
import
java.text.MessageFormat
;
import
java.util.Date
;
import
com.dianping.cat.configuration.client.entity.ClientConfig
;
import
com.dianping.cat.configuration.client.entity.Domain
;
import
com.dianping.cat.configuration.client.entity.Server
;
...
...
@@ -18,12 +21,10 @@ public class ClientConfigValidator extends DefaultValidator {
throw
new
RuntimeException
(
String
.
format
(
"Attribute(%s) of /config is required in config: %s"
,
"mode"
,
config
));
}
else
if
(
config
.
getServers
().
size
()
==
0
)
{
config
.
setEnabled
(
false
);
System
.
out
.
println
(
"[WARN]
CAT client was disabled due to no CAT servers configured!"
);
log
(
"WARN"
,
"
CAT client was disabled due to no CAT servers configured!"
);
}
else
if
(
config
.
getEnabled
()
!=
null
&&
!
config
.
isEnabled
())
{
System
.
out
.
println
(
"[WARN] CAT client was globally disabled!"
);
}
else
if
(
config
.
getEnabled
()
==
null
)
{
config
.
setEnabled
(
true
);
}
log
(
"WARN"
,
"CAT client was globally disabled!"
);
}
m_config
=
config
;
super
.
visitConfig
(
config
);
...
...
@@ -44,10 +45,16 @@ public class ClientConfigValidator extends DefaultValidator {
if
(!
domain
.
isEnabled
()
&&
m_config
.
isEnabled
())
{
m_config
.
setEnabled
(
false
);
System
.
out
.
println
(
"[WARN]
CAT client was disabled in domain("
+
domain
.
getId
()
+
") explicitly!"
);
log
(
"WARN"
,
"
CAT client was disabled in domain("
+
domain
.
getId
()
+
") explicitly!"
);
}
}
private
void
log
(
String
severity
,
String
message
)
{
MessageFormat
format
=
new
MessageFormat
(
"[{0,date,MM-dd HH:mm:ss.sss}] [{1}] [{2}] {3}"
);
System
.
out
.
println
(
format
.
format
(
new
Object
[]
{
new
Date
(),
severity
,
"Cat"
,
message
}));
}
@Override
public
void
visitServer
(
Server
server
)
{
super
.
visitServer
(
server
);
...
...
cat-core/src/main/java/com/dianping/cat/message/internal/DefaultMessageManager.java
浏览文件 @
ca8ca845
...
...
@@ -106,7 +106,7 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
if
(
ctx
!=
null
)
{
return
ctx
;
}
}
}
return
null
;
...
...
@@ -175,7 +175,9 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
m_factory
.
initialize
(
m_domain
.
getId
());
// start status update task
Threads
.
forGroup
(
"Cat"
).
start
(
m_statusUpdateTask
);
if
(
m_clientConfig
.
isEnabled
())
{
Threads
.
forGroup
(
"Cat"
).
start
(
m_statusUpdateTask
);
}
}
@Override
...
...
@@ -218,14 +220,14 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
}
boolean
shouldThrottle
(
MessageTree
tree
)
{
if
(!
isCatEnabled
())
{
if
(!
isCatEnabled
())
{
return
true
;
}
if
(
tree
.
getMessage
()
!=
null
&&
"Heartbeat"
.
equals
(
tree
.
getMessage
().
getName
()))
{
return
false
;
}
int
threadCount
=
ManagementFactory
.
getThreadMXBean
().
getThreadCount
();
return
threadCount
>
m_domain
.
getMaxThreads
();
...
...
cat-core/src/main/resources/META-INF/dal/model/client-model.xml
浏览文件 @
ca8ca845
...
...
@@ -4,13 +4,14 @@
enable-xml-schema=
"true"
enable-merger=
"true"
>
<entity
name=
"config"
class-name=
"ClientConfig"
root=
"true"
dynamic-attributes=
"true"
>
<attribute
name=
"mode"
required=
"true"
/>
<attribute
name=
"enabled"
value-type=
"boolean"
/>
<attribute
name=
"enabled"
value-type=
"boolean"
default-value=
"true"
/>
<element
name=
"base-log-dir"
value-type=
"String"
default-value=
"target/catlog"
/>
<entity-ref
name=
"domain"
type=
"map"
names=
"domains"
/>
<entity-ref
name=
"property"
type=
"map"
names=
"properties"
/>
</entity>
<entity
name=
"server"
>
<attribute
name=
"ip"
value-type=
"String"
key=
"true"
/>
<attribute
name=
"enabled"
value-type=
"boolean"
default-value=
"true"
/>
</entity>
<entity
name=
"domain"
>
<attribute
name=
"id"
value-type=
"String"
key=
"true"
/>
...
...
cat-core/src/main/resources/com/dianping/cat/configuration/client/config.xsd
浏览文件 @
ca8ca845
...
...
@@ -22,7 +22,7 @@
</xs:element>
</xs:sequence>
<xs:attribute
name=
"mode"
type=
"xs:string"
use=
"required"
/>
<xs:attribute
name=
"enabled"
type=
"xs:boolean"
/>
<xs:attribute
name=
"enabled"
type=
"xs:boolean"
default=
"true"
/>
<xs:anyAttribute
processContents=
"lax"
/>
</xs:complexType>
<xs:complexType
name=
"ServerType"
>
...
...
@@ -30,7 +30,7 @@
<xs:extension
base=
"xs:string"
>
<xs:attribute
name=
"ip"
type=
"xs:string"
use=
"required"
/>
<xs:attribute
name=
"port"
type=
"xs:int"
/>
<xs:attribute
name=
"enabled"
type=
"xs:boolean"
/>
<xs:attribute
name=
"enabled"
type=
"xs:boolean"
default=
"true"
/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
...
...
cat-core/src/test/java/com/dianping/cat/CatTest.java
浏览文件 @
ca8ca845
...
...
@@ -24,6 +24,7 @@ public class CatTest {
Assert
.
assertEquals
(
true
,
Cat
.
isInitialized
());
Assert
.
assertEquals
(
State
.
LAZY_INITIALIZED
,
Cat
.
getInstance
().
m_initialized
);
Cat
.
destroy
();
System
.
out
.
println
();
}
@Test
...
...
@@ -41,5 +42,24 @@ public class CatTest {
Assert
.
assertEquals
(
true
,
Cat
.
isInitialized
());
Assert
.
assertEquals
(
State
.
INITIALIZED
,
Cat
.
getInstance
().
m_initialized
);
Cat
.
destroy
();
System
.
out
.
println
();
}
@Test
public
void
testWithoutGlobalConfigInitialize
()
throws
InterruptedException
{
Cat
.
initialize
(
new
File
(
"/data/appdatas/cat/clientNoExist.xml"
));
MessageProducer
cat
=
Cat
.
getProducer
();
Transaction
t
=
cat
.
newTransaction
(
"TestType"
,
"TestName"
);
t
.
addData
(
"data here"
);
t
.
setStatus
(
"TestStatus"
);
t
.
complete
();
Thread
.
sleep
(
100
);
Assert
.
assertEquals
(
true
,
Cat
.
isInitialized
());
Assert
.
assertEquals
(
State
.
INITIALIZED
,
Cat
.
getInstance
().
m_initialized
);
Cat
.
destroy
();
System
.
out
.
println
();
}
}
cat-core/src/test/java/com/dianping/cat/message/AllTests.java
浏览文件 @
ca8ca845
...
...
@@ -4,6 +4,7 @@ import org.junit.runner.RunWith;
import
org.junit.runners.Suite
;
import
org.junit.runners.Suite.SuiteClasses
;
import
com.dianping.cat.CatTest
;
import
com.dianping.cat.message.configuration.ClientConfigTest
;
import
com.dianping.cat.message.internal.MessageIdFactoryTest
;
import
com.dianping.cat.message.internal.MillisSecondTimerTest
;
...
...
@@ -18,6 +19,7 @@ import com.dianping.cat.storage.report.LocalReportBucketTest;
@RunWith
(
Suite
.
class
)
@SuiteClasses
({
CatTest
.
class
,
/* <default> */
EventTest
.
class
,
...
...
cat-core/src/test/resources/META-INF/cat/client.xml
浏览文件 @
ca8ca845
<config
mode=
"client"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation=
"config.xsd"
>
<servers>
<server
ip=
"127.0.0.1"
port=
"2280"
/>
</servers>
<domain
id=
"Cat"
/>
</config>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录