项目中使用redis发布订阅实现了动态修改配置控制消费线程数。
1 redis配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Redis 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="20"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="5"/>
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
</bean>
<!-- redis sentinel 配置 -->
<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<!-- cluster name -->
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster" />
</bean>
</property>
<!-- sentinel信息 -->
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg index="0" value="172.17.27.27"/>
<constructor-arg index="1" value="26379"/>
</bean>
</set>
</property>
</bean>
<!-- 连接工厂配置 -->
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<!-- 使用的db编号-->
<property name="database" value="0" />
<!-- redis sentinel配置 -->
<constructor-arg ref="redisSentinelConfiguration"/>
<!-- jedis 连接池配置 -->
<constructor-arg ref="jedisPoolConfig" />
</bean>
<!-- 数据模板配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<bean id="topicContainer"
class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnFactory" />
<property name="taskExecutor">
<bean
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="3"></property>
</bean>
</property>
<property name="messageListeners">
<map>
<entry key-ref="threadNumRedisNessageListener">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="threadNumTopicName" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="threadNumRedisNessageListener" class="com.bestpay.messagecenter.product.core.oss.ThreadNumRedisNessageListener"/>
<bean id="threadNumRedisPublish" class="com.bestpay.messagecenter.product.core.oss.ThreadNumRedisPublish">
<property name="threadNumTopicName" value="threadNumTopicName"/>
</bean>
</beans>
2.监听类
package com.bestpay.messagecenter.product.core.oss;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import com.bestpay.messagecenter.product.core.model.oss.ConfigurationBO;
import lombok.extern.slf4j.Slf4j;
/**
* 接收修改线程数的监听类
* @author linxing
* @version Id: ThreadNumRedisNessageListener.java, v 0.1 2017/7/3 9:43 linxing Exp $$
*/
@Slf4j
public class ThreadNumRedisNessageListener implements MessageListener {
@Autowired
private RedisTemplate<Serializable, Serializable> redisTemplate;
@Override
public void onMessage(Message message, byte[] bytes) {
byte[] body = message.getBody();// 请使用valueSerializer
byte[] channel = message.getChannel();
ConfigurationBO configurationBO = (ConfigurationBO) redisTemplate.getValueSerializer()
.deserialize(body);
log.debug("关键词 在:{} 接收消息:{}", new String(channel), configurationBO);
//TODO
}
}
3.发送消息
package com.bestpay.messagecenter.product.core.oss;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import lombok.extern.slf4j.Slf4j;
/**
* @author linxing
* @version Id: ThreadNumRedisPublish.java, v 0.1 2017/7/3 10:04 linxing Exp $$
*/
@Slf4j
public class ThreadNumRedisPublish {
@Autowired
private RedisTemplate<Serializable, Serializable> redisTemplate;
private String threadNumTopicName;
public void sendMessage(Serializable message) {
log.debug("关键词 在{},发送消息:{}", threadNumTopicName, message);
redisTemplate.convertAndSend(threadNumTopicName, message);
}
/**
* 设置 threadNumTopicName
* return
*/
public void setThreadNumTopicName(String threadNumTopicName) {
this.threadNumTopicName = threadNumTopicName;
}
}