本文整理汇总了Java中com.hazelcast.core.ILock类的典型用法代码示例。如果您正苦于以下问题:Java ILock类的具体用法?Java ILock怎么用?Java ILock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ILock类属于com.hazelcast.core包,在下文中一共展示了ILock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: disposeLock
import com.hazelcast.core.ILock; //导入依赖的package包/类
public void disposeLock(Lock lock) {
switch (Settings.INSTANCE.clusteringMode()) {
case HAZELCAST:
((ILock) lock).destroy();
break;
case IGNITE:
break;
case SINGLE:
break;
default:
break;
}
}
示例2: synchronizePut
import com.hazelcast.core.ILock; //导入依赖的package包/类
public Object synchronizePut(Object key, Object value, String map) {
if(isRunning()){
ILock lock = hazelcast.getLock(map);
try
{
if(lock.tryLock(10, TimeUnit.SECONDS))
{
return hazelcast.getMap(map).put(key, value);
}
else
{
log.warn("[synchronizePut] Operation did not synchroznize in 10 secs");
return hazelcast.getMap(map).put(key, value);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.debug("", e);
}
finally
{
lock.unlock();
}
}
return null;
}
示例3: doRunInitialization
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected void doRunInitialization(final String beanName) {
LockKey lockKey = new LockKey(KeyType.INITIALIZATION, beanName);
// Try to get the initialization lock
ILock lock = hazelcastInstance.getLock(lockKey.toString());
if (lock.tryLock()) {
// No one else is trying to run this initialization right now. Check if it was already ran by someone else
if (!initializationControl.containsKey(beanName)) {
try {
// This initialization was never executed. Run it and mark it as executed
super.doRunInitialization(beanName);
initializationControl.put(beanName, beanName);
} finally {
HazelcastHelper.release(lock);
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Not running initialization for bean " + beanName + " because some other node is currently running it");
}
}
}
示例4: doRunPollingTask
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected boolean doRunPollingTask(final String key, final Callable<Boolean> task) {
LockKey lockKey = new LockKey(KeyType.POLLING_TASK, key);
ILock lock = hazelcastInstance.getLock(lockKey.toString());
// Ensure multiple nodes can't run a polling task simultaneously
if (lock.tryLock()) {
try {
return super.doRunPollingTask(key, task);
} finally {
HazelcastHelper.release(lock);
}
} else {
// Force a sleep, as couldn't get the lock for this polling task
if (LOG.isDebugEnabled()) {
LOG.debug("Some other cluster node is running the " + key + " polling task. Leaving.");
}
return false;
}
}
示例5: acquire
import com.hazelcast.core.ILock; //导入依赖的package包/类
private void acquire(final LockKey key) {
if (acquiredLocks.containsKey(key)) {
// Already own the lock
return;
}
ILock lock = hazelcastInstance.getLock(key.toString());
try {
if (lock.tryLock(timeoutSeconds, TimeUnit.SECONDS)) {
acquiredLocks.put(key, lock);
} else {
throw new LockingException();
}
} catch (InterruptedException e) {
throw new LockingException(e);
}
}
示例6: testLockUnlock
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Test
public void testLockUnlock() throws InterruptedException {
HazelcastClient hClient = getHazelcastClient();
final ILock lock = hClient.getLock("testLockUnlock");
lock.lock();
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch unlockLatch = new CountDownLatch(1);
new Thread(new Runnable() {
public void run() {
assertFalse(lock.tryLock());
unlockLatch.countDown();
lock.lock();
latch.countDown();
}
}).start();
assertTrue(unlockLatch.await(10, TimeUnit.SECONDS));
lock.unlock();
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
示例7: testTryLock
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Test
public void testTryLock() throws InterruptedException {
HazelcastClient hClient = getHazelcastClient();
final ILock lock = hClient.getLock("testTryLock");
assertTrue(lock.tryLock());
lock.lock();
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch unlockLatch = new CountDownLatch(1);
new Thread(new Runnable() {
public void run() {
assertFalse(lock.tryLock());
unlockLatch.countDown();
try {
assertTrue(lock.tryLock(10, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
latch.countDown();
}
}).start();
assertTrue(unlockLatch.await(10, TimeUnit.SECONDS));
lock.unlock();
lock.unlock();
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
示例8: acquireDistributedLock
import com.hazelcast.core.ILock; //导入依赖的package包/类
protected com.hazelcast.core.ILock acquireDistributedLock(Object object) {
if (object == null) {
if (log.isWarnEnabled()) {
log.warn("Could not acquire distributed lock, object is null");
}
return null;
}
if (log.isDebugEnabled()) {
log.debug(String.format("Acquiring distributed lock for %s...", object.getClass().getSimpleName()));
}
ILock lock = getHazelcastInstance().getLock(object);
if (log.isDebugEnabled()) {
log.debug(String.format("Distributed lock acquired for %s", object.getClass().getSimpleName()));
}
return lock;
}
示例9: releaseDistributedLock
import com.hazelcast.core.ILock; //导入依赖的package包/类
protected void releaseDistributedLock(ILock lock) {
if (lock == null) {
if (log.isWarnEnabled()) {
log.warn("Could not release distributed lock, lock is null");
}
return;
}
if (log.isDebugEnabled()) {
log.debug(String.format("Releasing distributed lock for %s...", lock.getKey()));
}
lock.forceUnlock();
if (log.isDebugEnabled()) {
log.debug(String.format("Distributed lock released for %s", lock.getKey()));
}
}
示例10: verify
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Verify(global = true)
public void verify() {
for (int i = 0; i < maxAccounts; i++) {
ILock lock = targetInstance.getLock(name + i);
assertFalse(name + ": Lock should be unlocked", lock.isLocked());
}
long totalValue = 0;
IList<Long> accounts = targetInstance.getList(name);
for (long value : accounts) {
totalValue += value;
}
logger.info(": totalValue=" + totalValue);
assertEquals(name + ": totalInitialValue != totalValue ", totalInitialValue, totalValue);
Counter total = new Counter();
IList<Counter> totals = targetInstance.getList(name + "count");
for (Counter count : totals) {
total.add(count);
}
logger.info("total count " + total);
}
示例11: timeStep
import com.hazelcast.core.ILock; //导入依赖的package包/类
@TimeStep
public void timeStep(BaseThreadState state) {
int lockIndex = state.randomInt(lockCount);
ILock lock = targetInstance.getLock(name + lockIndex);
int leaseTime = 1 + state.randomInt(maxLeaseTimeMillis);
int tryTime = 1 + state.randomInt(maxTryTimeMillis);
if (state.randomBoolean()) {
lock.lock(leaseTime, MILLISECONDS);
} else {
try {
lock.tryLock(tryTime, MILLISECONDS, leaseTime, MILLISECONDS);
} catch (InterruptedException e) {
logger.info("tryLock() got exception: " + e.getMessage());
}
}
}
示例12: verify
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Verify
public void verify() {
for (int i = 0; i < lockCount; i++) {
ILock lock = targetInstance.getLock(name + i);
boolean isLocked = lock.isLocked();
long remainingLeaseTime = lock.getRemainingLeaseTime();
if (isLocked) {
String message = format("%s is locked with remainingLeaseTime: %d ms", lock, remainingLeaseTime);
if (allowZeroMillisRemainingLeaseLockTime && remainingLeaseTime == 0) {
logger.warn(message);
} else {
fail(message);
}
}
if (remainingLeaseTime > 0) {
fail(format("%s has remainingLeaseTime: %d ms", lock, remainingLeaseTime));
}
}
}
示例13: doRunInitialization
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected void doRunInitialization(final String beanName) {
LockKey lockKey = new LockKey(KeyType.INITIALIZATION, beanName);
// Try to get the initialization lock
ILock lock = hazelcastInstance.getLock(lockKey);
if (lock.tryLock()) {
// No one else is trying to run this initialization right now. Check if it was already ran by someone else
if (!initializationControl.containsKey(beanName)) {
try {
// This initialization was never executed. Run it and mark it as executed
super.doRunInitialization(beanName);
initializationControl.put(beanName, beanName);
} finally {
HazelcastHelper.release(lock);
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Not running initialization for bean " + beanName + " because some other node is currently running it");
}
}
}
示例14: doRunPollingTask
import com.hazelcast.core.ILock; //导入依赖的package包/类
@Override
protected boolean doRunPollingTask(final String key, final Callable<Boolean> task) {
LockKey lockKey = new LockKey(KeyType.POLLING_TASK, key);
ILock lock = hazelcastInstance.getLock(lockKey);
// Ensure multiple nodes can't run a polling task simultaneously
if (lock.tryLock()) {
try {
return super.doRunPollingTask(key, task);
} finally {
HazelcastHelper.release(lock);
}
} else {
// Force a sleep, as couldn't get the lock for this polling task
if (LOG.isDebugEnabled()) {
LOG.debug("Some other cluster node is running the " + key + " polling task. Leaving.");
}
return false;
}
}
示例15: acquire
import com.hazelcast.core.ILock; //导入依赖的package包/类
private void acquire(final LockKey key) {
if (acquiredLocks.containsKey(key)) {
// Already own the lock
return;
}
ILock lock = hazelcastInstance.getLock(key);
try {
if (lock.tryLock(timeoutSeconds, TimeUnit.SECONDS)) {
acquiredLocks.put(key, lock);
} else {
throw new LockingException();
}
} catch (InterruptedException e) {
throw new LockingException(e);
}
}