Redis's configuration in spring and Java code implementation

Source: Internet
Author: User
Tags redis server

1. Build a Redis.properties Property file

# Redis settingredis.addr = 127.0.0.1redis.port = 6379redis.auth = Masterredis.maxidle = 200redis.maxactive = 1024redis.ma xwait = 10000redis.timeout = 10000redis.testonborrow = True
2, create a new Redisutil.java file, the file code is as follows:
PackageCn.com.taiji.sample.utils;Importjava.io.Serializable;ImportJava.util.ArrayList;ImportJava.util.List;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;ImportRedis.clients.jedis.JedisShardInfo;ImportRedis.clients.jedis.ShardedJedis;ImportRedis.clients.jedis.ShardedJedisPool;PublicClass RedisutilImplementsserializable{PrivateStaticFinalLong Serialversionuid = -1149678082569464779l;//Redis Server IPPrivateStaticString addr;//The port number of the RedisPrivateStaticIntPort//Access passwordPrivateStaticString Auth;//The maximum number of available connection instances, with a default value of 8;//If the assignment is-1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time.PrivateStaticIntmaxactive;//Controls the maximum number of Jedis instances in a pool that have an idle (idle) state, and the default value is 8.PrivateStaticIntMaxidle;//The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that never times out. If the waiting time is exceeded, the jedisconnectionexception is thrown directly;PrivateStaticIntmaxwait;PrivateStaticIntTimeOut;//Whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available;PrivateStaticBooleanTestonborrow;Publicstatic Jedis Jedis;//Non-slice client connectionsPublicStatic Jedispool Jedispool;//Non-tiled connection poolPublicStatic Shardedjedis Shardedjedis;//Slice Amount Client ConnectionPublicStatic Shardedjedispool Shardedjedispool;//Slice Connection Pool/*** Initialize non-tile pool*/PrivateStaticvoidInitialpool () {//Pool basic Configuration Jedispoolconfig config =NewJedispoolconfig (); Config.setmaxtotal (maxactive); Config.setmaxidle (Maxidle); Config.setmaxwaitmillis (maxwait); Config.settestonborrow (Testonborrow); Jedispool =NewJedispool (config, addr, port); }/*** Initialize the tile pool*/PrivateStaticvoidInitialshardedpool () {//Pool basic Configuration Jedispoolconfig config =NewJedispoolconfig (); Config.setmaxtotal (maxactive); Config.setmaxidle (Maxidle); Config.setmaxwaitmillis (maxwait); Config.settestonborrow (Testonborrow);//Slave link list<jedisshardinfo> shards =New arraylist<jedisshardinfo>(); Shards.add (NewJedisshardinfo (addr, port, auth));//Construction Pool Shardedjedispool =NewShardedjedispool (config, shards); }PublicStaticvoid Afterpropertiesset ()ThrowsException {//TODO auto-generated Method StubInitialpool (); Initialshardedpool ();Try{Shardedjedis =Shardedjedispool.getresource (); }Catch(Exception e) {System.out.println ("Connection Shardedjedispool failed!")); }Try{Jedis =Jedispool.getresource (); }Catch(Exception e) {System.out.println ("Connection Jedispool failed!")); } }PublicString getaddr () {ReturnAddr }PublicvoidSetaddr (String addr) {THIS.ADDR =Addr }PublicIntGetport () {ReturnPort }Publicvoid Setport (IntPort) {This.port =Port }PublicString GetAuth () {ReturnAuth }PublicvoidSetauth (String auth) {This.auth =Auth }PublicIntGetmaxactive () {Returnmaxactive; }Publicvoid Setmaxactive (Intmaxactive) {This.maxactive = maxactive;} public int Getmaxidle () { Span style= "COLOR: #0000ff" >return Maxidle;} public void setmaxidle ( Int Maxidle) {this.maxidle =  Maxidle;} public int getmaxwait () {return maxwait;} public void setmaxwait (int maxwait) {this.maxwait = maxwait;} public int gettimeout () {return timeOut,} public void SetTimeOut (int TimeOut) {this.timeout = timeout;} public boolean Istestonborrow () {return testonborrow;} public void Settestonborrow (Boolean Testonborrow) {this.testonborrow = Testonborrow;}}            

In the Set method, it will float yellow, without affecting the use.

3. In the Spring-servlet.xml configuration file, add the following code :

<Context:component-scanBase-package= "Cn.com.taiji.sample.config.web"/><Context:property-placeholderLocation= "file:#{systemproperties[' webapp.sample ']}/web-inf/conf/redis.properties"/><BeanId= "Redisutil"Class= "Cn.com.taiji.sample.utils.RedisUtil"><PropertyName= "Addr"><Value&GT;${REDIS.ADDR}</Value></Property><PropertyName= "Port"><Value>${redis.port}</Value></Property><PropertyName= "Auth"><Value>${redis.auth}</Value></Property><PropertyName= "Maxidle"><Value>${redis.maxidle}</Value></Property><PropertyName= "Maxactive"><Value>${redis.maxactive}</Value></Property><PropertyName= "Maxwait"><Value>${redis.maxwait}</Value></Property><PropertyName= "TimeOut"><value>${redis.timeout} </value></< Span style= "COLOR: #800000" >property> <property name= "Testonborrow" ><value>${ Redis.testonborrow}</value ></property> " Span style= "COLOR: #0000ff" ></bean>   
<location= ""/> refers to the redis.properties file path.
The spring configuration for Redis is now complete.

Redis's configuration in spring and Java code implementation

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.