| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.zy.asrs.domain.NotifyDto; |
| | | import com.zy.asrs.entity.HttpRequestLog; |
| | | import com.zy.asrs.service.HttpRequestLogService; |
| | | import com.zy.asrs.utils.NotifyUtils; |
| | | import com.zy.common.utils.HttpHandler; |
| | | import com.zy.common.utils.RedisUtil; |
| | |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Component |
| | |
| | | private SlaveProperties slaveProperties; |
| | | @Autowired |
| | | private ConfigService configService; |
| | | @Autowired |
| | | private HttpRequestLogService httpRequestLogService; |
| | | |
| | | @Scheduled(cron = "0/3 * * * * ? ") |
| | | public synchronized void notifyShuttle(){ |
| | |
| | | } |
| | | } |
| | | |
| | | private synchronized void notifyMsg(String deviceType, Integer device) { |
| | | @Scheduled(cron = "0/3 * * * * ? ") |
| | | public synchronized void notifyTask(){ |
| | | notifyMsg("task", 1); |
| | | } |
| | | |
| | | private synchronized void notifyMsg(String notifyType, Integer device) { |
| | | Config notifyEnableConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyEnable")); |
| | | if(notifyEnableConfig == null){ |
| | | return; |
| | |
| | | } |
| | | String notifyUriPath = notifyUriPathConfig.getValue(); |
| | | |
| | | List<String> keys = notifyUtils.takeKeys(deviceType, device); |
| | | List<String> keys = notifyUtils.takeKeys(notifyType, device); |
| | | if(keys == null){ |
| | | return; |
| | | } |
| | |
| | | continue; |
| | | } |
| | | |
| | | HttpRequestLog httpRequestLog = new HttpRequestLog(); |
| | | httpRequestLog.setName(notifyUri + notifyUriPath); |
| | | httpRequestLog.setRequest(JSON.toJSONString(notifyDto)); |
| | | httpRequestLog.setCreateTime(new Date()); |
| | | |
| | | try { |
| | | //触发通知 |
| | | String response = new HttpHandler.Builder() |
| | |
| | | .setJson(JSON.toJSONString(notifyDto)) |
| | | .build() |
| | | .doPost(); |
| | | httpRequestLog.setResponse(response); |
| | | |
| | | JSONObject jsonObject = JSON.parseObject(response); |
| | | Integer code = jsonObject.getInteger("code"); |
| | | if(code == 200){ |
| | | //通知成功 |
| | | redisUtil.del(key); |
| | | return; |
| | | |
| | | continue; |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | }finally { |
| | | //保存记录 |
| | | httpRequestLogService.insert(httpRequestLog); |
| | | } |
| | | |
| | | //通知失败 |
| | |
| | | if (times >= notifyDto.getFailTimes()) { |
| | | //超过次数 |
| | | redisUtil.del(key); |
| | | return; |
| | | continue; |
| | | } |
| | | |
| | | notifyDto.setLastRetryTime(System.currentTimeMillis()); |
| | | notifyDto.setRetryTimes(times); |
| | | redisUtil.set(key, notifyDto); |
| | | return; |
| | | continue; |
| | | } |
| | | } |
| | | |