Using java redis to implement redis Message Queue using jedis

Source: Internet
Author: User

Using java redis to implement redis Message Queue using jedis
Application scenarios

Recently, I am working on a project in the company and need to store chat content. Considering the high I/O connections and frequent connections in the database, I decided to use the cache.

I learned from the Internet that redis can store binary data for all content, while java can serialize all objects. The serialization method will be provided in the following code.

Serialization

Here I have compiled a java serialization tool that converts an object to byte [] and deserializes it into a java object based on the byte [] array;

ByteArrayOutputStream and ByteArrayInputStream are mainly used;

Note that each custom object to be serialized must implement the Serializable interface;

The Code is as follows:

Package com. bean. util; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. objectInputStream; import java. io. objectOutputStream; public class ObjectUtil {/** Object to byte [] * @ param obj * @ return * @ throws IOException */public static byte [] objectToBytes (Object obj) throws Exception {ByteArrayOutputStream bo = new ByteArrayOutputStream (); ObjectOutputStream oo = new ObjectOutputStream (bo); oo. writeObject (obj); byte [] bytes = bo. toByteArray (); bo. close (); oo. close (); return bytes;}/** byte [] convert Object * @ param bytes * @ return * @ throws Exception */public static Object bytesToObject (byte [] bytes) throws Exception {ByteArrayInputStream in = new ByteArrayInputStream (bytes); ObjectInputStream sIn = new ObjectInputStream (in); return sIn. readObject ();}}

Defines a message class, which is mainly used to receive the message content and set the following message table.

Package com. bean; import java. io. serializable;/** define the content of the Message received by the Message class and set the subscript of the Message * @ author lenovo **/public class Message implements Serializable {private static final long serialVersionUID = 7792729L; private int id; private String content; public int getId () {return id;} public void setId (int id) {this. id = id;} public String getContent () {return content;} public void setContent (String content) {this. content = content ;}}

Using redis for queue, we adopt the list push and pop operations in redis;

Combined with the characteristics of the queue:

Only new elements can be inserted at one end. Only FIFO is allowed at the end of the queue.

In redis, lpush (rpop) or rpush (lpop) can meet the requirements. In redis, the objects to push or pop in list must be converted to byte [].

Java uses Jedis for redis storage and redis connection pool settings

Package com. redis. util; import java. util. list; import java. util. map; import java. util. set; import redis. clients. jedis. jedis; import redis. clients. jedis. jedisPool; import redis. clients. jedis. jedisPoolConfig; public class JedisUtil {private static String JEDIS_IP; private static int JEDIS_PORT; private static String JEDIS_PASSWORD; // private static String secret; private static JedisPool jedisPool; static {Configuration conf = Configuration. getInstance (); JEDIS_IP = conf. getString ("jedis. ip "," 127.0.0.1 "); JEDIS_PORT = conf. getInt ("jedis. port ", 6379); JEDIS_PASSWORD = conf. getString ("jedis. password ", null); JedisPoolConfig config = new JedisPoolConfig (); config. setMaxActive (5000); config. setMaxIdle (256); // 20config. setMaxWait (5000L); config. setTestOnBorrow (true); config. setTestOnReturn (true); config. setTestWhileIdle (true); config. setMinEvictableIdleTimeMillis (60000l); config. setTimeBetweenEvictionRunsMillis (3000l); config. setNumTestsPerEvictionRun (-1); glasispool = new glasispool (config, glasis_ip, glasis_port, 60000 );} /*** get data ** @ param key * @ return */public static String get (String key) {String value = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); value = jedis. get (key) ;}catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return value;} public static void close (Jedis jedis) {try {jedisPool. returnResource (jedis);} catch (Exception e) {if (jedis. isConnected () {jedis. quit (); jedis. disconnect () ;}}/ *** get data ** @ param key * @ return */public static byte [] get (byte [] key) {byte [] value = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); value = jedis. get (key) ;}catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return value;} public static void set (byte [] key, byte [] value) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. set (key, value);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} public static void set (byte [] key, byte [] value, int time) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. set (key, value); jedis. expire (key, time);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} public static void hset (byte [] key, byte [] field, byte [] value) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. hset (key, field, value);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace () ;}finally {// return to connection pool close (jedis) ;}} public static void hset (String key, String field, String value) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. hset (key, field, value);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis );}} /*** get data ** @ param key * @ return */public static String hget (String key, String field) {String value = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); value = jedis. hget (key, field);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return value ;} /*** get data ** @ param key * @ return */public static byte [] hget (byte [] key, byte [] field) {byte [] value = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); value = jedis. hget (key, field);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return value;} public static void hdel (byte [] key, byte [] field) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. hdel (key, field);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis );}} /*** storage REDIS queue ordered storage * @ param byte [] key reids key name * @ param byte [] value key value */public static void lpush (byte [] key, byte [] value) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. lpush (key, value);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis );}} /*** store REDIS queue reverse storage * @ param byte [] key reids key name * @ param byte [] value key value */public static void rpush (byte [] key, byte [] value) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. rpush (key, value);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace () ;}finally {// return to the connection pool close (jedis) ;}}/*** to bring up the last element (tail element) in the list source, return to the client * @ param byte [] key reids key name * @ param byte [] value key value */public static void rpoplpush (byte [] key, byte [] destination) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. rpoplpush (key, destination);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis );}} /*** get queue data ** @ param byte [] key name * @ return */public static List
 
  
LpopList (byte [] key) {List
  
   
List = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); list = jedis. lrange (key, 0,-1);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return list ;} /*** get queue data ** @ param byte [] key name * @ return */public static byte [] rpop (byte [] key) {byte [] bytes = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); bytes = jedis. rpop (key);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return bytes;} public static void hmset (Object key, Map
   
    
Hash) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. hmset (key. toString (), hash);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} public static void hmset (Object key, Map
    
     
Hash, int time) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. hmset (key. toString (), hash); jedis. expire (key. toString (), time);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} public static List
     
      
Hmet (Object key, String... fields) {List
      
        Result = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); result = jedis. hmet (key. toString (), fields);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return result;} public static Set
       
         Hkeys (String key) {Set
        
          Result = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); result = jedis. hkeys (key);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return result;} public static List
         
           Lrange (byte [] key, int from, int to) {List
          
            Result = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); result = jedis. lrange (key, from, to);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return result;} public static Map
           
             HgetAll (byte [] key) {Map
            
              Result = null; Jedis jedis = null; try {jedis = jedisPool. getResource (); result = jedis. hgetAll (key);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace ();} finally {// return to connection pool close (jedis);} return result;} public static void del (byte [] key) {Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. del (key);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace () ;}finally {// return to connection pool close (jedis) ;}} public static long llen (byte [] key) {long len = 0; Jedis jedis = null; try {jedis = jedisPool. getResource (); jedis. llen (key);} catch (Exception e) {// release the redis object jedisPool. returnBrokenResource (jedis); e. printStackTrace () ;}finally {// return to connection pool close (jedis) ;}return len ;}}
            
           
          
         
        
       
      
     
    
   
  
 

Configuration is mainly used to read redis Configuration information

package com.redis.util;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class Configuration extends Properties {private static final long serialVersionUID = 50440463580273222L;private static Configuration instance = null;public static synchronized Configuration getInstance() {if (instance == null) {instance = new Configuration();}return instance;}public String getProperty(String key, String defaultValue) {String val = getProperty(key);return (val == null || val.isEmpty()) ? defaultValue : val;}public String getString(String name, String defaultValue) {return this.getProperty(name, defaultValue);}public int getInt(String name, int defaultValue) {String val = this.getProperty(name);return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);}public long getLong(String name, long defaultValue) {String val = this.getProperty(name);return (val == null || val.isEmpty()) ? defaultValue : Integer.parseInt(val);}public float getFloat(String name, float defaultValue) {String val = this.getProperty(name);return (val == null || val.isEmpty()) ? defaultValue : Float.parseFloat(val);}public double getDouble(String name, double defaultValue) {String val = this.getProperty(name);return (val == null || val.isEmpty()) ? defaultValue : Double.parseDouble(val);}public byte getByte(String name, byte defaultValue) {String val = this.getProperty(name);return (val == null || val.isEmpty()) ? defaultValue : Byte.parseByte(val);}public Configuration() {InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("config.xml");try {this.loadFromXML(in);in.close();} catch (IOException e) {}}}


Test redis queue

Package com. quene. test; import com. bean. message; import com. bean. util. objectUtil; import com. redis. util. jedisUtil; public class TestRedisQuene {public static byte [] redisKey = "key ". getBytes (); static {init ();} public static void main (String [] args) {pop ();} private static void pop () {byte [] bytes = JedisUtil. rpop (redisKey); Message msg = (Message) ObjectUtil. bytesToObject (bytes); if (msg! = Null) {System. out. println (msg. getId () + "" + msg. getContent () ;}} private static void init () {Message msg1 = new Message (1, "content 1"); JedisUtil. lpush (redisKey, ObjectUtil. objectToBytes (msg1); Message msg2 = new Message (2, "content 2"); JedisUtil. lpush (redisKey, ObjectUtil. objectToBytes (msg2); Message msg3 = new Message (3, "content 3"); JedisUtil. lpush (redisKey, ObjectUtil. objectToBytes (msg3);} the test result is as follows:
1 content 1
2 content 2
3 content 3

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.