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”,你會發現程序的控制檯仍然能夠正常打印隊列信息。