MsgFreeQueue.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package com.tmzn.devicelinkykc.taskQueue.queue;
  2. import com.tmzn.devicelinkykc.socket.DeviceConnectionMsg;
  3. import com.tmzn.devicelinkykc.taskQueue.runner.MsgFreeRunner;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.ApplicationArguments;
  7. import org.springframework.boot.ApplicationRunner;
  8. import org.springframework.stereotype.Component;
  9. import java.util.Map;
  10. import java.util.concurrent.ArrayBlockingQueue;
  11. import java.util.concurrent.BlockingQueue;
  12. @Component
  13. @Slf4j(topic = "MsgFreeQueue")
  14. public class MsgFreeQueue extends Thread implements ApplicationRunner {
  15. private BlockingQueue<Map<String, DeviceConnectionMsg>> freeQueue = new ArrayBlockingQueue(1);
  16. @Autowired
  17. private MsgFreeRunner msgFreeRunner;
  18. public void add(Map<String, DeviceConnectionMsg> object) {
  19. // freeQueue.put(object);
  20. // 非阻塞
  21. boolean success = freeQueue.offer(object);
  22. if (!success) {
  23. log.info("队列已满,丢弃free设备状态更新: size={}", object.size());
  24. }
  25. }
  26. @Override
  27. public void run() {
  28. while (true) {
  29. try {
  30. Map<String, DeviceConnectionMsg> free = freeQueue.take();
  31. msgFreeRunner.freeMsg(free);
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. log.error("执行异常", e);
  35. }
  36. }
  37. }
  38. @Override
  39. public void run(ApplicationArguments args) throws Exception {
  40. this.start();
  41. log.info("处理器启动");
  42. }
  43. }