Remove Embedded Redis

Fixes gh-248
This commit is contained in:
Rob Winch
2015-07-24 15:40:55 -05:00
parent e4c74ebe91
commit 8eca5ea4c6
30 changed files with 79 additions and 331 deletions

View File

@@ -17,7 +17,6 @@ group = 'samples'
dependencies {
compile project(':spring-session'),
project(':samples:spring-embedded-redis'),
"org.springframework.boot:spring-boot-starter-redis",
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-thymeleaf",

View File

@@ -15,11 +15,9 @@
*/
package sample.config;
import org.springframework.session.data.redis.config.annotation.web.http.*;
import org.springframework.session.redis.embedded.EnableEmbeddedRedis;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@EnableEmbeddedRedis
// tag::class[]
@EnableRedisHttpSession // <1>
public class HttpSessionConfig { }

View File

@@ -8,7 +8,6 @@ sonarRunner {
dependencies {
compile project(':spring-session-data-redis'),
project(':samples:spring-embedded-redis'),
"org.springframework:spring-web:$springVersion",
jstlDependencies

View File

@@ -7,14 +7,12 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- tag::beans[] -->
<bean class="org.springframework.session.redis.embedded.EmbeddedRedisConfiguration"/> <!--1-->
<!--2-->
<!--1-->
<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<!--3-->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:port="${spring.redis.port}"/>
<!--2-->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>
<!-- end::beans[] -->
</beans>

View File

@@ -8,7 +8,6 @@ sonarRunner {
dependencies {
compile project(':spring-session-data-redis'),
project(':samples:spring-embedded-redis'),
"org.springframework:spring-web:$springVersion",
jstlDependencies

View File

@@ -18,19 +18,14 @@ package sample;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.redis.embedded.EnableEmbeddedRedis;
import org.springframework.session.redis.embedded.RedisServerPort;
// tag::class[]
@EnableEmbeddedRedis // <1>
@EnableRedisHttpSession // <2>
@EnableRedisHttpSession // <1>
public class Config {
@Bean
public JedisConnectionFactory connectionFactory(@RedisServerPort int port) {
JedisConnectionFactory connection = new JedisConnectionFactory(); // <3>
connection.setPort(port);
return connection;
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(); // <2>
}
}
// end::class[]

View File

@@ -8,7 +8,6 @@ sonarRunner {
dependencies {
compile project(':spring-session-data-redis'),
project(':samples:spring-embedded-redis'),
"org.springframework:spring-webmvc:$springVersion",
"org.springframework.security:spring-security-config:$springSecurityVersion",
"org.springframework.security:spring-security-web:$springSecurityVersion",

View File

@@ -19,27 +19,22 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.redis.embedded.EnableEmbeddedRedis;
import org.springframework.session.redis.embedded.RedisServerPort;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;
// tag::class[]
@Configuration
@EnableEmbeddedRedis // <1>
@EnableRedisHttpSession // <2>
@EnableRedisHttpSession // <1>
public class HttpSessionConfig {
@Bean
public JedisConnectionFactory connectionFactory(@RedisServerPort int port) {
JedisConnectionFactory factory = new JedisConnectionFactory(); // <3>
factory.setPort(port);
return factory;
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(); // <2>
}
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy(); // <4>
return new HeaderHttpSessionStrategy(); // <3>
}
}
// end::class[]

View File

@@ -8,7 +8,6 @@ sonarRunner {
dependencies {
compile project(':spring-session-data-redis'),
project(':samples:spring-embedded-redis'),
"org.springframework:spring-web:$springVersion",
"org.springframework.security:spring-security-config:$springSecurityVersion",
"org.springframework.security:spring-security-web:$springSecurityVersion",

View File

@@ -19,20 +19,15 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.redis.embedded.EnableEmbeddedRedis;
import org.springframework.session.redis.embedded.RedisServerPort;
// tag::class[]
@Configuration
@EnableEmbeddedRedis // <1>
@EnableRedisHttpSession // <2>
@EnableRedisHttpSession // <1>
public class Config {
@Bean
public JedisConnectionFactory connectionFactory(@RedisServerPort int port) {
JedisConnectionFactory connection = new JedisConnectionFactory(); // <3>
connection.setPort(port);
return connection;
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(); // <2>
}
}
// end::class[]

View File

@@ -1,14 +0,0 @@
apply from: JAVA_GRADLE
tasks.findByPath("artifactoryPublish")?.enabled = false
sonarRunner {
skipProject = true
}
dependencies {
compile "com.github.kstyrc:embedded-redis:$embeddedRedisVersion",
"org.springframework:spring-context:$springVersion"
testCompile "junit:junit:$junitVersion",
"org.springframework.security:spring-security-test:$springSecurityVersion"
}

View File

@@ -1,113 +0,0 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.redis.embedded;
import java.io.IOException;
import java.net.ServerSocket;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import redis.embedded.RedisServer;
/**
* Runs an embedded Redis instance. This is only necessary since we do not want
* users to have to setup a Redis instance. In a production environment, this
* would not be used since a Redis Server would be setup.
*
* @author Rob Winch
*/
@Configuration
class EmbeddedRedisConfiguration {
public static final String SERVER_PORT_PROP_NAME = "spring.redis.port";
@Bean
public static RedisServerBean redisServer(ConfigurableEnvironment env) {
RedisServerBean bean = new RedisServerBean();
env.getPropertySources().addLast(bean);
return bean;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
/**
* Implements BeanDefinitionRegistryPostProcessor to ensure this Bean is initialized
* before any other Beans. Specifically, we want to ensure that the Redis Server is
* started before RedisHttpSessionConfiguration attempts to enable Keyspace
* notifications. We also want to ensure that we are able to register the
* {@link PropertySource} before any beans are initialized.
*/
static class RedisServerBean extends PropertySource<RedisServerBean> implements InitializingBean, DisposableBean, BeanDefinitionRegistryPostProcessor {
private final int port = getAvailablePort();
private RedisServer redisServer;
public RedisServerBean() {
super("redisServerPortPropertySource");
}
public void afterPropertiesSet() throws Exception {
redisServer = new RedisServer(port);
redisServer.start();
}
public void destroy() throws Exception {
if(redisServer != null) {
redisServer.stop();
}
}
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
@Override
public Object getProperty(String name) {
if(SERVER_PORT_PROP_NAME.equals(name)) {
return port;
}
return null;
}
private static int getAvailablePort() {
ServerSocket socket = null;
try {
socket = new ServerSocket(0);
return socket.getLocalPort();
} catch(IOException e) {
throw new RuntimeException(e);
} finally {
try {
if(socket != null) {
socket.close();
}
}catch(IOException e) {}
}
}
}
}

View File

@@ -1,64 +0,0 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.redis.embedded;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* <p>
* Runs an embedded Redis instance on a random available port.This is only necessary
* sincewe do not want users to have to setup a Redis instance. In a production
* environment, this would not be used since a Redis Server would be setup.
* </p>
* <p>
* The port being used can be identified by using {@literal @RedisServerPort} on a Spring
* Bean. For example:
* </p>
*
* <pre>
* {@literal @Configuration}
* {@literal @EnableEmbeddedRedis}
* public class RedisHttpSessionConfig {
*
* {@literal @Bean}
* public JedisConnectionFactory connectionFactory({@literal @RedisServerPort} int port) throws Exception {
* JedisConnectionFactory connection = new JedisConnectionFactory();
* connection.setPort(port);
* return connection;
* }
*
* }
* </pre>
*
* See <a href="https://github.com/spring-projects/spring-session/issues/121"
* >spring-projects/spring-session/issues/121</a> for details on exposing embedded Redis
* support.
*
* @author Rob Winch
* @see RedisServerPort
*
*/
@Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value={java.lang.annotation.ElementType.TYPE})
@Documented
@Import(EmbeddedRedisConfiguration.class)
@Configuration
public @interface EnableEmbeddedRedis {}

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.redis.embedded;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Value;
/**
* A convenience for finding the Redis Server port when using {@link EnableEmbeddedRedis}. For example:
*
* <pre>
* {@literal @Configuration}
* {@literal @EnableEmbeddedRedis}
* public class RedisHttpSessionConfig {
*
* {@literal @Bean}
* public JedisConnectionFactory connectionFactory({@literal @RedisServerPort} int port) throws Exception {
* JedisConnectionFactory connection = new JedisConnectionFactory();
* connection.setPort(port);
* return connection;
* }
*
* }
* </pre>
*
* @author Rob Winch
*/
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Value("${"+EmbeddedRedisConfiguration.SERVER_PORT_PROP_NAME+"}")
public @interface RedisServerPort { }

View File

@@ -8,7 +8,6 @@ sonarRunner {
dependencies {
compile project(':spring-session-data-redis'),
project(':samples:spring-embedded-redis'),
"org.springframework:spring-web:$springVersion",
jstlDependencies

View File

@@ -19,24 +19,19 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.redis.embedded.EnableEmbeddedRedis;
import org.springframework.session.redis.embedded.RedisServerPort;
/**
* @author Rob Winch
*/
@EnableEmbeddedRedis
// tag::class[]
@Configuration
@EnableRedisHttpSession
public class Config {
@Bean
public JedisConnectionFactory connectionFactory(@RedisServerPort int port) {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setPort(port);
return connection;
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
}
// end::class[]

View File

@@ -18,7 +18,6 @@ group = 'samples'
dependencies {
compile project(':spring-session-data-redis'),
project(':samples:spring-embedded-redis'),
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-data-jpa",
"org.springframework.boot:spring-boot-starter-thymeleaf",

View File

@@ -22,7 +22,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.session.redis.embedded.RedisServerPort;
@Configuration
public class DataSourceConfig {
@@ -34,9 +33,7 @@ public class DataSourceConfig {
}
@Bean
public JedisConnectionFactory connectionFactory(@RedisServerPort int port) {
JedisConnectionFactory connection = new JedisConnectionFactory(); // <3>
connection.setPort(port);
return connection;
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
}

View File

@@ -27,12 +27,10 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.redis.embedded.EnableEmbeddedRedis;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableEmbeddedRedis
// tag::enable-redis-httpsession[]
@EnableRedisHttpSession//(maxInactiveIntervalInSeconds = 60)
public class WebSecurityConfig