Using connection pooling
1 Public classTest {2 3 /**4 * Redis Address5 */6 Private Static FinalString ADDR = "10.124.133.184";7 8 /**9 * Redis PortTen */ One Private Static FinalInteger PORT = 6379; A - /** - * Redis access password the */ - Private Static FinalString AUTH = "icloud20180514160728"; - - /** + * Maximum number of available connection instances, default value is 8 - * If the assignment is-1, it means no limit, and if the pool has been assigned maxactive Jedis instance, the pool's status is exhausted (exhausted) + */ A Private Static FinalInteger max_active = 1024; at - /** - * Control how many Jedis instances of a pool have an idle (idle) state, and the default value is 8. - */ - Private Static FinalInteger Max_idle = 200; - in /** - * The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means never time out. If the waiting time is exceeded, the jedisconnectionexception is thrown directly; to */ + Private Static intMax_wait = 10000; - the Private Static FinalInteger TIMEOUT = 10000; * $ /**Panax Notoginseng * Whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available - */ the Private Static FinalBoolean Test_on_borrow =true; + A Private StaticJedispool Jedispool =NULL; the + - /** $ * Initializing Redis connection pool $ */ - Static { - Try { theJedispoolconfig config =Newjedispoolconfig (); - Config.setmaxidle (max_idle);Wuyi Config.settestonborrow (test_on_borrow); theJedispool =Newjedispool (config, ADDR, PORT, TIMEOUT, AUTH); -}Catch(Exception e) { Wu e.printstacktrace (); - } About } $ - /** - * Get Jedis instances - * @return A */ + Private Static synchronizedJedis Getjedis () { the Try { - if(Jedispool! =NULL) { $ returnJedispool.getresource (); the}Else { the return NULL; the } the}Catch(Exception e) { - e.printstacktrace (); in return NULL; the } the } About Public Static voidMain (string[] args) { the theJedis Jedis =NULL; theJedis =Getjedis (); + if(NULL!=Jedis) { -list<string> configlist = Jedis.configget ("*"); themap<string, string> confmap =NewHashmap<string, string>();BayiInteger Step = 2; the for(inti = 0; I < configlist.size (); i = i +Step) { theString paramname =Configlist.get (i); -String paramvalue = configlist.get (i + 1); - confmap.put (paramname, paramvalue); the } the jedis.close (); the}Else{ theSystem.out.println ("Jedis is null"); - } the } the}
No connection pooling
Public Static voidMain (string[] args) {Jedis Jedis=NULL; Jedis=NewJedis (ADDR, PORT); Jedis.auth (auth); Map<string, string> confmap =NewHashmap<string, string>(); List<String> configlist = Jedis.configget ("*"); Integer Step= 2; for(inti = 0; I < configlist.size (); i = i +Step) {String paramname=Configlist.get (i); String paramvalue= Configlist.get (i + 1); Confmap.put (ParamName, paramvalue); } for(Map.entry entry:confMap.entrySet ()) {System.out.println (Entry.getkey () )+ ": " +Entry.getvalue ()); } //jedis.configset ("Hash-max-ziplist-entries", "n");System.out.println ("hash-max-ziplist-entries:" + jedis.configget ("Hash-max-ziplist-entries")); Jedis.close (); }
Java Operations Redis