package com.zy.core.cache;
|
|
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;
|
|
/**
|
* 消息队列
|
* Created by vincent on 2020/8/5
|
*/
|
public class MessageQueue {
|
|
//四向穿梭车mq交换机
|
private static final Map<Integer, ConcurrentLinkedQueue<Task>> SHUTTLE_EXCHANGE = new ConcurrentHashMap<>();
|
//输送mq交换机
|
private static final Map<Integer, ConcurrentLinkedQueue<Task>> DEVP_EXCHANGE = new ConcurrentHashMap<>();
|
//提升机mq交换机
|
private static final Map<Integer, ConcurrentLinkedQueue<Task>> LIFT_EXCHANGE = new ConcurrentHashMap<>();
|
//货叉提升机mq交换机
|
private static final Map<Integer, ConcurrentLinkedQueue<Task>> FORK_LIFT_EXCHANGE = new ConcurrentHashMap<>();
|
//货叉提升机Master mq交换机
|
private static final Map<Integer, ConcurrentLinkedQueue<Task>> FORK_LIFT_MASTER_EXCHANGE = new ConcurrentHashMap<>();
|
//显示屏mq交换机
|
private static final Map<Integer, ConcurrentLinkedQueue<Task>> LED_EXCHANGE = new ConcurrentHashMap<>();
|
|
/**
|
* mq 交换机初始化
|
*/
|
public static void init(SlaveType type, Slave slave) {
|
switch (type) {
|
case Shuttle:
|
SHUTTLE_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>());
|
break;
|
case ForkLift:
|
FORK_LIFT_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>());
|
break;
|
case ForkLiftMaster:
|
FORK_LIFT_MASTER_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>());
|
break;
|
case Lift:
|
LIFT_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>());
|
break;
|
case Devp:
|
DEVP_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>());
|
break;
|
case Led:
|
LED_EXCHANGE.put(slave.getId(), new ConcurrentLinkedQueue<>());
|
break;
|
default:
|
break;
|
}
|
}
|
|
/**
|
* 添加元素
|
* 如果发现队列已满无法添加的话,会直接返回false。
|
*/
|
public static boolean offer(SlaveType type, Integer id, Task task) {
|
switch (type) {
|
case Shuttle:
|
return SHUTTLE_EXCHANGE.get(id).offer(task);
|
case ForkLift:
|
return FORK_LIFT_EXCHANGE.get(id).offer(task);
|
case ForkLiftMaster:
|
return FORK_LIFT_MASTER_EXCHANGE.get(id).offer(task);
|
case Lift:
|
return LIFT_EXCHANGE.get(id).offer(task);
|
case Devp:
|
return DEVP_EXCHANGE.get(id).offer(task);
|
case Led:
|
return LED_EXCHANGE.get(id).offer(task);
|
default:
|
return false;
|
}
|
}
|
|
/**
|
* 移除元素
|
* 若队列为空,返回null。
|
*/
|
public static Task poll(SlaveType type, Integer id) {
|
switch (type) {
|
case Shuttle:
|
return SHUTTLE_EXCHANGE.get(id).poll();
|
case ForkLift:
|
return FORK_LIFT_EXCHANGE.get(id).poll();
|
case ForkLiftMaster:
|
return FORK_LIFT_MASTER_EXCHANGE.get(id).poll();
|
case Lift:
|
return LIFT_EXCHANGE.get(id).poll();
|
case Devp:
|
return DEVP_EXCHANGE.get(id).poll();
|
case Led:
|
ConcurrentLinkedQueue<Task> task2 = LED_EXCHANGE.get(id);
|
if (task2 == null) {
|
return null;
|
}
|
return LED_EXCHANGE.get(id).poll();
|
default:
|
return null;
|
}
|
}
|
|
/**
|
* 取出元素,并不删除.
|
*/
|
public static Task peek(SlaveType type, Integer id) {
|
switch (type) {
|
case Shuttle:
|
return SHUTTLE_EXCHANGE.get(id).peek();
|
case ForkLift:
|
return FORK_LIFT_EXCHANGE.get(id).peek();
|
case ForkLiftMaster:
|
return FORK_LIFT_MASTER_EXCHANGE.get(id).peek();
|
case Lift:
|
return LIFT_EXCHANGE.get(id).peek();
|
case Devp:
|
return DEVP_EXCHANGE.get(id).peek();
|
case Led:
|
return LED_EXCHANGE.get(id).peek();
|
default:
|
return null;
|
}
|
}
|
|
public static void clear(SlaveType type, Integer id) {
|
switch (type) {
|
case Shuttle:
|
SHUTTLE_EXCHANGE.get(id).clear();
|
break;
|
case ForkLift:
|
FORK_LIFT_EXCHANGE.get(id).clear();
|
break;
|
case ForkLiftMaster:
|
FORK_LIFT_MASTER_EXCHANGE.get(id).clear();
|
break;
|
case Lift:
|
LIFT_EXCHANGE.get(id).clear();
|
break;
|
case Devp:
|
DEVP_EXCHANGE.get(id).clear();
|
break;
|
case Led:
|
LED_EXCHANGE.get(id).clear();
|
break;
|
default:
|
break;
|
}
|
}
|
|
}
|