欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年01月19日 14:38:51   作者:  
這篇文章主要介紹了使用redis實現(xià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);
 }

 /**
  * 此方法可以因為拿不到鎖而導(dǎo)致operation沒有執(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沒有被執(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->過期->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()}沒有被執(zhí)行
  * 上層必須處理該異常,捕獲到該異常可以retry本次操作,或者包裝成{@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包的實現(xiàn)步驟

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

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

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

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

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

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

    教你用Java實現(xiàn)一個簡單的代碼生成器

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

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

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

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

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

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

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

    SpringBoot 集成 Druid過程解析

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

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

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

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

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

最新評論