mirror of
https://gitee.com/smallc/blade-tool.git
synced 2026-07-10 18:39:44 +08:00
✨ blade-starter-redis 优化 pub sub
This commit is contained in:
parent
2673bb3a62
commit
f7eaeb0f7d
|
|
@ -23,8 +23,8 @@ import org.springblade.core.redis.pubsub.RPubSubListenerLazyFilter;
|
||||||
import org.springblade.core.redis.pubsub.RPubSubPublisher;
|
import org.springblade.core.redis.pubsub.RPubSubPublisher;
|
||||||
import org.springblade.core.redis.pubsub.RedisPubSubPublisher;
|
import org.springblade.core.redis.pubsub.RedisPubSubPublisher;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||||
|
|
@ -53,10 +53,8 @@ public class RedisPubSubConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnBean(RedisSerializer.class)
|
public static RPubSubListenerDetector topicListenerDetector(ApplicationContext applicationContext) {
|
||||||
public static RPubSubListenerDetector topicListenerDetector(RedisMessageListenerContainer redisMessageListenerContainer,
|
return new RPubSubListenerDetector(applicationContext);
|
||||||
RedisSerializer<Object> redisSerializer) {
|
|
||||||
return new RPubSubListenerDetector(redisMessageListenerContainer, redisSerializer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springblade.core.tool.utils.ReflectUtil;
|
import org.springblade.core.tool.utils.ReflectUtil;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||||
import org.springframework.data.redis.listener.Topic;
|
import org.springframework.data.redis.listener.Topic;
|
||||||
|
|
@ -39,8 +40,7 @@ import java.lang.reflect.Method;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class RPubSubListenerDetector implements BeanPostProcessor {
|
public class RPubSubListenerDetector implements BeanPostProcessor {
|
||||||
private final RedisMessageListenerContainer redisMessageListenerContainer;
|
private final ApplicationContext applicationContext;
|
||||||
private final RedisSerializer<Object> redisSerializer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||||
|
|
@ -59,6 +59,10 @@ public class RPubSubListenerDetector implements BeanPostProcessor {
|
||||||
}
|
}
|
||||||
// 精准模式和模糊匹配模式
|
// 精准模式和模糊匹配模式
|
||||||
Topic topic = ChannelUtil.getTopic(channel);
|
Topic topic = ChannelUtil.getTopic(channel);
|
||||||
|
// 获取参数
|
||||||
|
RedisMessageListenerContainer redisMessageListenerContainer = getRedisMessageListenerContainer();
|
||||||
|
RedisSerializer<?> redisSerializer = getRedisSerializer();
|
||||||
|
// 添加到监听器处理
|
||||||
redisMessageListenerContainer.addMessageListener((message, pattern) -> {
|
redisMessageListenerContainer.addMessageListener((message, pattern) -> {
|
||||||
String messageChannel = new String(message.getChannel());
|
String messageChannel = new String(message.getChannel());
|
||||||
Object body = redisSerializer.deserialize(message.getBody());
|
Object body = redisSerializer.deserialize(message.getBody());
|
||||||
|
|
@ -78,4 +82,11 @@ public class RPubSubListenerDetector implements BeanPostProcessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private RedisMessageListenerContainer getRedisMessageListenerContainer() {
|
||||||
|
return applicationContext.getBean(RedisMessageListenerContainer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RedisSerializer<?> getRedisSerializer() {
|
||||||
|
return applicationContext.getBean(RedisSerializer.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user