本文整理了Java中org.infinispan.Cache.addListener()
方法的一些代码示例,展示了Cache.addListener()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.addListener()
方法的具体详情如下:
包路径:org.infinispan.Cache
类名称:Cache
方法名:addListener
暂无
代码示例来源:origin: org.infinispan/infinispan-core
public void testAsyncNotification() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(2);
AbstractListener syncListener = new SyncListener(latch);
AbstractListener asyncListener = new AsyncListener(latch);
c.addListener(syncListener);
c.addListener(asyncListener);
c.put("k", "v");
latch.await();
assert syncListener.caller == Thread.currentThread();
assert asyncListener.caller != Thread.currentThread();
}
代码示例来源:origin: infinispan/infinispan-simple-tutorials
public static void main(String[] args) {
// Construct a simple local cache manager with default configuration
DefaultCacheManager cacheManager = new DefaultCacheManager();
// Define local cache configuration
cacheManager.defineConfiguration("local", new ConfigurationBuilder().build());
// Obtain the local cache
Cache<String, String> cache = cacheManager.getCache("local");
// Register a listener
cache.addListener(new MyListener());
// Store some values
cache.put("key1", "value1");
cache.put("key2", "value2");
cache.put("key1", "newValue");
// Stop the cache manager and release all resources
cacheManager.stop();
}
代码示例来源:origin: org.infinispan/infinispan-core
public void testActivatingAndPassivating() {
Cache c = cm.getCache("passivation");
TestListener l = new TestListener();
c.addListener(l);
assert l.loaded.isEmpty();
assert l.activated.isEmpty();
assert l.passivated.isEmpty();
c.put("k", "v");
assert l.loaded.isEmpty();
assert l.activated.isEmpty();
assert l.passivated.isEmpty();
c.evict("k");
assert l.loaded.isEmpty();
assert l.activated.isEmpty();
assert l.passivated.contains("k");
c.remove("k");
assert l.loaded.contains("k");
assert l.activated.contains("k");
assert l.passivated.contains("k");
// We should be fine if we evict a non existent key
c.evict("k");
}
代码示例来源:origin: org.infinispan/infinispan-core
public void testInsertEvent() {
Cache<Object, String> cache0 = cache();
ClusterListener clusterListener = new ClusterListener();
cache0.addListener(clusterListener);
cache.put(1, "v1");
verifySimpleInsertionEvents(clusterListener, 1, "v1");
}
代码示例来源:origin: org.infinispan/infinispan-core
protected void testFilter(Object keyToFilterOut, Object keyToUse, Long lifespan, CacheEventFilter<? super Object, ? super String> filter) {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
ClusterListener clusterListener = listener();
cache0.addListener(clusterListener, filter, null);
cache0.put(keyToFilterOut, FIRST_VALUE);
// We should not have gotten the message since it was filtered
assertEquals(clusterListener.events.size(), 0);
verifySimpleInsertion(cache0, keyToUse, FIRST_VALUE, lifespan, clusterListener, FIRST_VALUE);
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testRolledBackNotLocal() throws SystemException, NotSupportedException {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
Cache<Object, String> cache2 = cache(2, CACHE_NAME);
ClusterListener clusterListener = new ClusterListener();
cache0.addListener(clusterListener);
MagicKey key1 = new MagicKey(cache1, cache2);
MagicKey key2 = new MagicKey(cache2, cache1);
TransactionManager tm = cache2.getAdvancedCache().getTransactionManager();
tm.begin();
cache2.put(key1, FIRST_VALUE);
assertEquals(clusterListener.events.size(), 0);
cache2.put(key2, SECOND_VALUE);
assertEquals(clusterListener.events.size(), 0);
tm.rollback();
assertEquals(clusterListener.events.size(), 0);
}
代码示例来源:origin: org.infinispan/infinispan-core
public void testSameInvokingSequence() {
TxListener l0 = new TxListener(0);
cache(0).addListener(l0);
TxListener l1 = new TxListener(1);
cache(1).addListener(l1);
cache(0).put("k", "v");
assertEquals(l0.log, l1.log);
assertEquals(l0.log,Arrays.asList(TxEvent.STARTED, TxEvent.CREATED, TxEvent.COMPLETED));
}
代码示例来源:origin: org.infinispan/infinispan-core
private void induceListenerMalfunctioning(boolean throwError, FailureType failureType) throws Throwable {
Cache cache1 = cache(0, "replSync");
Cache cache2 = cache(1, "replSync");
ErrorInducingListener listener = new ErrorInducingListener(throwError);
cache2.addListener(listener);
try {
cache1.put(failureType, 1);
} catch (RemoteException e) {
Throwable cause = e.getCause(); // get the exception behind the remote one
if (throwError && cause.getCause() instanceof InvocationTargetException)
throw cause.getCause().getCause();
else
throw cause.getCause();
} finally {
cache2.removeListener(listener);
}
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testRolledBackLocal() throws SystemException, NotSupportedException {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
Cache<Object, String> cache2 = cache(2, CACHE_NAME);
ClusterListener clusterListener = new ClusterListener();
cache0.addListener(clusterListener);
MagicKey key1 = new MagicKey(cache0);
MagicKey key2 = new MagicKey(cache1, cache0);
TransactionManager tm = cache2.getAdvancedCache().getTransactionManager();
tm.begin();
cache0.put(key1, FIRST_VALUE);
assertEquals(clusterListener.events.size(), 0);
cache0.put(key2, SECOND_VALUE);
assertEquals(clusterListener.events.size(), 0);
tm.rollback();
assertEquals(clusterListener.events.size(), 0);
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testRolledBackOriginatorNotLocal() throws SystemException, NotSupportedException {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
Cache<Object, String> cache2 = cache(2, CACHE_NAME);
ClusterListener clusterListener = new ClusterListener();
cache0.addListener(clusterListener);
MagicKey key1 = new MagicKey(cache0);
MagicKey key2 = new MagicKey(cache1, cache0);
TransactionManager tm = cache2.getAdvancedCache().getTransactionManager();
tm.begin();
cache2.put(key1, FIRST_VALUE);
assertEquals(clusterListener.events.size(), 0);
cache2.put(key2, SECOND_VALUE);
assertEquals(clusterListener.events.size(), 0);
tm.rollback();
assertEquals(clusterListener.events.size(), 0);
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testPreviousValueConverterEventRaisedLocalNode() {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
String previousValue = "myOldValue";
long previousExpiration = 10000000;
MagicKey key = new MagicKey(cache0);
cache0.put(key, previousValue, previousExpiration, TimeUnit.MILLISECONDS);
ClusterListener clusterListener = listener();
cache0.addListener(clusterListener, null, new StringAppender());
String newValue = "myBrandSpankingNewValue";
long newExpiration = 314159;
verifySimpleModification(cache0, key, newValue, newExpiration, clusterListener,
previousValue + previousExpiration + newValue + newExpiration);
}
代码示例来源:origin: org.infinispan/infinispan-core
public void testListenerRemoval() {
cache.put("x", "y");
AtomicInteger i = new AtomicInteger(0);
int listenerSize = cache.getListeners().size();
CacheListener l = new CacheListener(i);
cache.addListener(l);
assertEquals(listenerSize + 1, cache.getListeners().size());
assert cache.getListeners().contains(l);
assert 0 == i.get();
cache.get("x");
assert 1 == i.get();
// remove the replListener
cache.removeListener(l);
assertEquals(listenerSize, cache.getListeners().size());
i.set(0);
assert 0 == i.get();
cache.get("x");
assert 0 == i.get();
}
代码示例来源:origin: org.infinispan/infinispan-core
public void testCallbackValues() throws Exception {
Cache<Object, Object> cache1 = cache(0, "replSync");
cache(1, "replSync");
MockListener l = new MockListener();
cache1.addListener(l);
try {
Pojo pojo = new Pojo();
cache1.put("key", pojo);
assertTrue("received " + l.newValue.getClass().getName(), l.newValue instanceof Pojo);
} finally {
cache1.removeListener(l);
}
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testNoAutCommitAndExpiryListener() throws SystemException, NotSupportedException,
HeuristicRollbackException, HeuristicMixedException, RollbackException {
ExpiryListener expiryListener = new ExpiryListener();
Cache<String, String> applicationCache = cacheManager.getCache();
applicationCache.addListener(expiryListener);
TransactionManager tm = applicationCache.getAdvancedCache().getTransactionManager();
tm.begin();
applicationCache.put("test1", "value1", 1, TimeUnit.SECONDS);
tm.commit();
tm.begin();
applicationCache.put("test2", "value2", 1, TimeUnit.SECONDS);
tm.commit();
timeService.advance(TimeUnit.SECONDS.toMillis(10));
ExpirationManager manager = applicationCache.getAdvancedCache().getExpirationManager();
manager.processExpiration();
assertEquals(2, expiryListener.getCount());
}
代码示例来源:origin: org.infinispan/infinispan-core
@Override
public void call() {
Cache<String, Person> cache = cm.getCache();
cm.getClassWhiteList().addClasses(Person.class);
// Obtain cache with custom valueEncoder
Cache storeMarshalled = cache.getAdvancedCache().withEncoding(JavaSerializationEncoder.class);
// Add a listener
SimpleListener simpleListener = new SimpleListener();
storeMarshalled.addListener(simpleListener);
Person value = new Person();
storeMarshalled.put("1", value);
// Assert values returned are passed through the valueEncoder
assertEquals(simpleListener.events.size(), 1);
assertEquals(simpleListener.events.get(0).getKey(), "1");
assertEquals(simpleListener.events.get(0).getValue(), value);
}
});
代码示例来源:origin: org.infinispan/infinispan-core
private void doCallsNormal(Method m,
boolean isInjectInPre, FailureLocation failLoc) {
Cache<String, String> cache = manager(0).getCache();
ErrorInducingListener listener =
new ErrorInducingListener(isInjectInPre, failLoc);
cache.addListener(listener);
cache.put(k(m), v(m));
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testPreviousValueFilterEventRaisedLocalNode() {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
String previousValue = "myOldValue";
long previousExpiration = 10000000;
MagicKey key = new MagicKey(cache0, cache1);
cache0.put(key, previousValue, previousExpiration, TimeUnit.MILLISECONDS);
ClusterListener clusterListener = listener();
cache1.addListener(clusterListener, new NewLifespanLargerFilter<Object, String>(), null);
// This event is ignored because lifespan is shorter
cache0.put(key, previousValue, previousExpiration - 100, TimeUnit.MILLISECONDS);
String newValue = "myBrandSpankingNewValue";
long newExpiration = 314159265;
verifySimpleModification(cache0, key, newValue, newExpiration, clusterListener, newValue);
}
代码示例来源:origin: org.infinispan/infinispan-core
@Test
public void testPreviousValueFilterEventRaisedBackupOwnerNode() {
Cache<Object, String> cache0 = cache(0, CACHE_NAME);
Cache<Object, String> cache1 = cache(1, CACHE_NAME);
String previousValue = "myOldValue";
long previousExpiration = 10000000;
MagicKey key = new MagicKey(cache0, cache1);
// This event is ignored because no previous lifespan
cache0.put(key, previousValue, previousExpiration, TimeUnit.MILLISECONDS);
ClusterListener clusterListener = listener();
cache1.addListener(clusterListener, new NewLifespanLargerFilter<Object, String>(), null);
// This event is ignored because lifespan is shorter
cache0.put(key, previousValue, previousExpiration - 100, TimeUnit.MILLISECONDS);
String newValue = "myBrandSpankingNewValue";
long newExpiration = 314159265;
verifySimpleModification(cache0, key, newValue, newExpiration, clusterListener, newValue);
}
代码示例来源:origin: org.infinispan/infinispan-query
@Override
public void call() {
LuceneIndexTracker luceneIndexTracker = new LuceneIndexTracker(new File(indexDir + "/person"));
luceneIndexTracker.mark();
Cache<Integer, Person> cache = cm.getCache();
CacheListener cacheListener = new CacheListener();
cache.addListener(cacheListener);
cache.put(1, person1);
cache.put(2, person2);
assertEquals(cacheListener.numberOfPassivations(), 1);
assertEquals(cacheListener.numberOfActivations(), 0);
assertTrue(luceneIndexTracker.indexChanged());
luceneIndexTracker.mark();
cache.get(1);
assertEquals(cacheListener.numberOfActivations(), 1);
assertFalse(luceneIndexTracker.indexChanged());
}
});
代码示例来源:origin: org.infinispan/infinispan-core
/**
* If it is used asynchronous listener all callbacks are made in separate thread. Exceptions are only logged, not
* thrown. See {@link org.infinispan.notifications.impl.AbstractListenerImpl} invoke() method logic
*/
private void doCallsWithExcepListAsync(Method m, boolean isInjectInPre,
FailureLocation failLoc) {
Cache<String, String> cache = manager(0).getCache();
ErrorInducingListenerAsync listenerAsync =
new ErrorInducingListenerAsync(isInjectInPre, failLoc);
cache.addListener(listenerAsync);
cache.put(k(m), v(m));
assert cache.get(k(m)).equals(v(m));
assert listenerAsync.caller != Thread.currentThread();
}
内容来源于网络,如有侵权,请联系作者删除!