Skip to content

Commit

Permalink
@wjw_optimize: 为了加快速度,对象的序列化采用Jackson Json.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjw465150 committed Nov 24, 2011
1 parent 4833759 commit 07bd5f7
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .classpath
Expand Up @@ -10,5 +10,7 @@
<classpathentry kind="var" path="TOMCAT_HOME/lib/el-api.jar"/>
<classpathentry kind="var" path="TOMCAT_HOME/lib/annotations-api.jar"/>
<classpathentry kind="lib" path="thirdparty/wjw-redismanager.jar"/>
<classpathentry kind="lib" path="web-app/WEB-INF/lib/jackson-core-asl-1.9.2.jar"/>
<classpathentry kind="lib" path="web-app/WEB-INF/lib/jackson-mapper-asl-1.9.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
4 changes: 2 additions & 2 deletions src/nl/justobjects/pushlet/client/PushletClient.java
Expand Up @@ -157,8 +157,8 @@ public void leave(boolean aUnsubscribe) throws PushletException {
event.setField(P_ID, id);
Event response = doControl(event);

throwOnNack(response);
id = null;
throwOnNack(response);
}

/**
Expand Down Expand Up @@ -370,7 +370,7 @@ protected Reader openURL(String aURL) throws PushletException {
// Note: somehow the client does not work with some JVMs when using
// BufferedInputStream... So do unbuffered input.
// p("Opening urlConnection inputstream");
return new InputStreamReader(urlConnection.getInputStream());
return new InputStreamReader(urlConnection.getInputStream(),"UTF-8");

} catch (Throwable t) {
warn("openURL() could not open " + aURL, t);
Expand Down
2 changes: 1 addition & 1 deletion src/nl/justobjects/pushlet/core/Event.java
Expand Up @@ -18,7 +18,6 @@
* @version $Id: Event.java,v 1.13 2007/11/23 14:33:07 justb Exp $
*/
public class Event implements Protocol, Serializable {

protected Map attributes = new HashMap(3);

public Event(String anEventType) {
Expand Down Expand Up @@ -150,4 +149,5 @@ public Object clone() {
private void setAttrs(Map theAttributes) {
attributes.putAll(theAttributes);
}

}
17 changes: 14 additions & 3 deletions src/nl/justobjects/pushlet/core/EventQueue.java
Expand Up @@ -3,6 +3,8 @@

package nl.justobjects.pushlet.core;

import java.util.Map;

import nl.justobjects.pushlet.redis.RedisManager;

/**
Expand Down Expand Up @@ -76,7 +78,7 @@ public boolean enQueue(Event item, long maxWaitTime) throws InterruptedException
}

// Put item in queue
redis.lpush(myLkey, redis.toXML(item));
redis.lpush(myLkey, toJsonString(item));

return true;
}
Expand Down Expand Up @@ -139,7 +141,7 @@ public Event[] deQueueAll(long maxWaitTime) throws InterruptedException {
String strEvent = null;
java.util.List<Event> listEvent = new java.util.ArrayList<Event>(this.getSize());
while ((strEvent = redis.lpop(myLkey)) != null) {
listEvent.add((Event) redis.fromXML(strEvent));
listEvent.add(fromJsonString(strEvent));
}
Event[] events = listEvent.toArray(new Event[0]);

Expand Down Expand Up @@ -169,12 +171,21 @@ public boolean isFull() {
* Circular counter.
*/
private Event fetchNext() {
return (Event) redis.fromXML(redis.lpop(myLkey));
return fromJsonString(redis.lpop(myLkey));
}

//@wjw_add 清除保存在redis里的事件
public void clear() {
redis.del(myLkey);
}

public static String toJsonString(Event event) {
return redis.objToJsonString(event.attributes);
}

public static Event fromJsonString(String content) {
Map attributes = redis.jsonStringToObj(content, java.util.HashMap.class);
return new Event(attributes);
}

}
32 changes: 21 additions & 11 deletions src/nl/justobjects/pushlet/core/Subscriber.java
Expand Up @@ -126,7 +126,7 @@ public String getId() {
*/
public Subscription addSubscription(String aSubject, String aLabel) throws PushletException {
Subscription subscription = Subscription.create(aSubject, aLabel);
String strSubscription = redis.toXML(subscription);
String strSubscription = subscription.toJsonString();
redis.hset(subscriptionHkey, aSubject, strSubscription);

//把单个的subject存到redis的Hash表里,方便match查找
Expand All @@ -150,7 +150,11 @@ public Subscription removeSubscription(String aSubscriptionId) {
if (strSubscription == null) {
subscription = null;
} else {
subscription = (Subscription) redis.fromXML(strSubscription);
try {
subscription = Subscription.fromJsonString(strSubscription);
} catch (PushletException e) {
subscription = null;
}
redis.hdel(subscriptionHkey, aSubscriptionId);

String[] subjects = subscription.getSubjects();
Expand All @@ -177,11 +181,14 @@ public void removeSubscriptions() {
Subscription subscription;
java.util.List<String> subscriptions = redis.hvals(subscriptionHkey);
for (String oneSubscription : subscriptions) {
subscription = (Subscription) redis.fromXML(oneSubscription);
String[] subjects = subscription.getSubjects();
for (String oneSubject : subjects) {
redis.hdel(PUSHLET_SUBJECT_PREFIX + oneSubject, session.getId());
redis.zrem(PUSHLET_ZSET_SUBJECT_PREFIX + oneSubject, session.getId());
try {
subscription = Subscription.fromJsonString(oneSubscription);
String[] subjects = subscription.getSubjects();
for (String oneSubject : subjects) {
redis.hdel(PUSHLET_SUBJECT_PREFIX + oneSubject, session.getId());
redis.zrem(PUSHLET_ZSET_SUBJECT_PREFIX + oneSubject, session.getId());
}
} catch (PushletException e) {
}
}

Expand Down Expand Up @@ -335,10 +342,13 @@ public Subscription match(Event event) {
return null;
}

Subscription subscription = (Subscription) redis.fromXML(strSubscription); //TODO@wjw_note 可以优化的地方

return subscription;

Subscription subscription;
try {
subscription = Subscription.fromJsonString(strSubscription);
return subscription;
} catch (PushletException e) {
return null;
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/nl/justobjects/pushlet/core/Subscription.java
Expand Up @@ -3,15 +3,22 @@

package nl.justobjects.pushlet.core;

import nl.justobjects.pushlet.redis.RedisManager;
import nl.justobjects.pushlet.util.PushletException;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

/**
* Represents single subject subscription
*
* @author Just van den Broecke - Just Objects &copy;
* @version $Id: Subscription.java,v 1.5 2007/11/23 14:33:07 justb Exp $
*/
public class Subscription implements ConfigDefs {
static RedisManager redis = RedisManager.getInstance();

public static final int ID_SIZE = 8;
public static final String SUBJECT_SEPARATOR = ",";

Expand Down Expand Up @@ -89,4 +96,16 @@ public String[] getSubjects() {
return subjects;
}

public String toJsonString() {
ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
node.put("subject", subject);
node.put("label", label);

return node.toString();
}

public static Subscription fromJsonString(String content) throws PushletException {
JsonNode node = redis.readTree(content);
return create(node.get("subject").getTextValue(), node.get("label").getTextValue());
}
}
58 changes: 51 additions & 7 deletions src/nl/justobjects/pushlet/redis/RedisManager.java
Expand Up @@ -5,10 +5,19 @@
import internal.org.apache.commons.pool.impl.GenericObjectPool;

import java.io.IOException;
import java.io.StringWriter;

import nl.justobjects.pushlet.core.Config;
import nl.justobjects.pushlet.core.ConfigDefs;
import nl.justobjects.pushlet.util.Log;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
Expand All @@ -19,7 +28,9 @@

public class RedisManager {
public static final String REDIS_CHARSET = "UTF-8";
static final XStream _xstream = new XStream(new XppDriver());
//static final XStream _xstream = new XStream(new XppDriver());
static final ObjectMapper _mapper = new ObjectMapper(); //@wjw_comment ObjectMapper是线程安全的

static ShardedJedisPool _shardedPool = null;
static JedisPool _pool = null;

Expand All @@ -35,9 +46,15 @@ public class RedisManager {
private static RedisManager instance;

static {
// Singleton + factory pattern: create single instance
// from configured class name
try {
//->初始化Json
_mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);

_mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
_mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
_mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//<-初始化Json

instance = (RedisManager) Config.getClass(ConfigDefs.REDIS_MANAGER_CLASS, "nl.justobjects.pushlet.redis.RedisManager").newInstance();
Log.info("RedisManager created className=" + instance.getClass());
debug = Config.getBoolProperty(ConfigDefs.REDIS_DEBUG);
Expand Down Expand Up @@ -85,6 +102,7 @@ public class RedisManager {
Log.info("RedisShards:" + shards.toString());
Log.info("初始化RedisManager:" + instance.toString());
} catch (Throwable t) {
t.printStackTrace();
Log.fatal("Cannot instantiate RedisManager from config", t);
}
}
Expand All @@ -108,12 +126,38 @@ public String toString() {
+ maxConn + ",socketTO=" + socketTO + '}';
}

public String toXML(Object obj) {
return _xstream.toXML(obj);
// public String toXML(Object obj) {
// return _xstream.toXML(obj);
// }
//
// public Object fromXML(String xml) {
// return _xstream.fromXML(xml);
// }

public JsonNode readTree(String content) {
try {
return _mapper.readTree(content);
} catch (IOException e) {
return new ObjectNode(JsonNodeFactory.instance);
}
}

public String objToJsonString(Object obj) {
try {
StringWriter writer = new StringWriter();
_mapper.writeValue(writer, obj);
return writer.toString();
} catch (Exception e) {
return "{}";
}
}

public Object fromXML(String xml) {
return _xstream.fromXML(xml);
public <T> T jsonStringToObj(String content, Class<T> classType) {
try {
return _mapper.readValue(content, classType);
} catch (Exception e) {
return null;
}
}

//TODO@redis的基本操作
Expand Down
Binary file added web-app/WEB-INF/lib/jackson-core-asl-1.9.2.jar
Binary file not shown.
Binary file added web-app/WEB-INF/lib/jackson-mapper-asl-1.9.2.jar
Binary file not shown.

0 comments on commit 07bd5f7

Please sign in to comment.