All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.terracotta.entity.EntityLockHandler Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
package com.terracotta.entity;

import org.terracotta.toolkit.Toolkit;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * EntityLockHandler
 */
public class EntityLockHandler {

  private final ExecutorService executorService;
  private final Toolkit toolkit;

  EntityLockHandler(Toolkit toolkit) {
    this.toolkit = toolkit;
    executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, "clustered-entity-locking-thread");
            thread.setDaemon(true);
            return thread;
        }
    });
  }

  public void readLock(final String lockName) {
    try {
      executorService.submit(new Callable() {
        @Override
        public Void call() throws Exception {
          toolkit.getReadWriteLock(lockName).readLock().lock();
          return null;
        }
      }).get();
    } catch (Exception e) {
      throw new IllegalStateException("Unable to acquire read lock for lock " + lockName, e);
    }
  }

  public void readUnlock(final String lockName) {
    try {
      executorService.submit(new Callable() {
        @Override
        public Void call() throws Exception {
          toolkit.getReadWriteLock(lockName).readLock().unlock();
          return null;
        }
      }).get();
    } catch (Exception e) {
      throw new IllegalStateException("Unable to release read lock for lock " + lockName, e);
    }
  }

  public void dispose() {
    executorService.shutdownNow();
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy