use of org.apache.geode.cache.CacheListener in project geode by apache.
the class ProxyDUnitTest method remoteOriginOps.
/**
* check remote ops done in a normal vm are correctly distributed to PROXY regions
*/
private void remoteOriginOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(dp);
af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListener() {
public void afterUpdate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterCreate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterInvalidate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterDestroy(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionInvalidate(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionDestroy(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionClear(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionCreate(RegionEvent e) {
}
public void afterRegionLive(RegionEvent e) {
}
public void close() {
}
};
af.addCacheListener(cl1);
Region r = createRootRegion("ProxyDUnitTest", af.create());
this.clInvokeCount = 0;
doCreateOtherVm();
DMStats stats = getDMStats();
long receivedMsgs = stats.getReceivedMessages();
if (ip.isAll()) {
getOtherVm().invoke(new CacheSerializableRunnable("do put") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.put("p", "v");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
// failure
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("p", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do create") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.create("c", "v");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do update") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.put("c", "v2");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.UPDATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals("v2", ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do invalidate") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.invalidate("c");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.INVALIDATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do destroy") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.destroy("c");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.DESTROY, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do putAll") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
Map m = new HashMap();
m.put("putAllKey1", "putAllValue1");
m.put("putAllKey2", "putAllValue2");
r.putAll(m);
}
});
assertEquals(2, this.clInvokeCount);
// @todo darrel; check putAll events
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do netsearch") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
// total miss
assertEquals(null, r.get("loadkey"));
}
});
assertEquals(0, this.clInvokeCount);
} else {
getOtherVm().invoke(new CacheSerializableRunnable("do entry ops") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.put("p", "v");
r.create("c", "v");
// update
r.put("c", "v");
r.invalidate("c");
r.destroy("c");
{
Map m = new HashMap();
m.put("putAllKey1", "putAllValue1");
m.put("putAllKey2", "putAllValue2");
r.putAll(m);
}
// total miss
assertEquals(null, r.get("loadkey"));
}
});
assertEquals(0, this.clInvokeCount);
assertEquals(0, r.size());
// check the stats to make sure none of the above sent up messages
assertEquals(receivedMsgs, stats.getReceivedMessages());
}
{
AttributesMutator am = r.getAttributesMutator();
CacheLoader cl = new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
if (helper.getKey().equals("loadkey")) {
return "loadvalue";
} else if (helper.getKey().equals("loadexception")) {
throw new CacheLoaderException("expected");
} else {
return null;
}
}
public void close() {
}
};
am.setCacheLoader(cl);
}
receivedMsgs = stats.getReceivedMessages();
getOtherVm().invoke(new CacheSerializableRunnable("check net loader") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
// net load
assertEquals("loadvalue", r.get("loadkey"));
// total miss
assertEquals(null, r.get("foobar"));
try {
r.get("loadexception");
fail("expected CacheLoaderException");
} catch (CacheLoaderException expected) {
}
}
});
assertTrue(stats.getReceivedMessages() > receivedMsgs);
if (ip.isAll()) {
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.NET_LOAD_CREATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
this.clInvokeCount = 0;
} else {
assertEquals(0, this.clInvokeCount);
}
{
AttributesMutator am = r.getAttributesMutator();
am.setCacheLoader(null);
CacheWriter cw = new CacheWriterAdapter() {
public void beforeCreate(EntryEvent event) throws CacheWriterException {
throw new CacheWriterException("expected");
}
};
am.setCacheWriter(cw);
}
receivedMsgs = stats.getReceivedMessages();
getOtherVm().invoke(new CacheSerializableRunnable("check net write") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
try {
r.put("putkey", "putvalue");
fail("expected CacheWriterException");
} catch (CacheWriterException expected) {
}
}
});
assertTrue(stats.getReceivedMessages() > receivedMsgs);
{
AttributesMutator am = r.getAttributesMutator();
am.setCacheWriter(null);
}
assertEquals(0, this.clInvokeCount);
this.clLastEvent = null;
getOtherVm().invoke(new CacheSerializableRunnable("check region invalidate") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.invalidateRegion();
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.REGION_INVALIDATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
this.clLastEvent = null;
getOtherVm().invoke(new CacheSerializableRunnable("check region clear") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.clear();
}
});
assertEquals(2, this.clInvokeCount);
assertEquals(Operation.REGION_CLEAR, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
this.clLastEvent = null;
getOtherVm().invoke(new CacheSerializableRunnable("check region destroy") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.destroyRegion();
}
});
assertEquals(3, this.clInvokeCount);
assertEquals(Operation.REGION_DESTROY, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertTrue(r.isDestroyed());
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class PutAllCallBkRemoteVMDUnitTest method createCacheForVM1.
public static void createCacheForVM1() {
try {
CacheListener aListener = new AfterCreateCallback();
CacheWriter aWriter = new BeforeCreateCallback();
ds = (new PutAllCallBkRemoteVMDUnitTest()).getSystem(props);
cache = CacheFactory.create(ds);
AttributesFactory factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr1 = factory.create();
paperRegion = cache.createRegion("paper", attr1);
factory.setCacheWriter(aWriter);
factory.addCacheListener(aListener);
RegionAttributes attr = factory.create();
region = cache.createRegion("map", attr);
} catch (CacheException ex) {
throw new RuntimeException("vm1 cache creation exception", ex);
}
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class PutAllCallBkSingleVMDUnitTest method testputAllSingleVM.
// test methods
@Test
public void testputAllSingleVM() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
// Object obj2;
Object[] objArr = new Object[1];
for (int i = 0; i < 5; i++) {
objArr[0] = "" + i;
vm0.invoke(PutAllCallBkSingleVMDUnitTest.class, "putMethod", objArr);
}
vm0.invoke(() -> PutAllCallBkSingleVMDUnitTest.putAllMethod());
vm0.invoke(new CacheSerializableRunnable("temp1") {
public void run2() throws CacheException {
if (!afterCreate) {
fail("FAILED in aftercreate call back");
}
assertEquals(region.size(), putAllcounter);
assertEquals(region.size(), beforeCreateputAllcounter);
}
});
vm0.invoke(new CacheSerializableRunnable("abc") {
public void run2() throws CacheException {
CacheListener bListener = new AfterUpdateCallback();
CacheWriter bWriter = new BeforeUpdateCallback();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setCacheWriter(bWriter);
factory.setCacheListener(bListener);
RegionAttributes attr = factory.create();
Region tempRegion = cache.createRegion("temp", attr);
// to invoke afterUpdate we should make sure that entries are already present
for (int i = 0; i < 5; i++) {
tempRegion.put(new Integer(i), new String("region" + i));
}
Map m = new HashMap();
for (int i = 0; i < 5; i++) {
m.put(new Integer(i), new String("map" + i));
}
tempRegion.putAll(m, "putAllAfterUpdateCallback");
// now, verifying callbacks
if (!afterUpdate) {
fail("FAILED in afterupdate call back");
}
assertEquals(tempRegion.size(), afterUpdateputAllcounter);
assertEquals(tempRegion.size(), beforeUpdateputAllcounter);
}
});
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class DistributedRegion method postCreateRegion.
/**
* In addition to inherited code this method also invokes RegionMembershipListeners
*/
@Override
protected void postCreateRegion() {
super.postCreateRegion();
// should we sync on this.distAdvisor first to prevent bug 44369?
synchronized (this.advisorListener) {
Set others = this.advisorListener.getInitialMembers();
CacheListener[] listeners = fetchCacheListenersField();
if (listeners != null) {
for (CacheListener listener : listeners) {
if (listener instanceof RegionMembershipListener) {
RegionMembershipListener regionMembershipListener = (RegionMembershipListener) listener;
try {
DistributedMember[] otherDms = new DistributedMember[others.size()];
others.toArray(otherDms);
regionMembershipListener.initialMembers(this, otherDms);
} catch (VirtualMachineError err) {
SystemFailure.initiateFailure(err);
// now, so don't let this thread continue.
throw err;
} catch (Throwable t) {
// Whenever you catch Error or Throwable, you must also
// catch VirtualMachineError (see above). However, there is
// _still_ a possibility that you are dealing with a cascading
// error condition, so you also need to check to see if the JVM
// is still usable:
SystemFailure.checkFailure();
logger.error(LocalizedMessage.create(LocalizedStrings.DistributedRegion_EXCEPTION_OCCURRED_IN_REGIONMEMBERSHIPLISTENER), t);
}
}
}
}
Set<String> allGatewaySenderIds = getAllGatewaySenderIds();
if (!allGatewaySenderIds.isEmpty()) {
for (GatewaySender sender : this.cache.getAllGatewaySenders()) {
if (sender.isParallel() && allGatewaySenderIds.contains(sender.getId())) {
// addShadowPartitionedRegionForUserRR
if (sender.getId().contains(AsyncEventQueueImpl.ASYNC_EVENT_QUEUE_PREFIX)) {
throw new AsyncEventQueueConfigurationException(LocalizedStrings.ParallelAsyncEventQueue_0_CAN_NOT_BE_USED_WITH_REPLICATED_REGION_1.toLocalizedString(new Object[] { AsyncEventQueueImpl.getAsyncEventQueueIdFromSenderId(sender.getId()), this.getFullPath() }));
}
throw new GatewaySenderConfigurationException(LocalizedStrings.ParallelGatewaySender_0_CAN_NOT_BE_USED_WITH_REPLICATED_REGION_1.toLocalizedString(new Object[] { sender.getId(), this.getFullPath() }));
}
}
}
}
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class AbstractRegion method initCacheListeners.
@Override
public void initCacheListeners(CacheListener[] newListeners) {
checkReadiness();
CacheListener[] listenersToAdd = null;
if (newListeners != null) {
listenersToAdd = new CacheListener[newListeners.length];
for (int i = 0; i < newListeners.length; i++) {
listenersToAdd[i] = wrapRegionMembershipListener(newListeners[i]);
}
}
CacheListener[] oldListeners;
synchronized (this.clSync) {
oldListeners = this.cacheListeners;
if (listenersToAdd == null || listenersToAdd.length == 0) {
this.cacheListeners = EMPTY_LISTENERS;
} else {
// we have some listeners to add
if (Arrays.asList(listenersToAdd).contains(null)) {
throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_INITCACHELISTENERS_PARAMETER_HAD_A_NULL_ELEMENT.toLocalizedString());
}
CacheListener[] newCacheListeners = new CacheListener[listenersToAdd.length];
System.arraycopy(listenersToAdd, 0, newCacheListeners, 0, newCacheListeners.length);
this.cacheListeners = newCacheListeners;
}
}
// moved the following out of the sync for bug 34512
if (listenersToAdd == null || listenersToAdd.length == 0) {
if (oldListeners != null && oldListeners.length > 0) {
for (CacheListener oldListener : oldListeners) {
closeCacheCallback(oldListener);
}
cacheListenersChanged(false);
}
} else {
// we had some listeners to add
if (oldListeners != null && oldListeners.length > 0) {
for (CacheListener oldListener : oldListeners) {
closeCacheCallback(oldListener);
}
} else {
cacheListenersChanged(true);
}
}
}
Aggregations