| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package com.tmzn.devicelinkykc.taskQueue.queue;
- import com.tmzn.devicelinkykc.socket.DeviceConnectionMsg;
- import com.tmzn.devicelinkykc.taskQueue.runner.MsgFreeRunner;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.ApplicationArguments;
- import org.springframework.boot.ApplicationRunner;
- import org.springframework.stereotype.Component;
- import java.util.Map;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- @Component
- @Slf4j(topic = "MsgFreeQueue")
- public class MsgFreeQueue extends Thread implements ApplicationRunner {
- private BlockingQueue<Map<String, DeviceConnectionMsg>> freeQueue = new ArrayBlockingQueue(1);
- @Autowired
- private MsgFreeRunner msgFreeRunner;
- public void add(Map<String, DeviceConnectionMsg> object) {
- // freeQueue.put(object);
- // 非阻塞
- boolean success = freeQueue.offer(object);
- if (!success) {
- log.info("队列已满,丢弃free设备状态更新: size={}", object.size());
- }
- }
- @Override
- public void run() {
- while (true) {
- try {
- Map<String, DeviceConnectionMsg> free = freeQueue.take();
- msgFreeRunner.freeMsg(free);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("执行异常", e);
- }
- }
- }
- @Override
- public void run(ApplicationArguments args) throws Exception {
- this.start();
- log.info("处理器启动");
- }
- }
|