当前位置: 首页>>代码示例>>Java>>正文


Java AdvancedCache.lock方法代码示例

本文整理汇总了Java中org.infinispan.AdvancedCache.lock方法的典型用法代码示例。如果您正苦于以下问题:Java AdvancedCache.lock方法的具体用法?Java AdvancedCache.lock怎么用?Java AdvancedCache.lock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.infinispan.AdvancedCache的用法示例。


在下文中一共展示了AdvancedCache.lock方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testBatch

import org.infinispan.AdvancedCache; //导入方法依赖的package包/类
/**
 * Test {@link AdvancedCache#startBatch()} (to handle concurrent read / update).
 * We lock to keys (A and B) and asks for second nodes (using message) to confirm that these keys cannot be locked
 * concurrently.
 * After keys' release, confirm that second node can now lock these keys.
 * 
 * @throws IOException
 * @throws InterruptedException
 * @throws TimeoutException
 * @throws ExecutionException
 */
@Test
public void testBatch() throws IOException, InterruptedException, TimeoutException, ExecutionException {
	// start infinispan
	final int nodeNumber = 1;
	
	// start test instance
	final EmbeddedCacheManager cacheManager = new TestCacheManagerBuilder("node main", null).build();
	this.cacheManager = cacheManager;
	
	final Object monitor = new Object();
	final Map<Address, String> messages = Maps.newConcurrentMap();
	
	cacheManager.start();
	cacheManager.getCache(TestConstants.CACHE_MESSAGE).addListener(new NodeMessageListener<String>() {
		@Override
		public void onMessage(Message<String> value) {
			messages.put(value.getAddress(), value.getMessage());
			synchronized (monitor) {
				monitor.notify();
			}
		}
	});
	// initializes two keys A and B
	cacheManager.getCache(TestConstants.CACHE_DEFAULT).put("A", "A");
	cacheManager.getCache(TestConstants.CACHE_DEFAULT).put("B", "B");
	
	// start another node that register a task that waits a message to lock A and B
	prepareCluster(nodeNumber, LockTask.class);
	
	// startBatch
	AdvancedCache<Object, Object> cache = cacheManager.<Object, Object>getCache(TestConstants.CACHE_DEFAULT).getAdvancedCache();
	cache.startBatch();
	boolean successful = false;
	final Address address = cacheManager.getAddress();
	
	Callable<Boolean> test = new Callable<Boolean>() {
		@Override
		public Boolean call() throws Exception {
			return ! address.equals(cacheManager.<String, Message<String>>getCache(TestConstants.CACHE_MESSAGE).get(TestConstants.CACHE_KEY_MESSAGE_BUS).getAddress());
		}
	};
	
	try {
		// lock A and B
		cache.lock("A", "B");
		// send message -> second node cannot lock -> message TimeoutException
		cacheManager.getCache(TestConstants.CACHE_MESSAGE).put(TestConstants.CACHE_KEY_MESSAGE_BUS, Message.from(address, LockTask.TRY_LOCK));
		// must be set accordingly with lockAcquisitionTimeout
		waitForEvent(monitor, test, 25, TimeUnit.SECONDS);
		Assert.assertEquals(LockTask.TIMEOUT_EXCEPTION, cacheManager.<String, Message<String>>getCache(TestConstants.CACHE_MESSAGE).get(TestConstants.CACHE_KEY_MESSAGE_BUS).getMessage());
		successful = true;
	} finally {
		// stopBatch
		cache.endBatch(successful);
	}
	// second node can lock
	cacheManager.getCache(TestConstants.CACHE_MESSAGE).put(TestConstants.CACHE_KEY_MESSAGE_BUS, Message.from(address, LockTask.TRY_LOCK));
	// must be set accordingly with lockAcquisitionTimeout
	waitForEvent(monitor, test, 25, TimeUnit.SECONDS);
	Assert.assertEquals(LockTask.LOCK_SUCCESS, cacheManager.<String, Message<String>>getCache(TestConstants.CACHE_MESSAGE).get(TestConstants.CACHE_KEY_MESSAGE_BUS).getMessage());
}
 
开发者ID:openwide-java,项目名称:owsi-core-parent,代码行数:73,代码来源:TestTransaction.java

示例2: execute

import org.infinispan.AdvancedCache; //导入方法依赖的package包/类
@Override
public void execute() throws Exception
{
    AdvancedCache ac = (AdvancedCache)ca.getCache("SOURCE-CACHE");

    // maipulate the JTA transaction with begin/commit/rollback

    ac.lock(key);

    System.out.println("> ok");

    // maipulate the JTA transaction with begin/commit/rollback

}
 
开发者ID:NovaOrdis,项目名称:playground,代码行数:15,代码来源:Lock.java


注:本文中的org.infinispan.AdvancedCache.lock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。