|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.UserSetup; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.RedisKeyExpirationEventMessageListener; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.UserSetting; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.event.EventPublisher; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @date:   2020年5月6日 上午11:35:46 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class ZLMKeepliveTimeoutListener extends KeyExpirationEventMessageListener { | 
|---|
|  |  |  | public class ZLMKeepliveTimeoutListener extends RedisKeyExpirationEventMessageListener { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Logger logger = LoggerFactory.getLogger(ZLMKeepliveTimeoutListener.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private ZLMRESTfulUtils zlmresTfulUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private UserSetup userSetup; | 
|---|
|  |  |  | private UserSetting userSetting; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private IMediaServerService mediaServerService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void init() { | 
|---|
|  |  |  | if (!userSetup.getRedisConfig()) { | 
|---|
|  |  |  | // 配置springboot默认Config为空,即不让应用去修改redis的默认配置,因为Redis服务出于安全会禁用CONFIG命令给远程用户使用 | 
|---|
|  |  |  | setKeyspaceNotificationsConfigParameter(""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | super.init(); | 
|---|
|  |  |  | public ZLMKeepliveTimeoutListener(RedisMessageListenerContainer listenerContainer, UserSetting userSetting) { | 
|---|
|  |  |  | super(listenerContainer, userSetting); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public ZLMKeepliveTimeoutListener(RedisMessageListenerContainer listenerContainer) { | 
|---|
|  |  |  | super(listenerContainer); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 监听失效的key,key格式为keeplive_deviceId | 
|---|
|  |  |  | * @param message | 
|---|
|  |  |  | * @param pattern | 
|---|
|  |  |  | 
|---|
|  |  |  | public void onMessage(Message message, byte[] pattern) { | 
|---|
|  |  |  | //  获取失效的key | 
|---|
|  |  |  | String expiredKey = message.toString(); | 
|---|
|  |  |  | String KEEPLIVEKEY_PREFIX = VideoManagerConstants.MEDIA_SERVER_KEEPALIVE_PREFIX + userSetup.getServerId() + "_"; | 
|---|
|  |  |  | String KEEPLIVEKEY_PREFIX = VideoManagerConstants.MEDIA_SERVER_KEEPALIVE_PREFIX + userSetting.getServerId() + "_"; | 
|---|
|  |  |  | if(!expiredKey.startsWith(KEEPLIVEKEY_PREFIX)){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if (mediaServerConfig == null) { | 
|---|
|  |  |  | publisher.zlmOfflineEventPublish(mediaServerId); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | logger.info("[zlm心跳到期]:{}验证后zlm仍在线,回复心跳信息", mediaServerId); | 
|---|
|  |  |  | logger.info("[zlm心跳到期]:{}验证后zlm仍在线,恢复心跳信息", mediaServerId); | 
|---|
|  |  |  | // 添加zlm信息 | 
|---|
|  |  |  | mediaServerService.updateMediaServerKeepalive(mediaServerId, mediaServerConfig); | 
|---|
|  |  |  | } | 
|---|