美文网首页
spring boot 整合redis

spring boot 整合redis

作者: PreacherZ | 来源:发表于2018-10-24 15:15 被阅读0次

引入pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
redis 配置类
import com.dxy.platform.scheduler.Receiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**

  • redis配置类

  • @author zxy
    **/
    @Configuration
    public class RedisConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
    MessageListenerAdapter listenerAdapter) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    //redis做消息列队监听的通道
    container.addMessageListener(listenerAdapter, new PatternTopic("webpush"));
    return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
    //监听处理bean 和处理方法
    return new MessageListenerAdapter(receiver, "receiveMessage");
    }
    //监听处理bean
    @Bean
    Receiver receiver() {
    return new Receiver();
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
    return new StringRedisTemplate(connectionFactory);
    }
    }

相关文章

网友评论

      本文标题:spring boot 整合redis

      本文链接:https://www.haomeiwen.com/subject/wpjyzftx.html