Java Code Examples for com.hazelcast.core.IMap#lock()

The following examples show how to use com.hazelcast.core.IMap#lock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HazelcastTransactionManager.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
protected void lock(TransactionDefinition definition) {
    Instant now = Instant.now();
    String name = definition.getName();
    final IMap<String, HazelcastTransactionDefinition> store = getStore();
    try {
        store.lock(name);
        HazelcastTransactionDefinition current = store.get(name);
        if (current == null) {
            store.put(name, new HazelcastTransactionDefinition(definition));
        } else if (now.isAfter(current.getMost())) {
            store.put(name, new HazelcastTransactionDefinition(definition));
        } else {
            throw new TransactionLockException();
        }
    } finally {
        store.unlock(name);
    }
}
 
Example 2
Source File: HazelcastTransactionManager.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
protected void unlock(TransactionDefinition definition) {
    Instant now = Instant.now();
    String name = definition.getName();
    final IMap<String, HazelcastTransactionDefinition> store = getStore();
    try {
        store.lock(name);
        HazelcastTransactionDefinition current = store.get(name);
        if (current == null) {
            throw new TransactionUnlockException();
        } else if (now.isAfter(current.getMost())) {
            throw new TransactionUnlockException();
        } else {
            store.remove(name);
        }
    } finally {
        store.unlock(name);
    }
}
 
Example 3
Source File: SQLDBCacheMetadata.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void updateCacheItem(CacheItem cacheItem) {
	String hashedSignature = cacheItem.getSignature();

	IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE);
	mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter
	try {
		if (containsCacheItem(hashedSignature, true)) {
			cacheDao.updateCacheItem(cacheItem);
			logger.debug("The dataset with hash [" + hashedSignature + "] has been updated");
		} else {
			logger.debug("The dataset with hash [" + hashedSignature + "] does not exist in cache");
		}
	} finally {
		mapLocks.unlock(hashedSignature);
	}
}
 
Example 4
Source File: SQLDBCacheMetadata.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void removeCacheItem(String signature) {
	String hashedSignature = Helper.sha256(signature);

	IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE);
	mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter
	try {
		if (containsCacheItem(signature)) {
			cacheDao.deleteCacheItemBySignature(hashedSignature);
			logger.debug("The dataset with signature[" + signature + "] and hash [" + hashedSignature + "] has been updated");
		} else {
			logger.debug("The dataset with signature[" + signature + "] and hash [" + hashedSignature + "] does not exist in cache");
		}
	} finally {
		mapLocks.unlock(hashedSignature);
	}
}
 
Example 5
Source File: SQLDBCacheMetadata.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public void removeCacheItem(String signature, boolean isHash) {
	if (isHash) {
		IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME,
				SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE);
		mapLocks.lock(signature); // it is possible to use also the method tryLock(...) with timeout parameter
		try {
			if (containsCacheItem(signature, true)) {
				cacheDao.deleteCacheItemBySignature(signature);
				logger.debug("The dataset with hash [" + signature + "] has been deleted");
			} else {
				logger.debug("The dataset with hash [" + signature + "] does not exist in cache");
			}
		} finally {
			mapLocks.unlock(signature);
		}
	} else {
		removeCacheItem(signature);
	}
}
 
Example 6
Source File: SQLDBCacheMetadata.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public CacheItem getCacheItem(String resultSetSignature) {
	CacheItem cacheItem = null;

	String hashedSignature = Helper.sha256(resultSetSignature);

	IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE);
	mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter
	try {
		cacheItem = cacheDao.loadCacheItemBySignature(hashedSignature);
		if (cacheItem != null) {
			logger.debug("The dataset with signature[" + resultSetSignature + "] and hash [" + hashedSignature + "] has been found in cache");
		} else {
			logger.debug("The dataset with signature[" + resultSetSignature + "] and hash [" + hashedSignature + "] does not exist in cache");
		}
	} finally {
		mapLocks.unlock(hashedSignature);
	}
	return cacheItem;
}
 
Example 7
Source File: SQLDBCacheMetadata.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public CacheItem getCacheItem(String signature, boolean isHash) {
	if (isHash) {
		CacheItem cacheItem = null;
		IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME,
				SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE);
		mapLocks.lock(signature); // it is possible to use also the method tryLock(...) with timeout parameter
		try {
			cacheItem = cacheDao.loadCacheItemBySignature(signature);
			if (cacheItem != null) {
				logger.debug("The dataset with hash [" + signature + "] has been found in cache");
			} else {
				logger.debug("The dataset with hash [" + signature + "] does not exist in cache");
			}
		} finally {
			mapLocks.unlock(signature);
		}
		return cacheItem;
	} else {
		return getCacheItem(signature);
	}
}
 
Example 8
Source File: HazelcastLockProvider.java    From ShedLock with Apache License 2.0 6 votes vote down vote up
@Override
@NonNull
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    log.trace("lock - Attempt : {}", lockConfiguration);
    final Instant now = ClockProvider.now();
    final String lockName = lockConfiguration.getName();
    final IMap<String, HazelcastLock> store = getStore();
    try {
        // lock the map key entry
        store.lock(lockName, keyLockTime(lockConfiguration), TimeUnit.MILLISECONDS);
        // just one thread at a time, in the cluster, can run this code
        // each thread waits until the lock to be unlock
        if (tryLock(lockConfiguration, now)) {
            return Optional.of(new HazelcastSimpleLock(this, lockConfiguration));
        }
    } finally {
        // released the map lock for the others threads
        store.unlock(lockName);
    }
    return Optional.empty();
}
 
Example 9
Source File: SQLDBCacheMetadata.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void addCacheItem(String dataSetName, String resultsetSignature, Map<String, Object> properties, String tableName, BigDecimal dimension) {
	String hashedSignature = Helper.sha256(resultsetSignature);

	IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE);
	mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter
	try {
		removeCacheItem(resultsetSignature);

		CacheItem item = new CacheItem();
		item.setName(dataSetName);
		item.setTable(tableName);
		item.setSignature(hashedSignature);
		item.setDimension(dimension);
		Date now = new Date();
		item.setCreationDate(now);
		item.setLastUsedDate(now);
		item.setProperties(properties);
		cacheDao.insertCacheItem(item);

		logger.debug("Added cacheItem : [ Name: " + item.getName() + " \n Signature: " + item.getSignature() + " \n Dimension: " + item.getDimension()
				+ " bytes (approximately)  ]");
	} finally {
		mapLocks.unlock(hashedSignature);
	}
}