RedisConfig.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2013-2019 Xia Jun(3979434@qq.com).
  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. ***************************************************************************************
  17. * *
  18. * Website : http://www.farsunset.com *
  19. * *
  20. ***************************************************************************************
  21. */
  22. package com.fdkk.config;
  23. import com.fdkk.constants.Constants;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.boot.autoconfigure.AutoConfigureBefore;
  27. import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.context.annotation.Configuration;
  30. import org.springframework.data.redis.connection.MessageListener;
  31. import org.springframework.data.redis.connection.RedisConnectionFactory;
  32. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  33. import org.springframework.data.redis.listener.ChannelTopic;
  34. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  35. import java.util.Objects;
  36. @Configuration
  37. @AutoConfigureBefore(RedisAutoConfiguration.class)
  38. public class RedisConfig {
  39. @Autowired
  40. public RedisConfig(LettuceConnectionFactory connectionFactory, @Value("${spring.profiles.active}") String profile){
  41. if (Objects.equals("dev",profile)){
  42. connectionFactory.setValidateConnection(true);
  43. }
  44. }
  45. @Bean
  46. public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,
  47. MessageListener pushMessageListener,
  48. MessageListener bindMessageListener){
  49. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  50. container.setConnectionFactory(connectionFactory);
  51. container.addMessageListener(pushMessageListener,new ChannelTopic(Constants.PUSH_MESSAGE_INNER_QUEUE));
  52. container.addMessageListener(bindMessageListener,new ChannelTopic(Constants.BIND_MESSAGE_INNER_QUEUE));
  53. return container;
  54. }
  55. }