亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

redis實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)同步工具代碼分享

 更新時(shí)間:2014年01月19日 14:38:51   作者:  
這篇文章主要介紹了使用redis實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)同步工具的代碼,大家參考使用吧

復(fù)制代碼 代碼如下:

package com.happyelements.odin.util;

import static com.google.common.base.Preconditions.checkNotNull;

import org.jetbrains.annotations.NotNull;

import com.happyelements.odin.jedis.JedisClient;
import com.happyelements.rdcenter.commons.util.DateTimeUtil;

/**
 * User: jude.wang
 * Date: 14-1-16
 * Time: 上午9:43
 */
public class ConcurrentUtil {

 public static final String USER_LOCK_KEY_FORMAT = "user_lock_%d_%s";
 public static final String CUSTOM_LOCK_FORMAT = "custom_lock_%s";
 public static final JedisClient redisClient = JedisClient.getInstance();
 public static final String UNLOCKED = "0";
 public static final String LOCKED = "1";
 private static final int MAX_REPEAT_TIMES = 10;

 @NotNull
 public static String buildUserLockKey(long userId, @NotNull String key) {
  checkNotNull(key);
  return String.format(USER_LOCK_KEY_FORMAT, userId, key);
 }

 @NotNull
 public static String buildCustomLockKey(@NotNull String key) {
  checkNotNull(key);
  return String.format(CUSTOM_LOCK_FORMAT, key);
 }

 /**
  * 此方法可以因?yàn)槟貌坏芥i而導(dǎo)致operation沒(méi)有執(zhí)行
  *
  * @param key
  * @see com.happyelements.odin.util.ConcurrentUtil#buildCustomLockKey(String)
  * @see com.happyelements.odin.util.ConcurrentUtil#buildUserLockKey(long, String)
  *
  * @param operation
  * @throws com.happyelements.odin.util.ConcurrentUtil.OperationNotExecException
  *             operation沒(méi)有被執(zhí)行
  */
 public static void doJobWithLock(@NotNull String key, @NotNull ILockOperation operation) throws OperationNotExecException {

  boolean locked = false;
  try {
   checkNotNull(key);
   checkNotNull(operation);
   locked = lock(key);

  } catch (Throwable t) {
   throw new OperationNotExecException(key, t);
  }

  try {
   if (locked) {
    // System.out.println(Thread.currentThread() + "\t" + "lock");
    operation.doJob();
   } else {
    throw new OperationNotExecException(key);
   }
  } finally {
   if (locked) {
    unlock(key);
   }
  }
 }

 private static void unlock(String key) {
  try {
   checkNotNull(key);
   String oldStatus = redisClient.getSet(key, UNLOCKED);
   if (isUnlocked(oldStatus)) {
    // lock->doJob->過(guò)期->unlock
    // TODO LOG
   }
  } catch (Throwable t) {
   // TODO LOG
  }
  // System.out.println(Thread.currentThread() + "\t" + "unlock");
 }

 private static boolean isUnlocked(String status) {
  return status == null || status.equals(UNLOCKED);
 }

 private static boolean lock(String key) {

  boolean locked = false;

  for (int i = 0; i < MAX_REPEAT_TIMES; i++) {
   String oldStatus = redisClient.getSet(key, LOCKED);

   if (isUnlocked(oldStatus)) {
    if (oldStatus == null) {
     redisClient.expire(key, DateTimeUtil.MINUTE_SECOND * 5);
     locked = true;
     break;
    }
    locked = true;
    break;
   }
  }

  return locked;
 }

 public static interface ILockOperation {
  void doJob();
 }

 /**
  * {@link com.happyelements.odin.util.ConcurrentUtil.ILockOperation#doJob()}沒(méi)有被執(zhí)行
  * 上層必須處理該異常,捕獲到該異??梢詒etry本次操作,或者包裝成{@link com.happyelements.rdcenter.commons.throwable.HeException} 拋出去
  */
 public static class OperationNotExecException extends Exception {
  public OperationNotExecException() {
  }

  public OperationNotExecException(String s) {
   super(s);
  }

  public OperationNotExecException(String s, Throwable throwable) {
   super(s, throwable);
  }

  public OperationNotExecException(Throwable throwable) {
   super(throwable);
  }
 }
}

相關(guān)文章

  • Maven導(dǎo)入本地jar包的實(shí)現(xiàn)步驟

    Maven導(dǎo)入本地jar包的實(shí)現(xiàn)步驟

    本文主要介紹了Maven導(dǎo)入本地jar包的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • SpringMVC自定義日期轉(zhuǎn)換器方式

    SpringMVC自定義日期轉(zhuǎn)換器方式

    這篇文章主要介紹了SpringMVC如何自定義日期轉(zhuǎn)換器問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java判斷線程池線程是否執(zhí)行完畢

    Java判斷線程池線程是否執(zhí)行完畢

    這篇文章主要介紹了Java判斷線程池線程是否執(zhí)行完畢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 教你用Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的代碼生成器

    教你用Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的代碼生成器

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著如何用Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的代碼生成器展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法

    mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法

    這篇文章主要介紹了mybatis 使用jdbc.properties文件設(shè)置不起作用的解決方法,需要的朋友可以參考下
    2018-03-03
  • Maven構(gòu)建忽略測(cè)試失敗的解決方案

    Maven構(gòu)建忽略測(cè)試失敗的解決方案

    這篇文章主要介紹了Maven構(gòu)建忽略測(cè)試失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot3.0+SpringSecurity6.0+JWT的實(shí)現(xiàn)

    SpringBoot3.0+SpringSecurity6.0+JWT的實(shí)現(xiàn)

    本文主要介紹了SpringBoot3.0+SpringSecurity6.0+JWT的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • SpringBoot 集成 Druid過(guò)程解析

    SpringBoot 集成 Druid過(guò)程解析

    這篇文章主要介紹了SpringBoot 集成 Druid過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java深入探索單例模式的應(yīng)用

    Java深入探索單例模式的應(yīng)用

    單例模式(Singleton Pattern)是 Java 中最簡(jiǎn)單的設(shè)計(jì)模式之一。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對(duì)象的最佳方式
    2022-06-06
  • 使用Java如何將文本復(fù)制到剪貼板

    使用Java如何將文本復(fù)制到剪貼板

    這篇文章主要介紹了使用Java如何將文本復(fù)制到剪貼板問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評(píng)論