创建spring-boot 项目
pom.xml引入依赖:4.0.0 com.cherrish demo-redis 0.0.1-SNAPSHOT jar demo-redis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
application.properties:
spring.redis.database=4spring.redis.host=192.168.1.17spring.redis.port=6379spring.redis.password=redis
Java代码:
/*********************************DemoRedisApplication.java*********************************/package com.cherrish.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoRedisApplication { public static void main(String[] args) { SpringApplication.run(DemoRedisApplication.class, args); }}/*********************************RedisConfig.java*********************************/package com.cherrish.demo;import com.cherrish.demo.redis.MessageReceiver;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;/** * @author cherrish * @time 2018-08-30 14:51 * @name RedisConfig * @desc: */@Configurationpublic class RedisConfig { @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(MessageReceiver receiver){ return new MessageListenerAdapter(receiver, "receiveMessage"); } @Bean StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory){ return new StringRedisTemplate(connectionFactory); }}/*********************************MessageReceiver.java*********************************/package com.cherrish.demo.redis;import org.springframework.stereotype.Component;/** * @author cherrish * @time 2018-08-30 15:05 * @name MessageReceiver * @desc: */@Componentpublic class MessageReceiver { public void receiveMessage(String message){ System.out.println("receive a message: " + message); }}/*********************************MessageSender.java*********************************/package com.cherrish.demo.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;/** * @author cherrish * @time 2018-08-30 15:01 * @name MessageSender * @desc: */@EnableScheduling@Componentpublic class MessageSender { @Autowired private StringRedisTemplate stringRedisTemplate; @Scheduled(fixedRate = 2000) public void sendMessage(){ stringRedisTemplate.convertAndSend("chat", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); }}