Spring-data-redis: 分布式队列

Spring-data-redis: 分布式队列

返回脚本百事通

Redis中list数据结构,具有“双端队列”的特性,同时redis具有持久数据的能力,因此redis实现分布式队列是非常安全可靠的。它类似于JMS中的“Queue”,只不过功能和可靠性(事务性)并没有JMS严格。

Redis中的队列阻塞时,整个connection都无法继续进行其他操作,因此在基于连接池设计是需要注意。

我们通过spring-data-redis,来实现“同步队列”,设计风格类似与JMS。

一.配置文件:

<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" default-autowire="byName">
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxActive" value="32"></property>
		<property name="maxIdle" value="6"></property>
		<property name="maxWait" value="15000"></property>
		<property name="minEvictableIdleTimeMillis" value="300000"></property>
		<property name="numTestsPerEvictionRun" value="3"></property>
		<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
		<property name="whenExhaustedAction" value="1"></property>
	</bean>
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
		<property name="poolConfig" ref="jedisPoolConfig"></property>
		<property name="hostName" value="127.0.0.1"></property>
		<property name="port" value="6379"></property>
		<property name="password" value="0123456"></property>
		<property name="timeout" value="15000"></property>
		<property name="usePool" value="true"></property>
	</bean>
	<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory"></property>
		<property name="defaultSerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
		</property>
	</bean>
	<bean id="jedisQueueListener" class="com.sample.redis.sdr.QueueListener"/>
	<bean id="jedisQueue" class="com.sample.redis.sdr.RedisQueue" destroy-method="destroy">
		<property name="redisTemplate" ref="jedisTemplate"></property>
		<property name="key" value="user:queue"></property>
		<property name="listener" ref="jedisQueueListener"></property>
	</bean>
</beans>

二.程序实例:

1) QueueListener:当队列中有数据时,可以执行类似于JMS的回调操作。

public interface RedisQueueListener<T> {

	public void onMessage(T value);
}
public class QueueListener<String> implements RedisQueueListener<String> {

	@Override
	public void onMessage(String value) {
		System.out.println(value);
		
	}

}

2) RedisQueue:队列操作,内部封装redisTemplate实例;如果配置了“listener”,那么queue将采用“消息回调”的方式执行,listenerThread是一个后台线程,用来自动处理“队列信息”。如果不配置“listener”,那么你可以将redisQueue注入到其他spring bean中,手动去“take”数据即可。

public class RedisQueue<T> implements InitializingBean,DisposableBean{
	private RedisTemplate redisTemplate;
	private String key;
	private int cap = Short.MAX_VALUE;//最大阻塞的容量,超过容量将会导致清空旧数据
	private byte[] rawKey;
	private RedisConnectionFactory factory;
	private RedisConnection connection;//for blocking
	private BoundListOperations<String, T> listOperations;//noblocking
	
	private Lock lock = new ReentrantLock();//基于底层IO阻塞考虑
	
	private RedisQueueListener listener;//异步回调
	private Thread listenerThread;
	
	private boolean isClosed;
	
	public void setRedisTemplate(RedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public void setListener(RedisQueueListener listener) {
		this.listener = listener;
	}

	public void setKey(String key) {
		this.key = key;
	}
	

	@Override
	public void afterPropertiesSet() throws Exception {
		factory = redisTemplate.getConnectionFactory();
		connection = RedisConnectionUtils.getConnection(factory);
		rawKey = redisTemplate.getKeySerializer().serialize(key);
		listOperations = redisTemplate.boundListOps(key);
		if(listener != null){
			listenerThread = new ListenerThread();
			listenerThread.setDaemon(true);
			listenerThread.start();
		}
	}
	
	
	/**
	 * blocking
	 * remove and get last item from queue:BRPOP
	 * @return
	 */
	public T takeFromTail(int timeout) throws InterruptedException{ 
		lock.lockInterruptibly();
		try{
			List<byte[]> results = connection.bRPop(timeout, rawKey);
			if(CollectionUtils.isEmpty(results)){
				return null;
			}
			return (T)redisTemplate.getValueSerializer().deserialize(results.get(1));
		}finally{
			lock.unlock();
		}
	}
	
	public T takeFromTail() throws InterruptedException{
		return takeFromHead(0);
	}
	
	/**
	 * 从队列的头,插入
	 */
	public void pushFromHead(T value){
		listOperations.leftPush(value);
	}
	
	public void pushFromTail(T value){
		listOperations.rightPush(value);
	}
	
	/**
	 * noblocking
	 * @return null if no item in queue
	 */
	public T removeFromHead(){
		return listOperations.leftPop();
	}
	
	public T removeFromTail(){
		return listOperations.rightPop();
	}
	
	/**
	 * blocking
	 * remove and get first item from queue:BLPOP
	 * @return
	 */
	public T takeFromHead(int timeout) throws InterruptedException{
		lock.lockInterruptibly();
		try{
			List<byte[]> results = connection.bLPop(timeout, rawKey);
			if(CollectionUtils.isEmpty(results)){
				return null;
			}
			return (T)redisTemplate.getValueSerializer().deserialize(results.get(1));
		}finally{
			lock.unlock();
		}
	}
	
	public T takeFromHead() throws InterruptedException{
		return takeFromHead(0);
	}

	@Override
	public void destroy() throws Exception {
		if(isClosed){
			return;
		}
		shutdown();
		RedisConnectionUtils.releaseConnection(connection, factory);
	}
	
	private void shutdown(){
		try{
			listenerThread.interrupt();
		}catch(Exception e){
			//
		}
	}
	
	class ListenerThread extends Thread {
		
		@Override
		public void run(){
			try{
				while(true){
					T value = takeFromHead();//cast exceptionyou should check.
					//逐个执行
					if(value != null){
						try{
							listener.onMessage(value);
						}catch(Exception e){
							//
						}
					}
				}
			}catch(InterruptedException e){
				//
			}
		}
	}
	
}

3) 使用与测试:

public static void main(String[] args) throws Exception{
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-redis-beans.xml");
	RedisQueue<String> redisQueue = (RedisQueue)context.getBean("jedisQueue");
	redisQueue.pushFromHead("test:app");
	Thread.sleep(15000);
	redisQueue.pushFromHead("test:app");
	Thread.sleep(15000);
	redisQueue.destroy();
}

在程序运行期间,你可以通过redis-cli(客户端窗口)执行“lpush”,你会发现程序的控制台仍然能够正常打印队列信息。




更多文章:



id="iframeu1254640_0" src="http://pos.baidu.com/acom?rdid=1254640&dc=2&di=u1254640&dri=0&dis=0&dai=1&ps=5913x8&dcb=BAIDU_EXP_UNION_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=88&tpr=1452072691177&ti=Spring-data-redis%3A%20%E5%88%86%E5%B8%83%E5%BC%8F%E9%98%9F%E5%88%97%20-%20%E8%84%9A%E6%9C%AC%E7%99%BE%E4%BA%8B%E9%80%9A&ari=1&dbv=2&drs=1&pcs=1337x719&pss=1337x5922&cfv=0&cpl=4&chi=1&cce=true&cec=GBK&tlm=1425257122&ltu=http%3A%2F%2Fwww.csdn123.com%2Fhtml%2Fblogs%2F20130616%2F22846.htm&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DeaAHmNKcNRPDMi0Z7iVAuHgU2Tw729bGOsLF1u7KwtRFNERJg8tHxfIBAHgGxgSzECcQNsDOm4eTFqrWAEFqNa%26wd%3D%26eqid%3Db6daff4d0001185700000003568cdeef&ecd=1&psr=1600x900&par=1538x900&pis=-1x-1&ccd=24&cja=true&cmi=6&col=zh-CN&cdo=-1&tcn=1452072691&exps=110211&qn=06472862a48c85e3&tt=1452072691088.92.113.115&feid=110211" width="960" height="90" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">

百度统计

scrolling="no" frameborder="0" allowtransparency="true" align="center,center" src="http://pos.baidu.com/ecom?adn=32&at=231&aurl=&cad=1&ccd=24&cec=GBK&cfv=19&ch=0&col=zh-CN&conBW=0&conOP=1&cpa=1&dai=1&dis=0&hn=4&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DeaAHmNKcNRPDMi0Z7iVAuHgU2Tw729bGOsLF1u7KwtRFNERJg8tHxfIBAHgGxgSzECcQNsDOm4eTFqrWAEFqNa%26wd%3D%26eqid%3Db6daff4d0001185700000003568cdeef&ltu=http%3A%2F%2Fwww.csdn123.com%2Fhtml%2Fblogs%2F20130616%2F22846.htm&lunum=6&n=94007089_cpr&pcs=1337x719&pis=10000x10000&ps=6053x8&psr=1600x900&pss=1337x6054&qn=ac064af064d6cb58&rad=&rsi0=960&rsi1=75&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000ff&rss3=&rss4=&rss5=&rss6=%23e10900&rss7=&scale=&skin=&slideLU_abs_pos=bottom&slideLU_in_per=1.0&slideLU_out_per=0.0&slideLU_rel_pos=left&stid=14&td_id=1656710&titFF=%E5%AE%8B%E4%BD%93&titFS=12&titTA=left&tn=baiduTlinkInlay&tpr=1452072691177&ts=1&wn=9&xuanting=0&cpro_id=u1656710_0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1656710&ti=Spring-data-redis%3A%20%E5%88%86%E5%B8%83%E5%BC%8F%E9%98%9F%E5%88%97%20-%20%E8%84%9A%E6%9C%AC%E7%99%BE%E4%BA%8B%E9%80%9A&tt=1452072691265.11.29.62" style="width: 960px; height: 75px;">
为您推荐
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值