优化redis集群工具

This commit is contained in:
zhuangqian 2017-01-05 22:27:07 +08:00
parent 4ebd92bf79
commit 1d408f4f4a
7 changed files with 178 additions and 128 deletions

View File

@ -16,7 +16,7 @@
package com.smallchill.core.listener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;
import com.smallchill.core.plugins.PluginManager;
@ -25,10 +25,10 @@ import com.smallchill.core.plugins.PluginManager;
* 关闭监听器
*/
@Component
public class StopListener implements ApplicationListener<ContextStoppedEvent> {
public class StopListener implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextStoppedEvent event) {
public void onApplicationEvent(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
destroyPlugin();
}

View File

@ -17,13 +17,16 @@ package com.smallchill.core.plugins.connection;
import com.smallchill.core.config.BladeConfig;
import com.smallchill.core.interfaces.IPlugin;
import com.smallchill.core.toolbox.kit.LogKit;
import com.smallchill.core.toolbox.redis.IJedis;
import com.smallchill.core.toolbox.redis.IKeyNamingPolicy;
import com.smallchill.core.toolbox.redis.RedisCluster;
import com.smallchill.core.toolbox.redis.RedisSingle;
import com.smallchill.core.toolbox.redis.serializer.JdkSerializer;
import org.beetl.sql.core.IDAutoGen;
import org.beetl.sql.core.SQLManager;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
@ -95,8 +98,12 @@ public class ConnectionPlugin implements IPlugin{
}
public void stop() {
sqlManagerPool.clear();
for (IJedis jedis : redisCachePool.values()) {
jedis.close();
}
redisCachePool.clear();
sqlManagerPool.clear();
LogKit.println("ConnectionPlugin关闭成功");
}
}

View File

@ -19,6 +19,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.smallchill.core.interfaces.ICallBack;
/**
* Redis 单机集群统一接口
*/
@ -582,4 +584,14 @@ public interface IJedis {
*/
public Double zscore(Object key, Object member);
/**
* 回调接口
*/
public <T> T call(ICallBack call);
/**
* 关闭连接
*/
public void close();
}

View File

@ -26,8 +26,12 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import com.smallchill.core.interfaces.ICallBack;
import com.smallchill.core.toolbox.kit.LogKit;
import com.smallchill.core.toolbox.redis.serializer.ISerializer;
/**
@ -96,7 +100,16 @@ public class RedisCluster implements IJedis{
}
public Set<String> keys(String pattern) {
return null;
Set<String> keys = new HashSet<>();
JedisCluster jedis = getJedis();
Map<String, JedisPool> clusterNodes = jedis.getClusterNodes();
for (JedisPool jedisPool : clusterNodes.values()) {
Jedis resource = jedisPool.getResource();
Set<String> key = resource.keys(pattern);
keys.addAll(key);
resource.close();
}
return keys;
}
public String mset(Object... keysValues) {
@ -744,6 +757,20 @@ public class RedisCluster implements IJedis{
finally {close(jedis);}
}
public void close() {
try {
jedisCluster.close();
} catch (IOException e) {
LogKit.error(e.getMessage(), e);
}
}
public <T> T call(ICallBack call) {
JedisCluster jedis = getJedis();
return call.call(jedis);
}
// ---------
protected byte[] keyToBytes(Object key) {
@ -833,13 +860,7 @@ public class RedisCluster implements IJedis{
public void close(JedisCluster jedis) {
if (threadLocalJedis.get() == null && jedis != null)
try {
jedis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
removeThreadLocalJedis();
}
}
public JedisCluster getThreadLocalJedis() {

View File

@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.smallchill.core.interfaces.ICallBack;
import com.smallchill.core.toolbox.redis.serializer.ISerializer;
import redis.clients.jedis.Jedis;
@ -790,6 +791,15 @@ public class RedisSingle implements IJedis{
finally {close(jedis);}
}
public void close() {
jedisPool.close();
}
public <T> T call(ICallBack call) {
Jedis jedis = getJedis();
return call.call(jedis);
}
// ---------
protected byte[] keyToBytes(Object key) {