设为首页 收藏本站
查看: 1135|回复: 0

[经验分享] 预热一下吧《实现Redis消息队列》

[复制链接]

尚未签到

发表于 2017-12-21 21:00:05 | 显示全部楼层 |阅读模式
/**  * @author ChinWay.Y
  * @time 2017-02-15
  */

  
public>  private static String JEDIS_IP;
  private static int JEDIS_PORT;
  private static String JEDIS_PASSWORD;
  private static JedisPool jedisPool;
  static {
  //Configuration自行写的配置文件解析类,继承自Properties
  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);
  config.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){
  jedisPool.returnBrokenResource(jedis);
  e.printStackTrace();
  }finally {
  close(jedis);
  }
  return value;
  }
  

  private static void close(Jedis jedis) {
  try{
  jedisPool.returnResource(jedis);
  }catch (Exception e){
  if(jedis.isConnected()){
  jedis.quit();
  jedis.disconnect();
  }
  }
  }
  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  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;
  }
  public static void hmset(Object key, Map 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 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 hmget(Object key, String... fields) {
  List 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 hkeys(String key) {
  Set 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 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) {
  //释放redis对象
  
            jedisPool.returnBrokenResource(jedis);
  e.printStackTrace();
  

  } finally {
  //返还到连接池
  
            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) {
  //释放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;
  }
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426622-1-1.html 上篇帖子: 简单实现Redis缓存中的排序功能 下篇帖子: Redis-07.Spring Data整合Jedis
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表