提交 ca8ca845 编写于 作者: Y You Yong

modify the cat

上级 4c2326f4
......@@ -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 is initialized with config file(%s)!", configFile));
s_instance.m_manager.initializeClient(config);
log("INFO", String.format("Cat client initialization 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
}
}
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);
......
......@@ -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();
......
......@@ -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" />
......
......@@ -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>
......
......@@ -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();
}
}
......@@ -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,
......
<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.
先完成此消息的编辑!
想要评论请 注册