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.RedisPubSubPublisher;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
|
@ -53,10 +53,8 @@ public class RedisPubSubConfiguration {
|
|||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(RedisSerializer.class)
|
||||
public static RPubSubListenerDetector topicListenerDetector(RedisMessageListenerContainer redisMessageListenerContainer,
|
||||
RedisSerializer<Object> redisSerializer) {
|
||||
return new RPubSubListenerDetector(redisMessageListenerContainer, redisSerializer);
|
||||
public static RPubSubListenerDetector topicListenerDetector(ApplicationContext applicationContext) {
|
||||
return new RPubSubListenerDetector(applicationContext);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springblade.core.tool.utils.ReflectUtil;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.Topic;
|
||||
|
|
@ -39,8 +40,7 @@ import java.lang.reflect.Method;
|
|||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class RPubSubListenerDetector implements BeanPostProcessor {
|
||||
private final RedisMessageListenerContainer redisMessageListenerContainer;
|
||||
private final RedisSerializer<Object> redisSerializer;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
|
|
@ -59,6 +59,10 @@ public class RPubSubListenerDetector implements BeanPostProcessor {
|
|||
}
|
||||
// 精准模式和模糊匹配模式
|
||||
Topic topic = ChannelUtil.getTopic(channel);
|
||||
// 获取参数
|
||||
RedisMessageListenerContainer redisMessageListenerContainer = getRedisMessageListenerContainer();
|
||||
RedisSerializer<?> redisSerializer = getRedisSerializer();
|
||||
// 添加到监听器处理
|
||||
redisMessageListenerContainer.addMessageListener((message, pattern) -> {
|
||||
String messageChannel = new String(message.getChannel());
|
||||
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