EmbeddedRedisConfig.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package sample.config;
  2. /*
  3. * Copyright 2002-2014 the original author or authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. import java.io.IOException;
  18. import java.net.ServerSocket;
  19. import org.springframework.beans.BeansException;
  20. import org.springframework.beans.factory.DisposableBean;
  21. import org.springframework.beans.factory.InitializingBean;
  22. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  23. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  24. import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
  25. import org.springframework.context.annotation.Bean;
  26. import org.springframework.context.annotation.Configuration;
  27. import redis.embedded.RedisServer;
  28. /**
  29. * Runs an embedded Redis instance. This is only necessary since we do not want
  30. * users to have to setup a Redis instance. In a production environment, this
  31. * would not be used since a Redis Server would be setup.
  32. *
  33. * @author Rob Winch
  34. */
  35. @Configuration
  36. public class EmbeddedRedisConfig {
  37. @Bean
  38. public static RedisServerBean redisServer() {
  39. return new RedisServerBean();
  40. }
  41. /**
  42. * Implements BeanDefinitionRegistryPostProcessor to ensure this Bean
  43. * is initialized before any other Beans. Specifically, we want to ensure
  44. * that the Redis Server is started before RedisHttpSessionConfiguration
  45. * attempts to enable Keyspace notifications.
  46. */
  47. static class RedisServerBean implements InitializingBean, DisposableBean, BeanDefinitionRegistryPostProcessor, RedisConnectionProperties {
  48. private RedisServer redisServer;
  49. public void afterPropertiesSet() throws Exception {
  50. redisServer = new RedisServer(getPort());
  51. redisServer.start();
  52. }
  53. public void destroy() throws Exception {
  54. if(redisServer != null) {
  55. redisServer.stop();
  56. }
  57. }
  58. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {}
  59. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
  60. private Integer availablePort;
  61. public int getPort() throws IOException {
  62. if(availablePort == null) {
  63. ServerSocket socket = new ServerSocket(0);
  64. availablePort = socket.getLocalPort();
  65. socket.close();
  66. }
  67. return availablePort;
  68. }
  69. }
  70. }