@auther:Lianggs
由于springboot 2.0升级后,很多的以前的方法都已被替换或者删除,导致原来可用的方法都不能用了,折腾了一天半的时间,终于搞定了。分享出来供有需求的同仁参考
配置什么的就不介绍了,和以前一样,
实现代码,config:
import java.util.concurrent.CountDownLatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
@Bean
Receiver receiver(CountDownLatch latch) {
return new Receiver(latch);
}
@Bean
CountDownLatch latch() {
return new CountDownLatch(1);
}
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
public class Receiver {
private CountDownLatch latch;
@Autowired
public Receiver(CountDownLatch latch) {
this.latch = latch;
}
public void receiveMessage(String message) {
latch.countDown();
}
}
}
工具类:
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
@Component
public class RedisService {
private static final Integer time = 300;
private static RedisTemplate redisTemplate;
@Resource
public void setRedisTemplate(RedisTemplate redisTemplate){
this.redisTemplate=redisTemplate;
}
/**
* 设置过期时间,秒
* @param key
* @param expireTime
* @return
*/
public static Boolean expire(String key,Integer expireTime){
return redisTemplate.expire(key,expireTime,TimeUnit.SECONDS);
}
public static void set(String key,Serializable value,Integer time){
redisTemplate.opsForValue().set(key,value);
expire(key,time);
}
public static void set(String key,Serializable value){
redisTemplate.opsForValue().set(key,value);
expire(key,time);
}
public static Object get(String key){
return redisTemplate.opsForValue().get(key);
}
public static Boolean delete(String key){
return redisTemplate.delete(key);
}
public static Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
}