|  |  | 
 |  |  | package com.zy.core.cache; | 
 |  |  |  | 
 |  |  | import com.zy.asrs.utils.CommandUtils; | 
 |  |  | import com.zy.core.Slave; | 
 |  |  | import com.zy.core.enums.SlaveType; | 
 |  |  | import com.zy.core.model.Task; | 
 |  |  | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.concurrent.ConcurrentHashMap; | 
 |  |  | import java.util.concurrent.ConcurrentLinkedQueue; | 
 |  |  | import java.util.concurrent.LinkedBlockingQueue; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 消息队列 | 
 |  |  | 
 |  |  | public class MessageQueue { | 
 |  |  |  | 
 |  |  |     // 堆垛机mq交换机 | 
 |  |  |     private static final Map<Integer, ConcurrentLinkedQueue<Task>> CRN_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     private static final Map<Integer, LinkedBlockingQueue<Task>> CRN_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     // 输送线mq交换机 | 
 |  |  |     private static final Map<Integer, ConcurrentLinkedQueue<Task>> DEVP_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     // 条码扫描仪mq交换机 | 
 |  |  |     private static final Map<Integer, ConcurrentLinkedQueue<Task>> BARCODE_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     // Led灯 mq交换机 | 
 |  |  |     private static final Map<Integer, ConcurrentLinkedQueue<Task>> LED_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     private static final Map<Integer, LinkedBlockingQueue<Task>> LED_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     // 磅称mq交换机 | 
 |  |  |     private static final Map<Integer, ConcurrentLinkedQueue<Task>> SCALE_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |     // 台车mq交换机 | 
 |  |  |     private static final Map<Integer, ConcurrentLinkedQueue<Task>> CAR_EXCHANGE = new ConcurrentHashMap<>(); | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * mq 交换机初始化 | 
 |  |  | 
 |  |  |     public static void init(SlaveType type, Slave slave) { | 
 |  |  |         switch (type) { | 
 |  |  |             case Crn: | 
 |  |  |                 CRN_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>()); | 
 |  |  |                 CRN_EXCHANGE.put(slave.getId(), new LinkedBlockingQueue<>(1)); | 
 |  |  |                 break; | 
 |  |  |             case Devp: | 
 |  |  |                 DEVP_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>()); | 
 |  |  | 
 |  |  |                 BARCODE_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>()); | 
 |  |  |                 break; | 
 |  |  |             case Led: | 
 |  |  |                 LED_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>()); | 
 |  |  |                 LED_EXCHANGE.put(slave.getId(), new LinkedBlockingQueue<>(1)); | 
 |  |  |                 break; | 
 |  |  |             case Scale: | 
 |  |  |                 SCALE_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>()); | 
 |  |  |                 break; | 
 |  |  |             case Car: | 
 |  |  |                 CAR_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>()); | 
 |  |  |                 break; | 
 |  |  |             default: | 
 |  |  |                 break; | 
 |  |  | 
 |  |  |                 return LED_EXCHANGE.get(id).offer(task); | 
 |  |  |             case Scale: | 
 |  |  |                 return SCALE_EXCHANGE.get(id).offer(task); | 
 |  |  |             case Car: | 
 |  |  |                 return CAR_EXCHANGE.get(id).offer(task); | 
 |  |  |             default: | 
 |  |  |                 return false; | 
 |  |  |         } | 
 |  |  | 
 |  |  |                 return LED_EXCHANGE.get(id).poll(); | 
 |  |  |             case Scale: | 
 |  |  |                 return SCALE_EXCHANGE.get(id).poll(); | 
 |  |  |             case Car: | 
 |  |  |                 return CAR_EXCHANGE.get(id).poll(); | 
 |  |  |             default: | 
 |  |  |                 return null; | 
 |  |  |         } | 
 |  |  | 
 |  |  |                 return LED_EXCHANGE.get(id).peek(); | 
 |  |  |             case Scale: | 
 |  |  |                 return SCALE_EXCHANGE.get(id).peek(); | 
 |  |  |             case Car: | 
 |  |  |                 return CAR_EXCHANGE.get(id).peek(); | 
 |  |  |             default: | 
 |  |  |                 return null; | 
 |  |  |         } | 
 |  |  | 
 |  |  |     public static void clear(SlaveType type, Integer id){ | 
 |  |  |         switch (type) { | 
 |  |  |             case Crn: | 
 |  |  |                  | 
 |  |  |                 CRN_EXCHANGE.get(id).clear(); | 
 |  |  |                 break; | 
 |  |  |             case Devp: | 
 |  |  | 
 |  |  |             case Scale: | 
 |  |  |                 SCALE_EXCHANGE.get(id).clear(); | 
 |  |  |                 break; | 
 |  |  |             case Car: | 
 |  |  |                 CAR_EXCHANGE.get(id).clear(); | 
 |  |  |                 break; | 
 |  |  |             default: | 
 |  |  |                 break; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     public static boolean offer(SlaveType type, Integer devpId, Task task, Runnable callback) { | 
 |  |  |         boolean result = offer(type, devpId, task); // 先执行原有任务逻辑 | 
 |  |  |         if (result && callback != null) { | 
 |  |  |             new Thread(() -> { | 
 |  |  |                 try { | 
 |  |  |                     Thread.sleep(200); // 模拟任务执行时间 | 
 |  |  |                     callback.run(); | 
 |  |  |                 } catch (InterruptedException e) { | 
 |  |  |                     Thread.currentThread().interrupt(); | 
 |  |  |                 } | 
 |  |  |             }).start(); | 
 |  |  |         } | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |