Skip to content

Commit

Permalink
修改redis manage
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Feb 4, 2016
1 parent c88ff6e commit 6a1ea57
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 16 deletions.
@@ -1,4 +1,4 @@
package com.shinemo.mpush.tools.redis;
package com.shinemo.mpush.test.redis;

import java.util.ArrayList;
import java.util.Collections;
Expand Down
@@ -0,0 +1,40 @@
package com.shinemo.mpush.test.redis;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.shinemo.mpush.tools.redis.RedisGroup;
import com.shinemo.mpush.tools.redis.RedisNode;
import com.shinemo.mpush.tools.redis.RedisRegister;
import com.shinemo.mpush.tools.redis.manage.RedisManage;
import com.shinemo.mpush.tools.redis.pubsub.Subscriber;
import com.shinemo.mpush.tools.spi.ServiceContainer;

public class PubSubTest {

private RedisRegister redisRegister = ServiceContainer.getInstance(RedisRegister.class);

@Before
public void init(){
RedisNode node = new RedisNode("127.0.0.1", 6379, "shinemoIpo");
RedisGroup group = new RedisGroup();
group.addRedisNode(node);
List<RedisGroup> listGroup = Lists.newArrayList(group);
redisRegister.init(listGroup);
}

@Test
public void pubSubTest(){

RedisManage.subscribe(Subscriber.holder, "/hello/123");

RedisManage.subscribe(Subscriber.holder, "/hello/124");

RedisManage.publish("/hello/123", "123");
RedisManage.publish("/hello/124", "124");
}

}
@@ -1,4 +1,4 @@
package com.shinemo.mpush.tools.redis;
package com.shinemo.mpush.test.redis;

import java.util.Date;
import java.util.HashSet;
Expand All @@ -10,6 +10,7 @@
import org.junit.Test;

import com.shinemo.mpush.tools.Jsons;
import com.shinemo.mpush.tools.redis.RedisPoolConfig;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
Expand Down
@@ -1,4 +1,4 @@
package com.shinemo.mpush.tools.redis;
package com.shinemo.mpush.test.redis;

import java.util.Date;
import java.util.Iterator;
Expand All @@ -9,6 +9,8 @@
import org.junit.Test;

import com.google.common.collect.Lists;
import com.shinemo.mpush.tools.redis.RedisNode;
import com.shinemo.mpush.tools.redis.RedisUtil;

import redis.clients.jedis.Jedis;

Expand Down
@@ -1,4 +1,4 @@
package com.shinemo.mpush.tools.redis;
package com.shinemo.mpush.test.redis;

import java.io.Serializable;
import java.util.Date;
Expand Down
Expand Up @@ -33,5 +33,9 @@ public RedisNode(String ip, int port, String password) {
this.port = port;
this.password = password;
}
@Override
public String toString() {
return "RedisNode [ip=" + ip + ", port=" + port + ", password=" + password + "]";
}

}
Expand Up @@ -470,7 +470,7 @@ public static <T> void publish(RedisNode node, String channel, T message) {
jedis = getClient(node);
jedis.publish(channel, value);
} catch (Exception e) {
LoggerManage.execption(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,node,channel);
LoggerManage.execption(LogType.REDIS, e, "redis publish exception:%s,%s,%s",value,Jsons.toJson(node),Jsons.toJson(channel));
} finally {
// 返还到连接池
close(jedis);
Expand All @@ -485,7 +485,7 @@ public static void subscribe(Set<RedisNode> nodeList, final JedisPubSub pubsub,
public void run() {
subscribe(node, pubsub, channels);
}
});
}).start();
}
}

Expand All @@ -496,7 +496,7 @@ public static void subscribe(RedisNode node, JedisPubSub pubsub, String... chann
jedis = getClient(node);
jedis.subscribe(pubsub, channel);
} catch (Exception e) {
LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:%s,%s",node,channel);
LoggerManage.execption(LogType.REDIS, e, "redis subscribe exception:%s,%s",Jsons.toJson(node),Jsons.toJson(channel));
} finally {
// 返还到连接池
close(jedis);
Expand Down
Expand Up @@ -3,11 +3,6 @@
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.shinemo.mpush.log.LogType;
import com.shinemo.mpush.log.LoggerManage;
Expand All @@ -18,8 +13,6 @@

public class JedisRegisterManager implements RedisRegister{

private static final Logger LOGGER = LoggerFactory.getLogger(JedisRegisterManager.class);

private static List<RedisGroup> groups = Lists.newArrayList();

/**
Expand Down
Expand Up @@ -6,6 +6,8 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.shinemo.mpush.log.LogType;
import com.shinemo.mpush.log.LoggerManage;
import com.shinemo.mpush.tools.redis.manage.RedisManage;
import com.shinemo.mpush.tools.redis.pubsub.Subscriber;
import com.shinemo.mpush.tools.thread.threadpool.ThreadPoolManager;
Expand All @@ -15,13 +17,16 @@ public class ListenerDispatcher implements MessageListener {
public static final ListenerDispatcher INSTANCE = new ListenerDispatcher();

private Map<String, List<MessageListener>> subscribes = Maps.newTreeMap();

private ListenerDispatcher(){}

private Executor executor = ThreadPoolManager.redisExecutor;

@Override
public void onMessage(final String channel, final String message) {
List<MessageListener> listeners = subscribes.get(channel);
if (listeners == null) {
LoggerManage.info(LogType.REDIS, "cannot find listener:%s,%s", channel,message);
return;
}
for (final MessageListener listener : listeners) {
Expand All @@ -41,7 +46,6 @@ public void subscribe(String channel, MessageListener listener) {
subscribes.put(channel, listeners);
}
listeners.add(listener);
Subscriber subscriber = new Subscriber();
RedisManage.subscribe(subscriber, channel);
RedisManage.subscribe(Subscriber.holder, channel);
}
}
Expand Up @@ -11,6 +11,10 @@ public class Subscriber extends JedisPubSub {

private static ListenerDispatcher dispatcher = ListenerDispatcher.INSTANCE;

public static Subscriber holder = new Subscriber();

private Subscriber(){}

@Override
public void onMessage(String channel, String message) {
LoggerManage.log(LogType.REDIS, "onMessage:%s,%s", channel,message);
Expand Down

0 comments on commit 6a1ea57

Please sign in to comment.