优化redis集群工具
This commit is contained in:
parent
4ebd92bf79
commit
1d408f4f4a
|
|
@ -16,7 +16,7 @@
|
||||||
package com.smallchill.core.listener;
|
package com.smallchill.core.listener;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.event.ContextStoppedEvent;
|
import org.springframework.context.event.ContextClosedEvent;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import com.smallchill.core.plugins.PluginManager;
|
import com.smallchill.core.plugins.PluginManager;
|
||||||
|
|
@ -25,10 +25,10 @@ import com.smallchill.core.plugins.PluginManager;
|
||||||
* 关闭监听器
|
* 关闭监听器
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class StopListener implements ApplicationListener<ContextStoppedEvent> {
|
public class StopListener implements ApplicationListener<ContextClosedEvent> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ContextStoppedEvent event) {
|
public void onApplicationEvent(ContextClosedEvent event) {
|
||||||
if (event.getApplicationContext().getParent() == null) {
|
if (event.getApplicationContext().getParent() == null) {
|
||||||
destroyPlugin();
|
destroyPlugin();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,16 @@ package com.smallchill.core.plugins.connection;
|
||||||
|
|
||||||
import com.smallchill.core.config.BladeConfig;
|
import com.smallchill.core.config.BladeConfig;
|
||||||
import com.smallchill.core.interfaces.IPlugin;
|
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.IJedis;
|
||||||
import com.smallchill.core.toolbox.redis.IKeyNamingPolicy;
|
import com.smallchill.core.toolbox.redis.IKeyNamingPolicy;
|
||||||
import com.smallchill.core.toolbox.redis.RedisCluster;
|
import com.smallchill.core.toolbox.redis.RedisCluster;
|
||||||
import com.smallchill.core.toolbox.redis.RedisSingle;
|
import com.smallchill.core.toolbox.redis.RedisSingle;
|
||||||
import com.smallchill.core.toolbox.redis.serializer.JdkSerializer;
|
import com.smallchill.core.toolbox.redis.serializer.JdkSerializer;
|
||||||
|
|
||||||
import org.beetl.sql.core.IDAutoGen;
|
import org.beetl.sql.core.IDAutoGen;
|
||||||
import org.beetl.sql.core.SQLManager;
|
import org.beetl.sql.core.SQLManager;
|
||||||
|
|
||||||
import redis.clients.jedis.JedisCluster;
|
import redis.clients.jedis.JedisCluster;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
|
|
@ -95,8 +98,12 @@ public class ConnectionPlugin implements IPlugin{
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
sqlManagerPool.clear();
|
for (IJedis jedis : redisCachePool.values()) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
redisCachePool.clear();
|
redisCachePool.clear();
|
||||||
|
sqlManagerPool.clear();
|
||||||
|
LogKit.println("ConnectionPlugin关闭成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.smallchill.core.interfaces.ICallBack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis 单机、集群统一接口
|
* Redis 单机、集群统一接口
|
||||||
*/
|
*/
|
||||||
|
|
@ -582,4 +584,14 @@ public interface IJedis {
|
||||||
*/
|
*/
|
||||||
public Double zscore(Object key, Object member);
|
public Double zscore(Object key, Object member);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回调接口
|
||||||
|
*/
|
||||||
|
public <T> T call(ICallBack call);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭连接
|
||||||
|
*/
|
||||||
|
public void close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,12 @@ import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisCluster;
|
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;
|
import com.smallchill.core.toolbox.redis.serializer.ISerializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -96,7 +100,16 @@ public class RedisCluster implements IJedis{
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> keys(String pattern) {
|
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) {
|
public String mset(Object... keysValues) {
|
||||||
|
|
@ -744,6 +757,20 @@ public class RedisCluster implements IJedis{
|
||||||
finally {close(jedis);}
|
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) {
|
protected byte[] keyToBytes(Object key) {
|
||||||
|
|
@ -833,13 +860,7 @@ public class RedisCluster implements IJedis{
|
||||||
|
|
||||||
public void close(JedisCluster jedis) {
|
public void close(JedisCluster jedis) {
|
||||||
if (threadLocalJedis.get() == null && jedis != null)
|
if (threadLocalJedis.get() == null && jedis != null)
|
||||||
try {
|
|
||||||
jedis.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
removeThreadLocalJedis();
|
removeThreadLocalJedis();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JedisCluster getThreadLocalJedis() {
|
public JedisCluster getThreadLocalJedis() {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.smallchill.core.interfaces.ICallBack;
|
||||||
import com.smallchill.core.toolbox.redis.serializer.ISerializer;
|
import com.smallchill.core.toolbox.redis.serializer.ISerializer;
|
||||||
|
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
|
|
@ -790,6 +791,15 @@ public class RedisSingle implements IJedis{
|
||||||
finally {close(jedis);}
|
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) {
|
protected byte[] keyToBytes(Object key) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user