feat(redis): 添加Redis消息订阅功能Demo
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2021-2026 Odboy
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package cn.odboy.framework.redis;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息订阅Demo
|
||||||
|
*
|
||||||
|
* @author odboy
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class DemoMessageSubscriber {
|
||||||
|
|
||||||
|
public void onMessage(String message, String channel) {
|
||||||
|
log.info("收到来自redis消息队列的message={}, channel={}", JSON.toJSONString(message), channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-5
@@ -34,16 +34,17 @@ public class RedisMessageConfig {
|
|||||||
/**
|
/**
|
||||||
* 消息监听容器
|
* 消息监听容器
|
||||||
*
|
*
|
||||||
* @param connectionFactory /
|
* @param connectionFactory /
|
||||||
* @param listenerAdapter /
|
* @param listenerWsMessageAdapter /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
|
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerWsMessageAdapter) {
|
||||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||||
container.setConnectionFactory(connectionFactory);
|
container.setConnectionFactory(connectionFactory);
|
||||||
// 指定监听的主题
|
// 指定监听的主题
|
||||||
container.addMessageListener(listenerAdapter, new ChannelTopic("WebSocketMessage"));
|
container.addMessageListener(listenerWsMessageAdapter, new ChannelTopic("DemoMessage"));
|
||||||
|
container.addMessageListener(listenerWsMessageAdapter, new ChannelTopic("WebSocketMessage"));
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +55,12 @@ public class RedisMessageConfig {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public MessageListenerAdapter listenerAdapter(WsMessageSubscriber subscriber) {
|
public MessageListenerAdapter listenerDemoMessageAdapter(DemoMessageSubscriber subscriber) {
|
||||||
|
return new MessageListenerAdapter(subscriber, "onMessage");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageListenerAdapter listenerWsMessageAdapter(WsMessageSubscriber subscriber) {
|
||||||
return new MessageListenerAdapter(subscriber, "onMessage");
|
return new MessageListenerAdapter(subscriber, "onMessage");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2021-2026 Odboy
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package cn.odboy.framework.redis;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息订阅Demo
|
||||||
|
*
|
||||||
|
* @author odboy
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class DemoMessageSubscriber {
|
||||||
|
|
||||||
|
public void onMessage(String message, String channel) {
|
||||||
|
log.info("收到来自redis消息队列的message={}, channel={}", JSON.toJSONString(message), channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-5
@@ -34,16 +34,17 @@ public class RedisMessageConfig {
|
|||||||
/**
|
/**
|
||||||
* 消息监听容器
|
* 消息监听容器
|
||||||
*
|
*
|
||||||
* @param connectionFactory /
|
* @param connectionFactory /
|
||||||
* @param listenerAdapter /
|
* @param listenerWsMessageAdapter /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
|
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerWsMessageAdapter) {
|
||||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||||
container.setConnectionFactory(connectionFactory);
|
container.setConnectionFactory(connectionFactory);
|
||||||
// 指定监听的主题
|
// 指定监听的主题
|
||||||
container.addMessageListener(listenerAdapter, new ChannelTopic("WebSocketMessage"));
|
container.addMessageListener(listenerWsMessageAdapter, new ChannelTopic("DemoMessage"));
|
||||||
|
container.addMessageListener(listenerWsMessageAdapter, new ChannelTopic("WebSocketMessage"));
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +55,12 @@ public class RedisMessageConfig {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public MessageListenerAdapter listenerAdapter(WsMessageSubscriber subscriber) {
|
public MessageListenerAdapter listenerDemoMessageAdapter(DemoMessageSubscriber subscriber) {
|
||||||
|
return new MessageListenerAdapter(subscriber, "onMessage");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageListenerAdapter listenerWsMessageAdapter(WsMessageSubscriber subscriber) {
|
||||||
return new MessageListenerAdapter(subscriber, "onMessage");
|
return new MessageListenerAdapter(subscriber, "onMessage");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user