目录结构如下:
application.properties配置文件(redis的配置):
spring.redis.host=localhost
spring.redis.pool.max-idle=300
spring.redis.pool.max-wait=3000
spring.redis.timeout=3000
spring.redis.port=6379
SbootInitializer.java文件 :
该类的作用是为了让spingboot项目可以从我的tomcat服务器启动
package com.sboot.boot;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import com.sboot.builder.SbootBuilder;
/**
* 用于从外部容器启动
*
* @author
* @version [版本号, 2017年12月21日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class SbootInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SbootBuilder.class);
}
}
SbootBuilder.java文件
package com.sboot.builder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
/**
* 工程组建类
*
* @author
* @version [版本号, 2017年12月21日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@SpringBootApplication
//扫描包,使类中的注解组建生效
@ComponentScan(value = {"com.sboot"})
public class SbootBuilder {
}
RedisConfig.java 文件
package com.sboot.redis;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
@Configuration
public class RedisConfig extends RedisAutoConfiguration {
@Autowired
private RedisProperties properties;
@Autowired
private ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration;
@Autowired
private ObjectProvider<RedisClusterConfiguration> clusterConfiguration;
@Bean
public RedisKeyListener redisKeyListener(){
return new RedisKeyListener();
}
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
List<Topic> topics = new ArrayList<>();
//监听0号库和1号库中的string 键的相关操作
topics.add(new PatternTopic("__keyevent@0__:*"));
topics.add(new PatternTopic("__keyevent@1__:*"));
container.addMessageListener(redisKeyListener(), topics);
return container;
}
}
RedisKeyListener.java 文件
package com.sboot.redis;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
/**
* <一句话功能简述>
* <功能详细描述>
*
* @version [版本号, 2017年12月29日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class RedisKeyListener implements MessageListener{
/**
*当0号库和1号库中的键发生相关改动该函数会被出发
* @param message
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
// TODO Auto-generated method stub
System.out.println("********hasdfasdf***********");
}
}
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.adyx.hubin.springboot</groupId>
<artifactId>sboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>sboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Java操作redis使用的jar包 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>2.0.2</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>sboot</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>2.3.2</version> -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
最后修改redis的配置文件
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
#notify-keyspace-events ""
notify-keyspace-events Eg$
重启redis服务器