监听redis消息队列的示例:
springBoot实现redis发布式监听消息队列,定时器监听不同功能的消息,代码:
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
@Component
public class RedisMessageListener {
private static final String RECEIVE_NAME = "test";
/**
* 监听quest1信息
*/
@Bean
public MessageListenerAdapter quest1() {
return new MessageListenerAdapter(new quest1(), RECEIVE_NAME);
}
/**
* 定义消息监听者容器
* @param connectionFactory 连接工厂
* @param quest1 监听quest1
* @return RedisMessageListenerContainer
*/
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter quest1) {
RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
listenerContainer.setConnectionFactory(connectionFactory);
listenerContainer.addMessageListener(接收消息--quest1, new PatternTopic("消息key"));
return listenerContainer;
}
}