1.引入pom依赖
<!--整合redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2. 修改redis配置文件
redis.windows.conf 配置文件找到右边这个放开 : notify-keyspace-events Ex
redis.windows-service.conf配置文件搜索 “PUBLISH __keyevent@0__:del foo ” 在下面一行加上 : PUBLISH __keyevent@0__:OpsType
3. redis配置类 ,两个redis配置类。一个redis操作类。直接粘贴复制到项目中即可!
RedisListenerConfig:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.RedisMessageListenerContainer; @Configuration public class RedisListenerConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }
RedisKeyExpirationListener:
import com.lgdz.pojo.LgdzEquipment; import com.lgdz.service.ILgdzEquipmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Component; @Component public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { /* @Autowired IDeviceBaseinfoService deviceBaseinfoService;*/ /**引入设备业务层*/ @Autowired private ILgdzEquipmentService lgdzEquipmentService; public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); System.out.println("expiredKey=========" + expiredKey); if (expiredKey.startsWith("offLine")) { //设备不在线时调用service 改变设备状态 根据ip让设备离线 System.out.println(expiredKey.substring(7)+" offLine"); //expiredKey.substring(7) 是相机推送的ip。 怎么截取与怎么把数据存到Redis有关 // 本案例用Redis存储实例: RedisUtil.setex("offLine"+ip,90,"1"); 键:offLine"+ip 时间:90秒 "1":数据为"1" LgdzEquipment equipment = lgdzEquipmentService.queryEquipmentByParameter(null,null,expiredKey.substring(7)); if (""!=equipment.getSerialno()&&null!=equipment.getSerialno()){ equipment.setOnlinestate("2"); //1 在线 2.离线 } } } }
4.Redis操作工具类
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Redis 公共类
*
* @author xizhuangchui
* @createDate 2015-6-27 16:39:40
*/
public class RedisUtil {
private static JedisPool pool = null;
public static String ip = "127.0.0.1";
public static int port = 6379;
/**
* 十分钟(600秒)
*/
public static int SMS_TIME = 10 * 60;
/**
* 构建redis连接池
*
* @return JedisPool
*/
public static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(500);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
/* Properties properties = BaseConfig.getSysProperties();*/
/* InputStream in = RedisUtil.class.getResourceAsStream("../../../redis.properties"); //---------------------这个是文件的路径(注意区别很简单,就是加上包的路径)
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
*/
RedisUtil.ip = "127.0.0.1";
RedisUtil.port = 6379;
pool = new JedisPool(config, ip, port);
}
return pool;
}
/**
* 返还到连接池
*
* @param pool redis连接池
* @param redis redis对象
*/
public static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
}
/**
* 获取数据
*
* @param key key的值
* @return String value的值
*/
public static String get(String key) {
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
/**
* 插入数据
*
* @param key
* @param value
* @return Boolean 返回插入状态
*/
public static Boolean set(String key, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 若key不存在则储存
*
* @param key
* @param value
* @return Long 成功操作的个数
*/
public static Long setnx(String key, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.setnx(key, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置key的过期时间,单位:秒,如果key在超时之前被修改,则该键关联的超时将被移除。
*
* @param key
* @param seconds 超时时间(秒)
* @param value
* @return 正常返回 OK
*/
public static String setex(String key, int seconds, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.setex(key, seconds, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置 list
*
* @param <T>
* @param key
*/
public static <T> Boolean setexList(String key, List<T> list) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key.getBytes(), ObjectTranscoder.serialize(list));
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置 list
*
* @param <T>
* @param key
*/
public static <T> Boolean setexList(String key, int seconds, List<T> list) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.setex(key.getBytes(), seconds, ObjectTranscoder.serialize(list));
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取list
*
* @param <T>
* @param key
* @return list
*/
public static <T> List<T> getList(String key) {
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
byte[] in = jedis.get(key.getBytes());
List<T> list = (List<T>) ObjectTranscoder.deserialize(in);
return list;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置 map
*
* @param <T>
* @param key
*/
public static <T> Boolean setMap(String key, Map<String, T> map) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key.getBytes(), ObjectTranscoder.serialize(map));
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置 map
*
* @param <T>
* @param key
*/
public <T> Boolean setesMap(String key, int seconds, Map<String, T> map) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.setex(key.getBytes(), seconds, ObjectTranscoder.serialize(map));
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取list
*
* @param <T>
* @param key
* @return list
*/
public <T> Map<String, T> getMap(String key) {
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
byte[] in = jedis.get(key.getBytes());
Map<String, T> map = (Map<String, T>) ObjectTranscoder.deserialize(in);
return map;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置
*
* @param <T>
* @param key
*/
public static <T> Boolean setexObject(String key, int seconds, Object o) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.setex(key.getBytes(), seconds, ObjectTranscoder.serialize(o));
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置
*
* @param <T>
* @param key
*/
public static <T> Boolean setObject(String key, Object o) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key.getBytes(), ObjectTranscoder.serialize(o));
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取list
*
* @param key
* @return list
*/
public static Object getObject(String key) {
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
byte[] in = jedis.get(key.getBytes());
Object o = ObjectTranscoder.deserialize(in);
return o;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 设置key对应的一条数据超时时间
*
* @param key
* @param milliseconds 超时的时间(毫秒)
* @return 成功操作的个数
*/
public static Long expire(String key, int milliseconds) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.expire(key, milliseconds);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 指定超时时间
*
* @param key
* @param millisecondsTimestamp 超时时间的时间戳
* @return 成功返回1失败返回0
*/
public static Long expireAt(String key, long millisecondsTimestamp) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.expireAt(key, millisecondsTimestamp);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 查询剩余时间
*
* @param key
* @return 剩余时间
*/
public static Long ttl(String key) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.ttl(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 判断key是否存在
*
* @param key
* @return Boolean
*/
public static Boolean exists(String key) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.exists(key);
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 删除多个
*
* @param keys key数组
* @return 成功操作的个数
*/
public static Long del(String[] keys) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.del(keys);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 删除一个
*
* @param key
* @return 成功操作的个数
*/
public static Long del(String key) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.del(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 返回指定Map中的key集合
*
* @param pattern
* @return
*/
public static Set<String> gethKeys(String pattern) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hkeys(pattern);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 返回指定Map中的values集合
*
* @param key
* @return
*/
public static List<String> gethVals(String key) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hvals(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 修改key
*
* @param oldkey
* @param newkey
* @return
*/
public static String rename(String oldkey, String newkey) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.rename(oldkey, newkey);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 如果不存在则修改key
*
* @param oldkey
* @param newkey
* @return
*/
public static Long renamenx(String oldkey, String newkey) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.renamenx(oldkey, newkey);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取value,并重新赋值
*
* @param key
* @param value
* @return
*/
public static String getSet(String key, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.getSet(key, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 给value追加值
*
* @param key
* @param value
* @return
*/
public static Long append(String key, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.append(key, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取指定位置的value值
*
* @param key
* @param start
* @param end
* @return
*/
public static String substr(String key, int start, int end) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.substr(key, start, end);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 存入map数据
*
* @param key
* @param hash
* @return
*/
public static String sethm(String key, Map<String, String> hash) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hmset(key, hash);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 存入MAP并设置超时
*
* @param key
* @param seconds 超时时间(秒)
* @param hash Map<String, String>
* @return Boolean
*/
public static Boolean sethmExpire(String key, int seconds, Map<String, String> hash) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
String state = jedis.hmset(key, hash);
if (state.equals("OK")) {
jedis.expire(key, seconds);
} else {
return false;
}
return true;
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 插入map指定的key对应的value
*
* @param key
* @param field
* @param value
* @return
*/
public static Long seth(String key, String field, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hset(key, field, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 若不存在则插入map指定的key对应的value
*
* @param key
* @param field
* @param value
* @return
*/
public static Long sethnx(String key, String field, String value) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hsetnx(key, field, value);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取map指定的key对应的value
*
* @param key
* @param fields
* @return
*/
public static List<String> gethm(String key, String fields) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hmget(key, fields);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 删除map指定的key
*
* @param key
* @param fields
* @return
*/
public static Long delh(String key, String fields) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hdel(key, fields);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
/**
* 获取指定key的MAP
*
* @param key
* @return Map<String, String>
*/
public static Map<String, String> gethAll(String key) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.hgetAll(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
public static void main(String[] args) {
String message = "C0700000000000046A100G10000001";
set("test", message);
System.out.println(get("test"));
}
/**
* 生成序列号
*
* @return
*/
public static Long incr(String key) {
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
return jedis.incr(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
return null;
} finally {
//返还到连接池
returnResource(pool, jedis);
}
}
}
5. 在设备心跳接口中,把设备ip存到rdis中 。
@RequestMapping(value = "/zs/heart", method = RequestMethod.POST) public String getheart(HttpServletRequest request, HttpServletResponse response){ String ip = request.getParameter("ip"); System.out.println(ip); RedisUtil.setex("offLine"+ip,90,"1"); // 键:offLine"+ip 存在Redis的时间:90秒 "1":数据为"1" LgdzEquipment equipment = lgdzEquipmentService.queryEquipmentByParameter(null,null,ip); System.out.println(equipment); if (null!=equipment.getSerialno()&&""!=equipment.getSerialno()){ if ("1".equals(equipment.getOnlinestate())){ //表示已经在线 System.out.println("进入在线状态==="); }else { //表示重新上线 logger.info("设备重新上线,ip:"+ip); System.out.println("进入重新上线==="); equipment.setOnlinestate("1");//1 在线 2.离线 lgdzEquipmentService.updateLgdzEquipment(equipment); } }else { //根据ip在项目中没有搜索到该设备 不做处理。 } String s1 = "{\"Response_AlarmInfoPlate\":{\"info\":\"no\",\"is_pay\":\"true\"}}"; return s1; }
来源:oschina
链接:https://my.oschina.net/daixingdeng/blog/4813820