主要有以下四个步骤:
①java序列化的工具类,主要是将对象转化为byte数组,和根据byte数组反序列化成java对象
package com.cqh.RedisQuene; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * Created by yl1794 on 2018/3/28. */ //java序列化的工具类,主要是将对象转化为byte数组,和根据byte数组反序列化成java对象 //注意:每个需要序列化的对象都要实现Serializable接口 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(); } }
②工具类,构建redis连接池,以及一些队列的相关方法
package com.cqh.RedisQuene; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.List; /** * Created by yl1794 on 2018/3/28. */ //工具类,构建redis连接池,以及一些队列的相关方法 public class JedisUtil { private static JedisPool jedisPool; static { //也可以自行从配置文件来读取host、ip、password、maxIdle等信息 JedisPoolConfig config=new JedisPoolConfig(); config.setMaxTotal(5000); config.setMaxIdle(256); config.setMaxWaitMillis(5000L); config.setTestOnBorrow(true); config.setTestOnReturn(true); config.setTestWhileIdle(true); config.setMinEvictableIdleTimeMillis(60000L); config.setTimeBetweenEvictionRunsMillis(3000L); config.setNumTestsPerEvictionRun(-1); jedisPool = new JedisPool(config, "127.0.0.1", 6379,60000,null); } //获取jedis实例 public synchronized static Jedis getJedis(){ if(jedisPool!=null){ return jedisPool.getResource(); }else{ return null; } } //返还到连接池 private static void close(Jedis jedis) { try{ jedisPool.returnResource(jedis); }catch (Exception e){ if(jedis.isConnected()){ jedis.quit(); jedis.disconnect(); } } } /** * 存储REDIS队列 顺序存储 * @param key reids键名 * @param 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 key reids键名 * @param 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 key reids键名 * @param destination 键值 */ 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 key 键名 * @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) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } return list; } /** * 获取队列数据 * @param 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; } /** * 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 * @param srckey * @param dstkey * @param timout * @return */ public static byte[] brpoplpush(byte[] srckey,byte[] dstkey,int timout){ byte[] value = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); value = jedis.brpoplpush(srckey,dstkey,timout); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { close(jedis); } return value; } }
③消息类(实现Serializable接口)
package com.cqh.RedisQuene; import java.io.Serializable; /** * Created by yl1794 on 2018/3/28. */ //消息类(实现Serializable接口) public class Message implements Serializable{ private static final long serialVersionUID = 559867242872277374L; private int id; private String content; public Message(int id, String content) { this.id = id; this.content = 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; } }
④测试类
package com.cqh.RedisQuene; import redis.clients.jedis.Jedis; import java.io.IOException; /** * Created by yl1794 on 2018/3/28. */ //测试类 public class TestRedisQueue { public static byte[] redisKey = "key".getBytes(); public static byte[] dstkey = "dstkey".getBytes(); static { try { init(); } catch (IOException e) { e.printStackTrace(); } } private static void init() throws IOException { Jedis jedis = JedisUtil.getJedis(); for (int i = 0; i < 5; i++) { Message message = new Message(i, "这是第" + i + "个内容"); try { JedisUtil.lpush(redisKey, ObjectUtil.objectToBytes(message)); System.out.println(jedis.lrange(redisKey,0,-1)); } catch (Exception e) { e.printStackTrace(); } } } private static void rpop() throws Exception { byte[] bytes = JedisUtil.rpop(redisKey); Message msg = (Message) ObjectUtil.bytesToObject(bytes); if (msg != null) { System.out.println(msg.getId() + "----" + msg.getContent()); } } private static void brpoplpush() throws Exception{ Jedis jedis = JedisUtil.getJedis(); while (true){ try { byte[] bytes = JedisUtil.brpoplpush(redisKey, dstkey, 0); Message msg = (Message) ObjectUtil.bytesToObject(bytes); if(msg != null){ System.out.println(msg.getId() + "----" + msg.getContent()); } System.out.println(jedis.lrange(redisKey,0,-1)); System.out.println(jedis.lrange(dstkey,0,-1)); Thread.sleep(1000); } catch (InterruptedException e){ e.printStackTrace(); } } } public static void main(String[] args) { try { // rpop(); brpoplpush(); } catch (Exception e) { e.printStackTrace(); } } }
运行结果如下: