提交 5484dffc 编写于 作者: Y youyong

Merge branch 'master' of ssh://192.168.8.22:58422/cat

package com.dianping.cat.consumer.transaction;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.google.gson.Gson;
public class GsonTest {
private void check(Object obj, String expected) {
String actual = new Gson().toJson(obj);
Assert.assertEquals(expected, actual);
}
private Map<String, Pojo> mapOfPojo() {
Map<String, Pojo> map = new HashMap<String, Pojo>();
map.put("first", new Pojo(1, "x"));
map.put("second", new Pojo(2, "y"));
map.put("third", new Pojo(3, "z"));
return map;
}
@Test
public void test() {
check(null, "");
check(1, "1");
check(1.2, "1.2");
check(true, "true");
check("xyz", "\"xyz\"");
check(new String[] { "x", "y" }, "[\"x\",\"y\"]");
check(new Pojo(3, null), "{\"x\":3}");
check(new Pojo(3, "a"), "{\"x\":3,\"y\":\"a\"}");
check(mapOfPojo(), "{\"second\":{\"x\":2,\"y\":\"y\"}," + //
"\"third\":{\"x\":3,\"y\":\"z\"}," + //
"\"first\":{\"x\":1,\"y\":\"x\"}}");
}
@Test
@Ignore
public void testOther() {
check(new Date(1330079278861L), "\"2012-02-24 18:27:58\"");
check(Date.class, "\"class java.util.Date\"");
}
public static class Pojo {
private int x;
private String y;
public Pojo(int x, String y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public String getY() {
return y;
}
}
}
......@@ -57,7 +57,7 @@ public class Cat {
public static MessageProducer getProducer() {
return getInstance().m_producer;
}
public static MessageManager getManager() {
return getInstance().m_manager;
}
......@@ -144,7 +144,7 @@ public class Cat {
System.out.println("Cat Set>>>>>>>>>>> Id:"+ Thread.currentThread().getId() +" "+Thread.currentThread().getName() );
MessageManager manager = getInstance().m_manager;
manager.setup();
manager.getThreadLocalMessageTree().setSessionToken(sessionToken);
}
......
......@@ -117,6 +117,8 @@ package com.dianping.cat.message;
* @author Frankie Wu
*/
public interface MessageProducer {
public String createMessageId();
/**
* Log an event in one shot.
*
......
......@@ -52,6 +52,11 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
}
}
@Override
public String createMessageId() {
return UUID.randomUUID().toString(); // TODO
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
......@@ -146,7 +151,7 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
@Override
public void start(Transaction transaction) {
if (Cat.isInitialized()) {
getContext().start(transaction);
getContext().start(this, transaction);
} else if (m_firstMessage) {
m_firstMessage = false;
m_logger.warn("CAT client is not enabled because it's not initialized yet");
......@@ -177,7 +182,7 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
MessageTree tree = m_tree.copy();
tree.setMessage(message);
tree.setMessageId(createMessageId());
tree.setMessageId(manager.createMessageId());
manager.flush(tree);
} else {
Transaction entry = m_stack.peek();
......@@ -186,10 +191,6 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
}
}
String createMessageId() {
return UUID.randomUUID().toString();
}
public void end(DefaultMessageManager manager, Transaction transaction) {
if (!m_stack.isEmpty()) {
Transaction current = m_stack.peek();
......@@ -211,13 +212,13 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
}
}
public void start(Transaction transaction) {
public void start(DefaultMessageManager manager, Transaction transaction) {
if (!m_stack.isEmpty()) {
Transaction entry = m_stack.peek();
entry.addChild(transaction);
} else {
m_tree.setMessageId(createMessageId());
m_tree.setMessageId(manager.createMessageId());
m_tree.setMessage(transaction);
}
......
......@@ -14,6 +14,11 @@ public class DefaultMessageProducer implements MessageProducer {
@Inject
private MessageManager m_manager;
@Override
public String createMessageId() {
return m_manager.createMessageId();
}
@Override
public void logError(Throwable cause) {
StringWriter writer = new StringWriter(2048);
......
......@@ -14,6 +14,12 @@ import com.dianping.cat.message.Transaction;
public interface MessageManager {
public void add(Message message);
/**
* Create a new message id.
* @return message id
*/
public String createMessageId();
/**
* Be triggered when a transaction ends, whatever it's the root transaction
* or nested transaction. However, if it's the root transaction then it will
......@@ -38,6 +44,13 @@ public interface MessageManager {
*/
public Config getServerConfig();
/**
* Get thread local message information.
*
* @return message tree
*/
public MessageTree getThreadLocalMessageTree();
/**
* Initialize CAT client with given CAT configuration.
*
......@@ -73,11 +86,4 @@ public interface MessageManager {
* @param transaction
*/
public void start(Transaction transaction);
/**
* Get thread local message information.
*
* @return message tree
*/
public MessageTree getThreadLocalMessageTree();
}
\ No newline at end of file
......@@ -30,7 +30,7 @@ public class HtmlMessageCodec implements MessageCodec {
private BufferWriter m_writer;
@Inject
private String m_logViewPrefix;
private String m_logViewPrefix = "/cat/m/";
private BufferHelper m_bufferHelper = new BufferHelper(m_writer);
......
......@@ -25,7 +25,6 @@ public class HtmlMessageCodecTest {
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
codec.setBufferWriter(new HtmlEncodingBufferWriter());
codec.setLogViewPrefix("/cat/m/");
codec.encodeMessage(message, buf, 0, null);
String actual = buf.toString(Charset.forName("utf-8"));
......
......@@ -33,7 +33,7 @@
<dependency>
<groupId>com.site.common</groupId>
<artifactId>lookup</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册