Spring-data-redis: Distributed queue

Source: Internet
Author: User
Tags connection pooling redis sleep thread xmlns
Spring-data-redis: Distributed queueReturn Script Blackstone

The list data structure in Redis, with the characteristics of "double-ended queue", and the ability of Redis to have persistent data, is very safe and reliable for Redis to implement distributed queues. It is similar to "Queue" in JMS, except that functionality and reliability (transactional) are not strictly JMS.

When a queue in Redis is blocked, the entire connection cannot continue with other operations, so it is important to be aware of the connection pooling design.

We implemented the "Sync queue" through Spring-data-redis, with a design style similar to JMS.

A. configuration file:

<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=" ></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>

two. Program Examples:

1) Queuelistener: You can perform a JMS-like callback operation when there is data in the queue.

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: queue operation, internal encapsulation redistemplate instance, if "listener" is configured, queue will be executed as "message callback", Listenerthread is a background thread, used to automatically process "queue information". If you do not configure "listener", you can inject redisqueue into other spring beans and manually "take" the data.

public class Redisqueue<t> implements initializingbean,disposablebean{private Redistemplate redistemplate;
	Private String key;
	private int cap = short.max_value;//maximum blocked capacity, exceeding capacity will result in emptying old data private byte[] rawkey;
	Private Redisconnectionfactory Factory; Private redisconnection connection;//for blocking private boundlistoperations<string, t> listoperations;// Noblocking Private Lock lock = new Reentrantlock ();//based on underlying IO blocking consider private Redisqueuelistener listener;//asynchronous callback 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 Timeo
		UT) 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);
	}/** * from the head of the queue, insert */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.
						Executes an if (value! = null) One by one {try{listener.onmessage (value);
 }catch (Exception e) {//}}}}catch (Interruptedexception e) {//}}}}

3) Use and test:

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 ();
}

While the program is running, you can perform "Lpush" through the REDIS-CLI (client window), and you will find that the program's console still prints the queue information normally.




More articles:Wuhan Enterprise catalogue jmeter pressure test tools--the work of environmental construction of the Little Foot (original) "Cultivation 12" when the carrot + stick failure, we can more effectively motivate employees. Swtableview (Cctableview) Moves a list item to a specified index the memory growth strategy for the cell STL string that corresponds to an online learning programming site collection about using Texturepacker to package pictures, the run-time sprite picture turns red and has strange text Step by step implementation of their own framework series (III): Implementation of client-side communication injection framework Roboguice

<iframe id= "Iframeu1254640_0" src= "http://pos.baidu.com/acom?rdid=1254640&amp;dc=2&amp;di=u1254640 &amp;dri=0&amp;dis=0&amp;dai=1&amp;ps=5913x8&amp;dcb=baidu_exp_union_define&amp;dtm= Baidu_dup_setjsonadslot&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=88&amp;tpr= 1452072691177&amp;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&amp;ari=1&amp;dbv=2&amp;drs=1&amp;pcs=1337x719&amp;pss =1337x5922&amp;cfv=0&amp;cpl=4&amp;chi=1&amp;cce=true&amp;cec=gbk&amp;tlm=1425257122 &amp;ltu=http%3a%2f%2fwww.csdn123.com%2fhtml%2fblogs%2f20130616%2f22846.htm&amp;ltr=https%3a%2f% 2fwww.baidu.com%2flink%3furl% 3deaahmnkcnrpdmi0z7ivauhgu2tw729bgoslf1u7kwtrfnerjg8thxfibahggxgszeccqnsdom4etfqrwaefqna%26wd%3d%26eqid% 3db6daff4d0001185700000003568cdeef&amp;ecd=1&amp;psr=1600x900&amp;par=1538x900&amp;pis=-1x-1 &amp;ccd=24&amp;cja=true&amp;cmi=6&amp;col=zh-cn&amp;cdo=-1&amp;tcn=1452072691&amp;exps= 110211&amp;qn=06472862a48c85e3&amp;tt=1452072691088.92.113.115&amp;feid=110211 "width=" 960 "height=" "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; " ></iframe>

Baidu statistics <iframe scrolling= "no" frameborder= "0" allowtransparency= "true" align= "Center,center" src= "/http pos.baidu.com/ecom?adn=32&amp;at=231&amp;aurl=&amp;cad=1&amp;ccd=24&amp;cec=gbk&amp; cfv=19&amp;ch=0&amp;col=zh-cn&amp;conbw=0&amp;conop=1&amp;cpa=1&amp;dai=1&amp;dis= 0&amp;hn=4&amp;ltr=https%3a%2f%2fwww.baidu.com%2flink%3furl% 3deaahmnkcnrpdmi0z7ivauhgu2tw729bgoslf1u7kwtrfnerjg8thxfibahggxgszeccqnsdom4etfqrwaefqna%26wd%3d%26eqid% 3db6daff4d0001185700000003568cdeef&amp;ltu=http%3a%2f%2fwww.csdn123.com%2fhtml%2fblogs%2f20130616% 2f22846.htm&amp;lunum=6&amp;n=94007089_cpr&amp;pcs=1337x719&amp;pis=10000x10000&amp;ps= 6053x8&amp;psr=1600x900&amp;pss=1337x6054&amp;qn=ac064af064d6cb58&amp;rad=&amp;rsi0=960 &amp;rsi1=75&amp;rsi5=4&amp;rss0=%23FFFFFF&amp;rss1=%23FFFFFF&amp;rss2=%230000ff&amp; rss3=&amp;rss4=&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=&amp;slidelu_abs_pos=bottom&amp;slidelu_in_per=1.0&amp;slidelu_out_per=0.0 &amp;slideLU_rel_pos=left&amp;stid=14&amp;td_id=1656710&amp;titFF=%E5%AE%8B%E4%BD%93&amp; Titfs=12&amp;titta=left&amp;tn=baidutlinkinlay&amp;tpr=1452072691177&amp;ts=1&amp;wn=9 &amp;xuanting=0&amp;cpro_id=u1656710_0&amp;dtm=baidu_dup2_setjsonadslot&amp;dc=2&amp;di= u1656710&amp;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&amp;tt=1452072691265.11.29.62 "style=" width:960px; height:75px; " ></iframe> Recommended for you

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.