启动时redis未连接导致无法启动

This commit is contained in:
648540858 2022-08-29 14:39:18 +08:00
parent d79b378f3d
commit 03c43f84ce
4 changed files with 158 additions and 17 deletions

View File

@ -20,7 +20,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.TransactionStatus;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
@ -40,7 +39,6 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo;
import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; import com.genersoft.iot.vmp.storager.dao.MediaServerMapper;
import com.genersoft.iot.vmp.utils.DateUtil; import com.genersoft.iot.vmp.utils.DateUtil;
import com.genersoft.iot.vmp.utils.redis.RedisUtil; import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
@ -135,7 +133,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
logger.info("media server [ {} ] ssrcConfig is null", mediaServerItem.getId()); logger.info("media server [ {} ] ssrcConfig is null", mediaServerItem.getId());
return null; return null;
}else { }else {
String ssrc = null; String ssrc;
if (presetSsrc != null) { if (presetSsrc != null) {
ssrc = presetSsrc; ssrc = presetSsrc;
}else { }else {
@ -497,7 +495,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
} }
// 获取分数最低的及并发最低的 // 获取分数最低的及并发最低的
Set<Object> objects = RedisUtil.ZRange(key, 0, -1); Set<Object> objects = RedisUtil.zRange(key, 0, -1);
ArrayList<Object> mediaServerObjectS = new ArrayList<>(objects); ArrayList<Object> mediaServerObjectS = new ArrayList<>(objects);
String mediaServerId = (String)mediaServerObjectS.get(0); String mediaServerId = (String)mediaServerObjectS.get(0);

View File

@ -21,6 +21,7 @@ import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.*; import java.util.*;

View File

@ -33,11 +33,11 @@ public class SpringBeanFactory implements ApplicationContextAware {
/** /**
* 获取对象 这里重写了bean方法起主要作用 * 获取对象 这里重写了bean方法起主要作用
*/ */
public static Object getBean(String beanId) throws BeansException { public static <T> T getBean(String beanId) throws BeansException {
if (applicationContext == null) { if (applicationContext == null) {
return null; return null;
} }
return applicationContext.getBean(beanId); return (T) applicationContext.getBean(beanId);
} }
/** /**

View File

@ -5,15 +5,13 @@ import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.utils.SpringBeanFactory; import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*; import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
* @description:Redis工具类 * Redis工具类
* @author: swwheihei * @author swwheihei
* @date: 2020年5月6日 下午8:27:29 * @date 2020年5月6日 下午8:27:29
*/ */
@SuppressWarnings(value = {"rawtypes", "unchecked"}) @SuppressWarnings(value = {"rawtypes", "unchecked"})
public class RedisUtil { public class RedisUtil {
@ -21,7 +19,7 @@ public class RedisUtil {
private static RedisTemplate redisTemplate; private static RedisTemplate redisTemplate;
static { static {
redisTemplate = (RedisTemplate)SpringBeanFactory.getBean("redisTemplate"); redisTemplate = SpringBeanFactory.getBean("redisTemplate");
} }
/** /**
@ -31,6 +29,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean expire(String key, long time) { public static boolean expire(String key, long time) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
if (time > 0) { if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS); redisTemplate.expire(key, time, TimeUnit.SECONDS);
@ -45,9 +46,11 @@ public class RedisUtil {
/** /**
* 根据 key 获取过期时间 * 根据 key 获取过期时间
* @param key * @param key
* @return
*/ */
public static long getExpire(String key) { public static long getExpire(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.getExpire(key, TimeUnit.SECONDS); return redisTemplate.getExpire(key, TimeUnit.SECONDS);
} }
@ -57,6 +60,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean hasKey(String key) { public static boolean hasKey(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.hasKey(key); return redisTemplate.hasKey(key);
} catch (Exception e) { } catch (Exception e) {
@ -71,6 +77,9 @@ public class RedisUtil {
* @param key 一个或者多个 * @param key 一个或者多个
*/ */
public static boolean del(String... key) { public static boolean del(String... key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
if (key != null && key.length > 0) { if (key != null && key.length > 0) {
if (key.length == 1) { if (key.length == 1) {
@ -95,6 +104,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Object get(String key) { public static Object get(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return key == null ? null : redisTemplate.opsForValue().get(key); return key == null ? null : redisTemplate.opsForValue().get(key);
} }
@ -105,6 +117,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean set(String key, Object value) { public static boolean set(String key, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForValue().set(key, value); redisTemplate.opsForValue().set(key, value);
return true; return true;
@ -122,6 +137,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean set(String key, Object value, long time) { public static boolean set(String key, Object value, long time) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
if (time > 0) { if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
@ -142,6 +160,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static long incr(String key, long delta) { public static long incr(String key, long delta) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
if (delta < 0) { if (delta < 0) {
throw new RuntimeException("递增因子必须大于 0"); throw new RuntimeException("递增因子必须大于 0");
} }
@ -155,6 +176,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static long decr(String key, long delta) { public static long decr(String key, long delta) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
if (delta < 0) { if (delta < 0) {
throw new RuntimeException("递减因子必须大于 0"); throw new RuntimeException("递减因子必须大于 0");
} }
@ -170,6 +194,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Object hget(String key, String item) { public static Object hget(String key, String item) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForHash().get(key, item); return redisTemplate.opsForHash().get(key, item);
} }
@ -179,6 +206,9 @@ public class RedisUtil {
* @return 对应的多个键值 * @return 对应的多个键值
*/ */
public static Map<Object, Object> hmget(String key) { public static Map<Object, Object> hmget(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForHash().entries(key); return redisTemplate.opsForHash().entries(key);
} }
@ -189,6 +219,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean hmset(String key, Map<Object, Object> map) { public static boolean hmset(String key, Map<Object, Object> map) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForHash().putAll(key, map); redisTemplate.opsForHash().putAll(key, map);
return true; return true;
@ -206,6 +239,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean hmset(String key, Map<Object, Object> map, long time) { public static boolean hmset(String key, Map<Object, Object> map, long time) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForHash().putAll(key, map); redisTemplate.opsForHash().putAll(key, map);
if (time > 0) { if (time > 0) {
@ -226,6 +262,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean hset(String key, String item, Object value) { public static boolean hset(String key, String item, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForHash().put(key, item, value); redisTemplate.opsForHash().put(key, item, value);
return true; return true;
@ -244,6 +283,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean hset(String key, String item, Object value, long time) { public static boolean hset(String key, String item, Object value, long time) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForHash().put(key, item, value); redisTemplate.opsForHash().put(key, item, value);
if (time > 0) { if (time > 0) {
@ -262,6 +304,9 @@ public class RedisUtil {
* @param item 可以多个no null * @param item 可以多个no null
*/ */
public static void hdel(String key, Object... item) { public static void hdel(String key, Object... item) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
redisTemplate.opsForHash().delete(key, item); redisTemplate.opsForHash().delete(key, item);
} }
@ -272,6 +317,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean hHasKey(String key, String item) { public static boolean hHasKey(String key, String item) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForHash().hasKey(key, item); return redisTemplate.opsForHash().hasKey(key, item);
} }
@ -283,6 +331,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Double hincr(String key, String item, Double by) { public static Double hincr(String key, String item, Double by) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForHash().increment(key, item, by); return redisTemplate.opsForHash().increment(key, item, by);
} }
@ -294,6 +345,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Double hdecr(String key, String item, Double by) { public static Double hdecr(String key, String item, Double by) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForHash().increment(key, item, -by); return redisTemplate.opsForHash().increment(key, item, -by);
} }
@ -305,6 +359,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Set<Object> sGet(String key) { public static Set<Object> sGet(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForSet().members(key); return redisTemplate.opsForSet().members(key);
} catch (Exception e) { } catch (Exception e) {
@ -320,6 +377,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean sHasKey(String key, Object value) { public static boolean sHasKey(String key, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForSet().isMember(key, value); return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) { } catch (Exception e) {
@ -335,6 +395,9 @@ public class RedisUtil {
* @return 成功个数 * @return 成功个数
*/ */
public static long sSet(String key, Object... values) { public static long sSet(String key, Object... values) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForSet().add(key, values); return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) { } catch (Exception e) {
@ -351,6 +414,9 @@ public class RedisUtil {
* @return 成功放入个数 * @return 成功放入个数
*/ */
public static long sSet(String key, long time, Object... values) { public static long sSet(String key, long time, Object... values) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
long count = redisTemplate.opsForSet().add(key, values); long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) { if (time > 0) {
@ -369,6 +435,9 @@ public class RedisUtil {
* @return 长度 * @return 长度
*/ */
public static long sGetSetSize(String key) { public static long sGetSetSize(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForSet().size(key); return redisTemplate.opsForSet().size(key);
} catch (Exception e) { } catch (Exception e) {
@ -384,6 +453,9 @@ public class RedisUtil {
* @return 成功移除个数 * @return 成功移除个数
*/ */
public static long setRemove(String key, Object... values) { public static long setRemove(String key, Object... values) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForSet().remove(key, values); return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) { } catch (Exception e) {
@ -401,6 +473,9 @@ public class RedisUtil {
* @param score * @param score
*/ */
public static void zAdd(Object key, Object value, double score) { public static void zAdd(Object key, Object value, double score) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
redisTemplate.opsForZSet().add(key, value, score); redisTemplate.opsForZSet().add(key, value, score);
} }
@ -411,6 +486,9 @@ public class RedisUtil {
* @param value * @param value
*/ */
public static void zRemove(Object key, Object value) { public static void zRemove(Object key, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
redisTemplate.opsForZSet().remove(key, value); redisTemplate.opsForZSet().remove(key, value);
} }
@ -422,6 +500,9 @@ public class RedisUtil {
* @param delta -1 表示减 1 表示加1 * @param delta -1 表示减 1 表示加1
*/ */
public static Double zIncrScore(Object key, Object value, double delta) { public static Double zIncrScore(Object key, Object value, double delta) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().incrementScore(key, value, delta); return redisTemplate.opsForZSet().incrementScore(key, value, delta);
} }
@ -433,6 +514,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Double zScore(Object key, Object value) { public static Double zScore(Object key, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().score(key, value); return redisTemplate.opsForZSet().score(key, value);
} }
@ -444,6 +528,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Long zRank(Object key, Object value) { public static Long zRank(Object key, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().rank(key, value); return redisTemplate.opsForZSet().rank(key, value);
} }
@ -454,6 +541,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Long zSize(Object key) { public static Long zSize(Object key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().zCard(key); return redisTemplate.opsForZSet().zCard(key);
} }
@ -467,7 +557,10 @@ public class RedisUtil {
* @param end * @param end
* @return * @return
*/ */
public static Set<Object> ZRange(Object key, int start, int end) { public static Set<Object> zRange(Object key, int start, int end) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().range(key, start, end); return redisTemplate.opsForZSet().range(key, start, end);
} }
/** /**
@ -479,6 +572,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { public static Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().rangeWithScores(key, start, end); return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
} }
/** /**
@ -492,6 +588,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Set<String> zRevRange(Object key, int start, int end) { public static Set<String> zRevRange(Object key, int start, int end) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().reverseRange(key, start, end); return redisTemplate.opsForZSet().reverseRange(key, start, end);
} }
/** /**
@ -503,6 +602,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Set<String> zSortRange(Object key, int min, int max) { public static Set<String> zSortRange(Object key, int min, int max) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForZSet().rangeByScore(key, min, max); return redisTemplate.opsForZSet().rangeByScore(key, min, max);
} }
@ -517,6 +619,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static List<Object> lGet(String key, long start, long end) { public static List<Object> lGet(String key, long start, long end) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForList().range(key, start, end); return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) { } catch (Exception e) {
@ -531,6 +636,9 @@ public class RedisUtil {
* @return 长度 * @return 长度
*/ */
public static long lGetListSize(String key) { public static long lGetListSize(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForList().size(key); return redisTemplate.opsForList().size(key);
} catch (Exception e) { } catch (Exception e) {
@ -548,6 +656,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Object lGetIndex(String key, long index) { public static Object lGetIndex(String key, long index) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForList().index(key, index); return redisTemplate.opsForList().index(key, index);
} catch (Exception e) { } catch (Exception e) {
@ -563,6 +674,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean lSet(String key, Object value) { public static boolean lSet(String key, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForList().rightPush(key, value); redisTemplate.opsForList().rightPush(key, value);
return true; return true;
@ -580,6 +694,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean lSet(String key, Object value, long time) { public static boolean lSet(String key, Object value, long time) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForList().rightPush(key, value); redisTemplate.opsForList().rightPush(key, value);
if (time > 0) { if (time > 0) {
@ -599,6 +716,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean lSetList(String key, List<Object> values) { public static boolean lSetList(String key, List<Object> values) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForList().rightPushAll(key, values); redisTemplate.opsForList().rightPushAll(key, values);
return true; return true;
@ -616,6 +736,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean lSetList(String key, List<Object> values, long time) { public static boolean lSetList(String key, List<Object> values, long time) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForList().rightPushAll(key, values); redisTemplate.opsForList().rightPushAll(key, values);
if (time > 0) { if (time > 0) {
@ -636,6 +759,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static boolean lUpdateIndex(String key, long index, Object value) { public static boolean lUpdateIndex(String key, long index, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
redisTemplate.opsForList().set(key, index, value); redisTemplate.opsForList().set(key, index, value);
return true; return true;
@ -655,6 +781,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static long lRemove(String key, long count, Object value) { public static long lRemove(String key, long count, Object value) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
return redisTemplate.opsForList().remove(key, count, value); return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) { } catch (Exception e) {
@ -669,6 +798,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Object lLeftPop(String key) { public static Object lLeftPop(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForList().leftPop(key); return redisTemplate.opsForList().leftPop(key);
} }
@ -678,6 +810,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static Object lrightPop(String key) { public static Object lrightPop(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
return redisTemplate.opsForList().rightPop(key); return redisTemplate.opsForList().rightPop(key);
} }
@ -687,6 +822,9 @@ public class RedisUtil {
* @return true / false * @return true / false
*/ */
public static List<Object> keys(String key) { public static List<Object> keys(String key) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
try { try {
Set<String> set = redisTemplate.keys(key); Set<String> set = redisTemplate.keys(key);
return new ArrayList<>(set); return new ArrayList<>(set);
@ -727,6 +865,9 @@ public class RedisUtil {
* @return * @return
*/ */
public static List<Object> scan(String query) { public static List<Object> scan(String query) {
if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { Set<String> resultKeys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build(); ScanOptions scanOptions = ScanOptions.scanOptions().match("*" + query + "*").count(1000).build();
Cursor<byte[]> scan = connection.scan(scanOptions); Cursor<byte[]> scan = connection.scan(scanOptions);
@ -743,9 +884,10 @@ public class RedisUtil {
// ============================== 消息发送与订阅 ============================== // ============================== 消息发送与订阅 ==============================
public static void convertAndSend(String channel, JSONObject msg) { public static void convertAndSend(String channel, JSONObject msg) {
// redisTemplate.convertAndSend(channel, msg); if (redisTemplate == null) {
redisTemplate = SpringBeanFactory.getBean("redisTemplate");
}
redisTemplate.convertAndSend(channel, msg); redisTemplate.convertAndSend(channel, msg);
} }
} }