-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhengjiaao
committed
Apr 11, 2024
1 parent
bb401ef
commit a97db4e
Showing
7 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
starter-data/starter-data-redis/src/main/java/com/zja/redis/msg/RedisMsgSenderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...-data/starter-data-redis/src/main/java/com/zja/redis/msg/RedisMsgSubscribeListenerV3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
starter-data/starter-data-redis/src/main/java/com/zja/redis/msg/UserDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters