1. 队列
//发布
@ApiOperation(value = "put 普通队列")
@PostMapping("/queuePut")
public Object put(@RequestBody CommonMapRespDTO respDTO) {
for (int i = 0; i < 20; i++) {
//队列
RQueue<Object> queue = redissonClient.getQueue(TopicConstant.normanQueueName);
//队列满 返回异常
queue.add(respDTO.getValue());
}
return "OK";
}
//消费
private void queueTest() {
ExecutorServiceCasche.EXECUTOR.execute(() -> {
//阻塞队列
RQueue<Object> queue = redissonClient.getQueue(TopicConstant.normanQueueName);
while (true) {
//如果为空则等待
Object object = queue.poll();
if (object == null) {
//50微妙
ThreadUtil.sleep(50, TimeUnit.MILLISECONDS);
continue;
}
System.out.println(Thread.currentThread().getName() + "读取到的值:" + object);
}
});
}
2. 阻塞队列
//发布数据
@ApiOperation(value = "put 阻塞队列")
@PostMapping("/blockingQueuePut")
public Object blockingQueue(@RequestBody CommonMapRespDTO respDTO) throws InterruptedException {
for (int i = 0; i < 20; i++) {
//阻塞队列
RBlockingQueue<Object> rBlockingQueue = redissonClient.getBlockingQueue(queueName);
rBlockingQueue.put(respDTO.getValue());
}
return "OK";
}
//消费
private void blockQueueTest() {
ExecutorServiceCasche.EXECUTOR.execute(() -> {
//阻塞队列
RBlockingQueue<Object> rBlockingQueue = redissonClient.getBlockingQueue(
RedissionController.queueName);
while (true) {
Object object = null;
try {
object = rBlockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "读取到的值:" + object);
}
});
}
3. 延迟队列
//发布
@ApiOperation(value = "delayedQueue 延时队列")
@PostMapping("/delayedQueue")
public Object delayedQueue(@RequestBody CommonMapRespDTO respDTO) {
Random random = new Random();
for (int i = 0; i < 20; i++) {
//基于redis发送变更通知
RBlockingQueue<Object> rBlockingQueue = redissonClient.getBlockingQueue(TopicConstant.delayedQueueName);
RDelayedQueue<Object> queue = redissonClient.getDelayedQueue(rBlockingQueue);
Integer temp = random.nextInt(20);
queue.offer(temp, temp, TimeUnit.SECONDS);
}
return "OK";
}
//消费
private void delayQueue() {
ExecutorServiceCasche.EXECUTOR.execute(() -> {
//阻塞队列
RBlockingDeque<Object> blockingDeque = redissonClient
.getBlockingDeque(TopicConstant.delayedQueueName);
RDelayedQueue<Object> queue = redissonClient.getDelayedQueue(blockingDeque);
while (true) {
Object object = null;
try {
object = blockingDeque.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "读取到的值:" + object);
}
});
}
4. 发布订阅
//发送
@ApiOperation(value = "topic 发布订阅模式")
@PostMapping("/topic")
public Object topic(@RequestBody CommonMapRespDTO respDTO) {
for (int i = 0; i < 20; i++) {
//基于redis发送变更通知
RTopic topic = redissonClient.getTopic(TopicConstant.DICT_CHANGE_TOPIC);
topic.publish("change");
}
return "OK";
}
//消费
private void dictDataChangelistener() {
//小代码变更 topic 发布订阅模式
RTopic rTopic = redissonClient.getTopic(TopicConstant.DICT_CHANGE_TOPIC);
rTopic.addListener(String.class, new MessageListener<String>() {
@Override
public void onMessage(CharSequence charSequence, String s) {
try {
String key = charSequence.toString();
//单线程更新
System.out.println(Thread.currentThread().getName() + "==>收到通知:" + key);
} catch (Exception e) {
log.error("==>同步小代码异常", e);
}
}
});
}