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&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=" "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&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; " ></iframe> Recommended for you