package com.zy.bms.schedule; import com.zy.bms.model.Device; import com.zy.bms.service.DeviceService; import com.zy.bms.service.RabbitMQApi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.util.List; import java.util.Set; /** * 定时更新设备在线状态 * * @author yang xiao kun * create on 2021/5/14 */ @EnableAsync @Configuration @EnableScheduling public class DeviceStatusSchedule { @Autowired private RabbitMQApi rabbitMQApi; @Autowired private DeviceService deviceService; /** * 更新频率 2分钟 * 定时更新设备在线状态 */ @Async @Scheduled(cron = "0 0/2 * * * ?") public void updateDeviceStatus() { //全部的设备 List devices = deviceService.list(); Set online = rabbitMQApi.getOnlineDevice(); for (Device item : devices) { item.setStatus(online.contains(item.getClientId()) ? 1 : 0); } deviceService.updateDeviceStatus(devices); } }