EmbeddedRedisConfig.java 3.4 KB

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