前言:
- SpringBoot整合Redis此处不作赘述
- 为减少篇幅,只有部分代码(不影响使用)
- 本文基于lombok插件以及logback日志,所以如果某些注解报错或者无法导入基本是这两处的问题
- 勿喷
正文
-
Redis常量配置(个人习惯,本类按需求而定)
public class RedisConstant { /** * 消息订阅点赞主题 */ public static final String TOPIC_PRAISE = "TOPIC_PRAISE"; /** * 消息订阅收藏主题 */ public static final String TOPIC_COLLECT = "TOPIC_COLLECT"; /** * 消息订阅评论主题 */ public static final String TOPIC_COMMENT = "TOPIC_COMMENT"; /** * 消息订阅关注主题 */ public static final String TOPIC_FOCUS = "TOPIC_FOCUS"; }
-
配置消息处理器
package com.java.single.service.impl; import java.util.concurrent.CountDownLatch; import com.java.single.entity.res.MessageCollect; import com.java.single.entity.res.MessageComment; import com.java.single.entity.res.MessageFocus; import com.java.single.util.PushUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; @Slf4j @Service public class ReceiverServiceImpl { private CountDownLatch latch; @Autowired public ReceiverServiceImpl(CountDownLatch latch) { this.latch = latch; } public void praiseReceive(MessagePraise messagePraise) { log.info("消费点赞数据:[{}]", messagePraise); PushUtil.pushOne(messagePraise.getToUserId(), "新消息", messagePraise.getUserName() + "点赞您的" + messagePraise.getTitle() + " 作品"); latch.countDown(); } public void collectReceive(MessageCollect messageCollect) { log.info("消费收藏数据:[{}]", messageCollect); Push