1丧慈、隊列初始化
@Component
public class DelayQueueManager implements CommandLineRunner {
@Resource
private AltcPsControlService altcPsControlService;
private final Logger logger = LoggerFactory.getLogger(DelayQueueManager.class);
private DelayQueue<DelayTask> delayQueue = new DelayQueue<>();
/**
* 加入到延時隊列中
* @param task
*/
public void put(DelayTask task) {
logger.info("加入延時任務:{}", task);
delayQueue.put(task);
}
/**
* 取消延時任務
* @param task
* @return
*/
public boolean remove(DelayTask task) {
logger.info("取消延時任務:{}", task);
return delayQueue.remove(task);
}
@Override
public void run(String... args) throws Exception {
logger.info("初始化延時隊列");
Executors.newSingleThreadExecutor().execute(new Thread(this::excuteThread));
this.initAltcPsControl();
}
/**
* 延時任務執(zhí)行線程
*/
private void excuteThread() {
while (true) {
try {
DelayTask task = delayQueue.take();
processTask(task);
} catch (InterruptedException e) {
break;
}
}
}
/**
* 初始化掃描布控表加入延遲隊列
* @Author caody
* @Date 2020/11/4 15:39
* @Param
* @return {@link }
**/
private void initAltcPsControl(){
//延遲1分鐘后執(zhí)行
try {
Thread.sleep(1000*60);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("初始化掃描布控表加入延遲隊列");
List<AltcPsControlEntity> controlEntityList = altcPsControlService.selectInitDelayQueue();
if (controlEntityList.size()>0){
for (AltcPsControlEntity psControlEntity : controlEntityList){
String identifier = psControlEntity.getId().toString();
long expire = psControlEntity.getEndTime().getTime() - psControlEntity.getBeginTime().getTime();
TaskBase taskBase = new TaskBase(identifier);
DelayTask delayTask = new DelayTask(taskBase,expire);
this.put(delayTask);
}
}
}
/**
* 內部執(zhí)行延時任務
* @param task
*/
private void processTask(DelayTask task) {
logger.info("執(zhí)行延時任務:{}", task);
//根據(jù)task中的data自定義數(shù)據(jù)來處理相關邏輯灭翔,例 if (task.getData() instanceof XXX) {}
TaskBase taskBase = task.getData();
Long id = Long.valueOf(taskBase.getIdentifier());
AltcPsControlEntity altcPsControlEntity = new AltcPsControlEntity();
altcPsControlEntity.setId(id);
altcPsControlEntity.setStatus((byte) 3);
boolean psControl = altcPsControlService.updateAltcPsControl(altcPsControlEntity);
if (!psControl){
logger.info("任務執(zhí)行失敗,重新放入隊列中,一小時后重新執(zhí)行:{}", task);
Long expire = 1000*60*60L;
DelayTask delayTask = new DelayTask(taskBase,expire);
this.put(delayTask);
}
}
}
2威恼、消息體結構
public class DelayTask implements Delayed {
final private TaskBase data;
final private long expire;
public DelayTask(TaskBase data, long expire) {
super();
this.data = data;
this.expire = expire + System.currentTimeMillis();
}
public TaskBase getData() {
return data;
}
public long getExpire() {
return expire;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof DelayTask) {
return this.data.getIdentifier().equals(((DelayTask) obj).getData().getIdentifier());
}
return false;
}
@Override
public String toString() {
return "{" + "data:" + data.toString() + "," + "expire:" + new Date(expire) + "}";
}
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(this.expire - System.currentTimeMillis(), unit);
}
@Override
public int compareTo(Delayed o) {
long delta = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS);
return (int) delta;
}
}
3积仗、消息內容
public class TaskBase {
private String identifier;
public TaskBase(String identifier) {
this.identifier = identifier;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}