java redis使用之利用jedis實現redis訊息佇列

來源:互聯網
上載者:User

標籤:redis   隊列   緩衝   jedis使用   

應用情境

最近在公司做項目,需要對聊天內容進行儲存,考慮到資料庫查詢的IO串連數高、串連頻繁的因素,決定利用緩衝做。

從網上瞭解到redis可以對所有的內容進行二進位的儲存,而java是可以對所有對象進行序列化的,序列化的方法會在下面的代碼中提供實現。

序列化

這裡我編寫了一個java序列化的工具,主要是對對象轉換成byte[],和根據byte[]數組還原序列化成java對象;

主要是用到了ByteArrayOutputStream和ByteArrayInputStream;

需要注意的是每個自訂的需要序列化的對象都要實現Serializable介面;

其代碼如下:

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 {/**對象轉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[]轉對象 * @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();}}

定義一個訊息類,主要用於接收訊息內容和訊息下表的設定。

package com.bean;import java.io.Serializable;/**定義訊息類接收訊息內容和設定訊息的下標 * @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;}}

利用redis做隊列,我們採用的是redis中list的push和pop操作;

結合隊列的特點:

  • 只允許在一端插入
  • 新元素只能在隊列的尾部
  • FIFO:先進先出原則

redis中lpush(rpop)或rpush(lpop)可以滿足要求,而redis中list 裡要push或pop的對象僅需要轉換成byte[]即可

java採用Jedis進行redis的儲存和redis的串連池設定

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 JEDIS_SLAVE;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);jedisPool = new JedisPool(config, JEDIS_IP, JEDIS_PORT, 60000);}/** * 擷取資料 * @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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return value;}public static void close(Jedis jedis) {try {jedisPool.returnResource(jedis);} catch (Exception e) {if (jedis.isConnected()) {jedis.quit();jedis.disconnect();}}}/** * 擷取資料 *  * @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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}/** * 擷取資料 *  * @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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return value;}/** * 擷取資料 *  * @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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池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) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}/** * 儲存REDIS隊列 順序儲存 * @param byte[] key reids鍵名 * @param byte[] value 索引值 */public static void lpush(byte[] key, byte[] value) {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.lpush(key, value);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}/** * 儲存REDIS隊列 反向儲存 * @param byte[] key reids鍵名 * @param byte[] value 索引值 */public static void rpush(byte[] key, byte[] value) {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.rpush(key, value);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}/** * 將列表 source 中的最後一個元素(尾元素)彈出,並返回給用戶端 * @param byte[] key reids鍵名 * @param byte[] value 索引值 */public static void rpoplpush(byte[] key, byte[] destination) {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.rpoplpush(key, destination);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}/** * 擷取隊列資料 * @param byte[] key 鍵名 * @return */public static List<byte[]> lpopList(byte[] key) {List<byte[]> list = null;Jedis jedis = null;try {jedis = jedisPool.getResource();list = jedis.lrange(key, 0, -1);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return list;}/** * 擷取隊列資料 * @param byte[] key 鍵名 * @return */public static byte[] rpop(byte[] key) {byte[] bytes = null;Jedis jedis = null;try {jedis = jedisPool.getResource();bytes = jedis.rpop(key);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return bytes;}public static void hmset(Object key, Map<String, String> hash) {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.hmset(key.toString(), hash);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}public static void hmset(Object key, Map<String, String> hash, int time) {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.hmset(key.toString(), hash);jedis.expire(key.toString(), time);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}public static List<String> hmget(Object key, String... fields) {List<String> result = null;Jedis jedis = null;try {jedis = jedisPool.getResource();result = jedis.hmget(key.toString(), fields);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return result;}public static Set<String> hkeys(String key) {Set<String> result = null;Jedis jedis = null;try {jedis = jedisPool.getResource();result = jedis.hkeys(key);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return result;}public static List<byte[]> lrange(byte[] key, int from, int to) {List<byte[]> result = null;Jedis jedis = null;try {jedis = jedisPool.getResource();result = jedis.lrange(key, from, to);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return result;}public static Map<byte[], byte[]> hgetAll(byte[] key) {Map<byte[], byte[]> result = null;Jedis jedis = null;try {jedis = jedisPool.getResource();result = jedis.hgetAll(key);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return result;}public static void del(byte[] key) {Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.del(key);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}}public static long llen(byte[] key) {long len = 0;Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.llen(key);} catch (Exception e) {//釋放redis對象jedisPool.returnBrokenResource(jedis);e.printStackTrace();} finally {//返還到串連池close(jedis);}return len;}}

Configuration主要用於讀取redis配置資訊

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) {}}}


測試redis隊列

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, "內容1");JedisUtil.lpush(redisKey, ObjectUtil.objectToBytes(msg1));Message msg2 = new Message(2, "內容2");JedisUtil.lpush(redisKey, ObjectUtil.objectToBytes(msg2));Message msg3 = new Message(3, "內容3");JedisUtil.lpush(redisKey, ObjectUtil.objectToBytes(msg3));}}測試結果如下:
1   內容1
2   內容2
3   內容3

java redis使用之利用jedis實現redis訊息佇列

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.