Skip to content

Commit

Permalink
fix(redis) 监听器实例
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Apr 11, 2024
1 parent bb401ef commit a97db4e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
/**
* Redis消息发布-发送消息
*/
@Deprecated // todo 会有一些坑,比如:多加双引号 "" 或 json字符串内容转义
public class RedisMsgPublish {

@Autowired
private RedisTemplate redisTemplate;

/**
* 发布消息
*
* @param channel 通道
* @param message 消息
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.zja.redis.msg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
* @author: zhengja
* @since: 2024/04/11 14:01
*/
@Service
public class RedisMsgSenderService {

@Autowired
private RedisTemplate<String, String> redisTemplate;

public void sendMessage(String channel, Object message) {
redisTemplate.convertAndSend(channel, message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.zja.redis.msg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
* Redis 消息订阅监听器-接收消息
*/
@Component
public class RedisMsgSubscribeListenerV3 implements MessageListener {

/**
* 监听通道
*/
public final String channel = "channel-3";

/**
* 监听方法
*
* @param message 消息
* @param pattern 通道
*/
@Override
public void onMessage(Message message, byte[] pattern) {

// 消息体
String jsonUser = null;
try {
// 解决string乱码
jsonUser = new String(message.getBody(), StandardCharsets.UTF_8);

String channel = new String(pattern);
System.out.println("通道:" + channel);
System.out.println("消息体:" + jsonUser);

ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(jsonUser, User.class);
System.out.println("user:" + user);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

}

@Data
@NoArgsConstructor
@AllArgsConstructor
static class User {
private String id;
private String name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.zja.redis.msg;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* @author: zhengja
* @since: 2024/04/11 11:16
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserDTO implements Serializable {

private String id;
private String name;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @Date: 2021-10-28 16:23
* @Since:
*/
package com.zja.redis.config;
package com.zja.redis.msg.config;

import com.zja.redis.msg.RedisMsgPublish;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* @Date: 2021-10-28 16:24
* @Since:
*/
package com.zja.redis.config;
package com.zja.redis.msg.config;

import com.zja.redis.msg.RedisMsgSubscribeListenerV1;
import com.zja.redis.msg.RedisMsgSubscribeListenerV2;
import com.zja.redis.msg.RedisMsgSubscribeListenerV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -30,6 +31,9 @@ public class RedisMsgSubscribeConfig {
@Autowired
private RedisMsgSubscribeListenerV2 redisMsgSubscribeListenerV2;

@Autowired
private RedisMsgSubscribeListenerV3 redisMsgSubscribeListenerV3;

/**
* Redis 消息侦听器容器(工厂)
*/
Expand All @@ -45,6 +49,7 @@ public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnecti
*/
container.addMessageListener(redisMsgSubscribeListenerV1, new ChannelTopic(redisMsgSubscribeListenerV1.channel));
container.addMessageListener(redisMsgSubscribeListenerV2, new ChannelTopic(redisMsgSubscribeListenerV2.channel));
container.addMessageListener(redisMsgSubscribeListenerV3, new ChannelTopic(redisMsgSubscribeListenerV3.channel));
return container;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
*/
package com.zja.redis.msg;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
* 测试Redis 发布、订阅
Expand All @@ -22,13 +31,35 @@ public class RedisMsgPublishTests {
@Autowired
private RedisMsgPublish redisMsgPublish;

@Autowired
RedisMsgSenderService redisMsgSenderService;

@Test
public void pubMsg_V1() {
redisMsgPublish.pubMsg("channel-1", "channel-1 消息体");
// redisMsgPublish.pubMsg("channel-1", "channel-1 消息体");

redisMsgSenderService.sendMessage("channel-1", "channel-1 消息体");
}

@Test
public void pubMsg_V2() {
redisMsgPublish.pubMsg("channel-2", "channel-2 消息体");
// redisMsgPublish.pubMsg("channel-2", "channel-2 消息体");

redisMsgSenderService.sendMessage("channel-2", "channel-2 消息体");
}

@Test
public void pubMsg_V3() throws JsonProcessingException, UnsupportedEncodingException {
UserDTO userDTO = new UserDTO();
userDTO.setId("2");
userDTO.setName("李四");

ObjectMapper objectMapper = new ObjectMapper();
String jsonUser = objectMapper.writeValueAsString(userDTO); // 支持对象、对象集合、Map等转为json字符串
System.out.println("发送消息: " + jsonUser);

// redisMsgPublish.pubMsg("channel-3", jsonUser); // todo 不要用,有坑,会转义 或 多加 ""

redisMsgSenderService.sendMessage("channel-3", jsonUser);
}
}

0 comments on commit a97db4e

Please sign in to comment.