Exemplo n.º 1
0
 /*
  * (non-Javadoc)
  * @see com.lmiky.admin.lock.service.LockService#unlock(java.lang.String, java.lang.Long)
  */
 public void unlock(String lockTarget, Long userId) throws LockException {
   synchronized (mutex) {
     try {
       String key = CACHE_HEAD + lockTarget;
       LockData lockData = (LockData) cache.get(key);
       if (lockData != null) {
         if (lockData.getUserId().equals(userId)) {
           cache.remove(key);
         }
       }
     } catch (CacheException e) {
       throw new LockException(e.getMessage());
     }
   }
 }
Exemplo n.º 2
0
 /* (non-Javadoc)
  * @see com.lmiky.admin.lock.service.LockService#isLockByUser(java.lang.String, java.lang.Long)
  */
 public boolean isLockByUser(String lockTarget, Long userId) throws LockException {
   synchronized (mutex) {
     try {
       String key = CACHE_HEAD + lockTarget;
       LockData lockData = (LockData) cache.get(key);
       if (lockData == null) { // 未被加锁
         return false;
       } else if (!lockData.getUserId().equals(userId)) {
         return false;
       }
       lockData.setLockTime(System.currentTimeMillis());
       return true;
     } catch (CacheException e) {
       throw new LockException(e.getMessage());
     }
   }
 }
Exemplo n.º 3
0
 /*
  * (non-Javadoc)
  * @see com.lmiky.admin.lock.service.LockService#lock(java.lang.String, java.lang.Long, java.lang.String)
  */
 public void lock(String lockTarget, Long userId, String userName) throws LockException {
   synchronized (mutex) {
     try {
       String cacheKey = CACHE_HEAD + lockTarget;
       LockData lockData = (LockData) cache.get(cacheKey);
       if (lockData == null) { // 未被加锁
         lockData = new LockData(userId, userName, System.currentTimeMillis());
         cache.put(cacheKey, lockData);
       } else if (!lockData.getUserId().equals(userId)
           && System.currentTimeMillis() - lockData.getLockTime() < maxLockMillis) { // 已经加锁,并且还未超时
         throw new LockException("locked by " + lockData.getUserName(), lockData.getUserName());
       } else { // 被自己加锁或者加锁超时
         lockData.setUserId(userId);
         lockData.setUserName(userName);
         lockData.setLockTime(System.currentTimeMillis());
       }
     } catch (CacheException e) {
       throw new LockException(e.getMessage());
     }
   }
 }