RedisConfig.java 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.genersoft.iot.vmp.conf.redis;
  2. import com.alibaba.fastjson.parser.ParserConfig;
  3. import com.genersoft.iot.vmp.common.VideoManagerConstants;
  4. import com.genersoft.iot.vmp.service.impl.*;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.cache.annotation.CachingConfigurerSupport;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.data.redis.connection.RedisConnectionFactory;
  10. import org.springframework.data.redis.core.RedisTemplate;
  11. import org.springframework.data.redis.listener.PatternTopic;
  12. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  13. import org.springframework.data.redis.serializer.StringRedisSerializer;
  14. import com.genersoft.iot.vmp.utils.redis.FastJsonRedisSerializer;
  15. /**
  16. * @description:Redis中间件配置类,使用spring-data-redis集成,自动从application.yml中加载redis配置
  17. * @author: swwheihei
  18. * @date: 2019年5月30日 上午10:58:25
  19. *
  20. */
  21. @Configuration
  22. public class RedisConfig extends CachingConfigurerSupport {
  23. @Autowired
  24. private RedisGpsMsgListener redisGPSMsgListener;
  25. @Autowired
  26. private RedisAlarmMsgListener redisAlarmMsgListener;
  27. @Autowired
  28. private RedisStreamMsgListener redisStreamMsgListener;
  29. @Autowired
  30. private RedisGbPlayMsgListener redisGbPlayMsgListener;
  31. @Autowired
  32. private RedisPushStreamStatusMsgListener redisPushStreamStatusMsgListener;
  33. @Autowired
  34. private RedisPushStreamStatusListMsgListener redisPushStreamListMsgListener;
  35. @Autowired
  36. private RedisPushStreamResponseListener redisPushStreamResponseListener;
  37. @Bean
  38. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  39. RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
  40. // 使用fastJson序列化
  41. FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
  42. // value值的序列化采用fastJsonRedisSerializer
  43. redisTemplate.setValueSerializer(fastJsonRedisSerializer);
  44. redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
  45. // 全局开启AutoType,不建议使用
  46. ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
  47. // key的序列化采用StringRedisSerializer
  48. redisTemplate.setKeySerializer(new StringRedisSerializer());
  49. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  50. redisTemplate.setConnectionFactory(redisConnectionFactory);
  51. return redisTemplate;
  52. }
  53. /**
  54. * redis消息监听器容器 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
  55. * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
  56. *
  57. * @param connectionFactory
  58. * @return
  59. */
  60. @Bean
  61. RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
  62. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  63. container.setConnectionFactory(connectionFactory);
  64. container.addMessageListener(redisGPSMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_GPS));
  65. container.addMessageListener(redisAlarmMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_SUBSCRIBE_ALARM_RECEIVE));
  66. container.addMessageListener(redisStreamMsgListener, new PatternTopic(VideoManagerConstants.WVP_MSG_STREAM_CHANGE_PREFIX + "PUSH"));
  67. container.addMessageListener(redisGbPlayMsgListener, new PatternTopic(RedisGbPlayMsgListener.WVP_PUSH_STREAM_KEY));
  68. container.addMessageListener(redisPushStreamStatusMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_PUSH_STREAM_STATUS_CHANGE));
  69. container.addMessageListener(redisPushStreamListMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_PUSH_STREAM_LIST_CHANGE));
  70. container.addMessageListener(redisPushStreamResponseListener, new PatternTopic(VideoManagerConstants.VM_MSG_STREAM_PUSH_RESPONSE));
  71. return container;
  72. }
  73. }