xinyb_
2021-09-24 17c1055ac108e6bef6c2b515c58b2f9ecea0da73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/redis 
            http://www.springframework.org/schema/redis/spring-redis.xsd"
       xmlns:redis="http://www.springframework.org/schema/redis">
 
    <!--引入配置文件-->
    <context:property-placeholder location="classpath:/redis.properties" ignore-unresolvable="true"></context:property-placeholder>
 
    <!--redis链接密码-->
    <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
        <constructor-arg name="thePassword" value="${redis.password}"></constructor-arg>
    </bean>
 
    <!--spring-data-redis2.0以上的配置-->
    <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
        <property name="hostName" value="${redis.hostname}"/>
        <property name="port" value="${redis.port}"/>
        <property name="password" ref="redisPassword" />
        <property name="database" value="${redis.database}"/>
    </bean>
    <!--配置jedis链接工厂 spring-data-redis2.0中
        建议改为构造器传入一个RedisStandaloneConfiguration  单机
                            RedisSentinelConfiguration  主从复制
                            RedisClusterConfiguration  集群-->
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--注销掉的部分为spring-data-redis2.0以下的版本配置的方式-->
        <!--  <property name="hostName" value="${redis.hostname}"/>
          <property name="port" value="${redis.port}"/>
          <property name="poolConfig" ref="jedisPoolConfig"/>
          <property name="password" value="${redis.password}" />
          <property name="database" value="${redis.database}"/>-->
        <!--spring-data-redis2.0以上建议获取的方式-->
        <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
    </bean>
 
    <!--手动设置 key  与 value的序列化方式-->
    <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
 
    <!--配置jedis模板  -->
    <bean id = "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />
    </bean>
 
    <!--也可以StringRedisTemplate  专注于String的操作  -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <!--<property name="connectionFactory" ref="jedisConnectionFactory"></property>-->
        <!--在StringRedisTemplate与redisTemplate不同,可以直接造构造器中传入ConnectionFactory-->
        <constructor-arg name="connectionFactory" ref="redisConnectionFactory"></constructor-arg>
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />
    </bean>
 
    <!-- web socket 发送订阅消息  -->
    <bean id="redisDAO" class="com.yc.im.service.RedisDAOImpl">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>
 
    <!-- AI雷达监听订阅消息,并触发事件 -->
    <bean id="listenerForWebSocket" class="com.yc.im.api.messagelistener.WebSocketMessageListener">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
    <bean id="listenerForDataSource" class="com.yc.multiData.DataSourceMessageListener">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
    <bean id="listenerForImageCache" class="com.yc.sdk.shopping.filter.ClearImageCacheMessageListener">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
    <!-- 系统消息监听订阅消息,并触发事件 -->
    <bean id="listenerForMessages" class="com.yc.sdk.WebSocketMessage.api.messagelistener.WebSocketMessageListener">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
     <!-- 监听二维码订阅,并触发事件-->
    <bean id="listenerForQrCodeSession" class="com.yc.phoneQRLogin.listener.WebSocketqrCodeListener">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
    <!-- 监听二维码过期,并触发事件 -->
    <bean id="listenerForQrCodeInvalid" class="com.yc.phoneQRLogin.listener.WebSocketqrCodeInvalidListener">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
 
    <!-- the default ConnectionFactory -->
    <bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
 
    <redis:listener-container >
        <!-- the method attribute can be skipped as the default method name is "onMessage" -->
        <redis:listener  ref="listenerForWebSocket" serializer="jdkSerializer" topic="Onbus_WSMQ" />
        <redis:listener  ref="listenerForDataSource" serializer="jdkSerializer" topic="Onbus_DataSource" />
        <redis:listener  ref="listenerForImageCache" serializer="jdkSerializer" topic="Onbus_ClearImageCache" />
        <redis:listener  ref="listenerForMessages" serializer="jdkSerializer" topic="Onbus_Messages" />
        <redis:listener  ref="listenerForQrCodeSession" serializer="jdkSerializer" topic="Onbus_Qrcode" />
        <redis:listener  ref="listenerForQrCodeInvalid" serializer="jdkSerializer" topic="__keyevent@*__:expired"/>
    </redis:listener-container>
 
 
 
    <!--设置jedisPool链接池的配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--最大连接数 -->
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <!--最大空闲连接数 -->
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <!--每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
        <!--释放连接的扫描间隔(单位:毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
        <!--连接最小空闲时间(单位:毫秒) -->
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
        <!--连接空闲多久后释放,当空闲时间大于该值并且空闲连接大于最大空闲连接时直接释放连接 -->
        <property name="softMinEvictableIdleTimeMillis" value="${redis.softMinEvictableIdleTimeMillis}"/>
        <!--获取连接时最大等待毫秒数,如果该值小于0,则阻塞不确定的时长,默认值-1 -->
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <!--在获取连接时检查连接有效性,默认为false -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <!--在连接空闲时检查连接有效性,默认为false -->
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
        <!--连接耗尽是否阻塞,false代表抛异常,true代表阻塞直到超时,默认为true -->
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>
    </bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1" value="${redis.hostname}" />
        <constructor-arg index="2" value="${redis.port}" type="int" />
        <constructor-arg index="3" value="${redis.timeout}" type="int" />
        <constructor-arg index="4" value="${redis.password}" />
    </bean>
 
</beans>