use of org.apache.geode.cache.CacheListener in project geode by apache.
the class AbstractRegion method addCacheListener.
@Override
public void addCacheListener(CacheListener aListener) {
checkReadiness();
if (aListener == null) {
throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_ADDCACHELISTENER_PARAMETER_WAS_NULL.toLocalizedString());
}
CacheListener wcl = wrapRegionMembershipListener(aListener);
boolean changed = false;
synchronized (this.clSync) {
CacheListener[] oldListeners = this.cacheListeners;
if (oldListeners == null || oldListeners.length == 0) {
this.cacheListeners = new CacheListener[] { wcl };
changed = true;
} else {
List<CacheListener> listeners = Arrays.asList(oldListeners);
if (!listeners.contains(aListener)) {
this.cacheListeners = (CacheListener[]) ArrayUtils.insert(oldListeners, oldListeners.length, wcl);
}
}
}
if (changed) {
// moved the following out of the sync for bug 34512
cacheListenersChanged(true);
}
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class AbstractRegion method removeCacheListener.
@Override
public void removeCacheListener(CacheListener aListener) {
checkReadiness();
if (aListener == null) {
throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_REMOVECACHELISTENER_PARAMETER_WAS_NULL.toLocalizedString());
}
boolean changed = false;
synchronized (this.clSync) {
CacheListener[] oldListeners = this.cacheListeners;
if (oldListeners != null && oldListeners.length > 0) {
List newListeners = new ArrayList(Arrays.asList(oldListeners));
if (newListeners.remove(aListener)) {
if (newListeners.isEmpty()) {
this.cacheListeners = EMPTY_LISTENERS;
} else {
CacheListener[] newCacheListeners = new CacheListener[newListeners.size()];
newListeners.toArray(newCacheListeners);
this.cacheListeners = newCacheListeners;
}
closeCacheCallback(aListener);
if (newListeners.isEmpty()) {
changed = true;
}
}
}
}
if (changed) {
cacheListenersChanged(false);
}
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class SlowRecDUnitTest method doCreateOtherVm.
private void doCreateOtherVm(final Properties p, final boolean addListener) {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
getSystem(p);
createAckRegion(true, false);
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.DISTRIBUTED_NO_ACK);
af.setDataPolicy(DataPolicy.REPLICATE);
if (addListener) {
CacheListener cl = new CacheListenerAdapter() {
public void afterUpdate(EntryEvent event) {
// make the slow receiver event slower!
try {
Thread.sleep(500);
} catch (InterruptedException shuttingDown) {
fail("interrupted");
}
}
};
af.setCacheListener(cl);
} else {
CacheListener cl = new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
if (event.getKey().equals("sleepkey")) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
public void afterUpdate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
if (event.getKey().equals("sleepkey")) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
public void afterInvalidate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
public void afterDestroy(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
};
af.setCacheListener(cl);
}
Region r1 = createRootRegion("slowrec", af.create());
// place holder so we receive updates
r1.create("key", "value");
}
});
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class TXOrderDUnitTest method testFarSideOpForLoad.
/**
* Tests fix for #40870 Remote CacheListeners invoke afterCreate with Operation.LOCAL_LOAD_CREATE
* when create executed transactionally"
*/
@Ignore("TODO: test is disabled")
@Test
public void testFarSideOpForLoad() throws Exception {
Host host = Host.getHost(0);
VM vm1 = host.getVM(0);
VM vm2 = host.getVM(1);
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertTrue(e.getOperation().isLocalLoad());
}
};
af.addCacheListener(cl1);
CacheLoader cl = new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
LogWriterUtils.getLogWriter().info("Loading value:" + helper.getKey() + "_value");
return helper.getKey() + "_value";
}
public void close() {
}
};
af.setCacheLoader(cl);
createRootRegion("r1", af.create());
return null;
}
});
vm2.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
LogWriterUtils.getLogWriter().info("op:" + e.getOperation().toString());
assertTrue(!e.getOperation().isLocalLoad());
}
};
af.addCacheListener(cl1);
createRootRegion("r1", af.create());
return null;
}
});
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region r = getRootRegion("r1");
getCache().getCacheTransactionManager().begin();
r.get("obj_2");
getCache().getCacheTransactionManager().commit();
return null;
}
});
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class TXOrderDUnitTest method testFarSideOrder.
/**
* make sure listeners get invoked in correct order on far side of tx
*/
@Test
public void testFarSideOrder() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(getCurrentExpectedKey(), e.getKey());
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
r2.createSubregion("r3", af.create());
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
ArrayList keys = new ArrayList();
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
keys.add(ee.getKey());
assertEquals(null, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
assertEquals(TXOrderDUnitTest.this.expectedKeys, keys);
TXOrderDUnitTest.this.invokeCount = 1;
}
};
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ctm.addListener(tl1);
this.invokeCount = 0;
this.clCount = 0;
this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
doCommitOtherVm();
assertEquals(1, this.invokeCount);
assertEquals(6, this.clCount);
}
Aggregations