redis监控,在docker容器内找到redis下(你的端口)18628文件,修改配置,打开配置config set notify-keyspace-events $E
E表示监控事件
redis监控命令:
config set notify-keyspace-events $E
config get notify-keyspace-events
psubscribe '__key*__:*'(本地测试)
1.KeyExpiredListener
@Slf4j
@Component("keyExpiredListener")
public class KeyExpiredListener extends JedisPubSub {
@Autowired
private RemoteControlOperate remoteControlOperate;
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
// System.out.println("onPSubscribe "
// + pattern + " " + subscribedChannels);
log.info("redis事件监听连接成功!");
}
@Override
public void onPMessage(String pattern, String channel, String message) {
// System.out.println("onPMessage pattern测试: "
// + pattern + "————" + channel + "————" + message);
remoteControlOperate.ykqOperate(message);
}
@Override
public void onMessage(String channel, String message) {
System.out.println("onMessage pattern "
+ "-" + channel + "-" + message);
}
}
2.RedisListenerExecution
@Component
public class RedisListenerExecution {
@Autowired
private KeyExpiredListener keyExpiredListener;
public void RedisListenerStart(){
try {
RedisListenerRunnable run = new RedisListenerRunnable(keyExpiredListener);
ThreadPoolExecutorUtil.getPoll().execute(run);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.RedisListenerRunnable
public class RedisListenerRunnable implements Runnable{
private KeyExpiredListener keyExpiredListener = null;
public RedisListenerRunnable(KeyExpiredListener keyExpiredListener){
this.keyExpiredListener = keyExpiredListener;
}
@Override
public void run(){
int i=0;
while (true){
try {
Jedis jedis = RedisPoolUtil.getJedis();
jedis.psubscribe(keyExpiredListener, "__key*__:*");
}catch (Exception e){
e.printStackTrace();
System.out.println("线程重启次数:"+(++i));
}
}
}
}
4.RedisPoolUtil
Slf4j
public class RedisPoolUtil {
private static JedisPool jedisPool = null;
//连接Redis连接池
public static Boolean ConnRedis(String RedisUrl) throws Exception{
Boolean connFlag=false;
try{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(8);
config.setMaxTotal(8);
config.setMinIdle(0);
String[] lstParas=RedisUrl.split(":",-1);
String redisHost=lstParas[0];
int redisPort=Integer.parseInt(lstParas[1]);
jedisPool = new JedisPool(config, redisHost, redisPort);
connFlag=true;
}
catch (Exception e){
CloseRedis();
log.error("连接Redis发生异常:"+e.getMessage());
}
return connFlag;
}
//关闭RedsPool
public static void CloseRedis(){
try{
if(jedisPool!=null){
jedisPool.close();
jedisPool.destroy();
jedisPool=null;
}
}
catch (Exception e){
log.error("关闭Redis发生异常:"+e.getMessage());
}
}
//获取Redis连接
public synchronized static Jedis getJedis() throws Exception {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
}
} catch (Exception e) {
log.error("获取Redis实例发生异常:"+e.getMessage());
}
return null;
}
//释放Redis连接
public static void close(final Jedis jedis) {
try{
if (jedis != null) {
jedis.close();
}
}
catch(Exception e){
}
}
//读取Redis值
public static String Read(String redisKey) throws Exception{
String readVal="";
try{
Jedis jedis = getJedis();
if(jedis!=null){
try{
readVal=jedis.get(redisKey);
}
catch (Exception e){
log.error("获取Redis标签="+redisKey+"值失败:"+e.getMessage());
}
close(jedis);
}
}
catch (Exception e){
log.error("获取Redis标签="+redisKey+"值异常:"+e.getMessage());
}
return readVal;
}
}
5.RemoteControlOperate
@Slf4j
@Component("remoteControlOperate")
public class RemoteControlOperate {
@Autowired
private CommonSqlMapper commonSqlMapper;
@Autowired
private SqlDataResolve sqlDataResolve;
@Autowired
private SysCommonFunc sysCommonFunc;
private Map<String,Object> remoteControlMap= new HashMap<>();
@PostConstruct
public void initRemoteControl() throws Exception{
try{
String sql = "select tag_addr from e_iot_me_device_remote_control order BY remote_control_id";
List<Map<String, Object>> itemList=commonSqlMapper.ExecSelectSql(sql);
for(Map<String,Object> map:itemList){
remoteControlMap.put(map.get("tag_addr").toString(),null);
}
log.info("初始化启动:"+remoteControlMap);
}
catch (Exception ex){
throw ex;
}
}
public void ykqOperate(String tag_addr){
// log.info("监听到遥控器点位:"+tag_addr);
if(!remoteControlMap.containsKey(tag_addr))
return;
try{
String TagValue = String.valueOf(ScadaClientApi.ReadScadaTagValue(tag_addr)==null? 0:ScadaClientApi.ReadScadaTagValue(tag_addr));
String TagNowValue = String.valueOf(remoteControlMap.get(tag_addr)==null? 0:remoteControlMap.get(tag_addr));
// String TagValue = String.valueOf(ScadaClientApi.ReadScadaTagValue(tag_addr));
// String TagNowValue = String.valueOf(remoteControlMap.get(tag_addr));
if(!TagNowValue.equals(TagValue)){
String alarmMsgSql = "select dev.device_des,dev.attribute3 as device_code,con.tag_des\n" +
"from e_iot_fmod_device dev,e_iot_me_device_remote_control con\n" +
"where dev.device_id = con.device_id\n" +
"and con.tag_addr='"+tag_addr+"'";
List<Map<String, Object>> itemList=commonSqlMapper.ExecSelectSql(alarmMsgSql);
for(Map<String,Object> map:itemList){
String device_des = map.get("device_des").toString();
String device_code = map.get("device_code").toString();
String tag_des = map.get("tag_des").toString();
if(TagValue.equals("1")){
Long alarm_id = sqlDataResolve.GetIncreaseID("e_iot_me_device_alarm_id_seq");
String insertSql="insert into e_iot_me_device_alarm "+"(alarm_id,func_code,func_des,device_code,device_des,alarm_part," +
"alarm_msg,alarm_reason,creation_date,reset_flag,solution,ykq_flag) " +
"values (" +alarm_id+"," +
"'"+device_code+"-ALARM'," +
"'"+device_code+"报警'," +
"'"+device_des+"'," +
"'"+device_code+"报警'," +
"'遥控器组'," +
"'"+tag_des+"'," +
"'1等于1'," +
"'"+sysCommonFunc.GetNowDateTime() +"'," +
"'N'," +
"'遥控器触发',"+
"'N')";
commonSqlMapper.ExecUpdateSql(insertSql);
// log.info("准备插入遥控器报警的执行sql :"+insertSql);
}
if(TagValue.equals("0")){
String updateSql="update e_iot_me_device_alarm set " +
"reset_date='"+sysCommonFunc.GetNowDateTime()+"'," +
"reset_flag='Y'," +
"reset_by='"+"遥控器报警自动解除"+"' " +
"where reset_flag='N' and alarm_msg='"+tag_des+"' and device_code='"+device_des+"' and ykq_flag ='N' ";
commonSqlMapper.ExecUpdateSql(updateSql);
// log.info("准备解除遥控器报警的执行sql :"+updateSql);
}
}
remoteControlMap.put(tag_addr,TagValue);
}
return;
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
6.JobRunner
@Slf4j
@Component
@RequiredArgsConstructor
public class JobRunner implements ApplicationRunner {
@Autowired
private CommonSqlMapper commonSqlMapper;
private final QuartzManage quartzManage;
@Autowired
private RedisListenerExecution redisListenerExecution;
/**
* 项目启动时重新激活启用的定时任务
*
* @param applicationArguments /
*/
@Override
public void run(ApplicationArguments applicationArguments) {
log.info("--------------------注入定时任务---------------------");
try {
log.info("-----注入redis监听任务-----");
redisListenerExecution.RedisListenerStart();
log.info("-------redis监听件务注人完成----------- -");
} catch (Exception e) {
log.error("定时任务启动发生异常,直接退出:"+e.getMessage());
System.exit(0);
}
}
}