Files
klp-oa/klp-demo/src/main/java/com/klp/demo/controller/RedisPubSubController.java

48 lines
1.2 KiB
Java
Raw Normal View History

2025-07-17 18:07:48 +08:00
package com.klp.demo.controller;
2021-12-21 10:15:12 +08:00
2025-07-17 18:07:48 +08:00
import com.klp.common.core.domain.R;
import com.klp.common.utils.redis.RedisUtils;
2021-12-21 10:15:12 +08:00
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Redis 发布订阅 演示案例
*
* @author Lion Li
*/
@RequiredArgsConstructor
2021-12-21 10:15:12 +08:00
@RestController
@RequestMapping("/demo/redis/pubsub")
public class RedisPubSubController {
/**
* 发布消息
*
* @param key 通道Key
* @param value 发送内容
*/
2021-12-21 10:15:12 +08:00
@GetMapping("/pub")
public R<Void> pub(String key, String value) {
2021-12-21 10:15:12 +08:00
RedisUtils.publish(key, value, consumer -> {
System.out.println("发布通道 => " + key + ", 发送值 => " + value);
});
return R.ok("操作成功");
2021-12-21 10:15:12 +08:00
}
/**
* 订阅消息
*
* @param key 通道Key
*/
2021-12-21 10:15:12 +08:00
@GetMapping("/sub")
public R<Void> sub(String key) {
2021-12-21 10:15:12 +08:00
RedisUtils.subscribe(key, String.class, msg -> {
System.out.println("订阅通道 => " + key + ", 接收值 => " + msg);
});
return R.ok("操作成功");
2021-12-21 10:15:12 +08:00
}
}