use of org.apache.geode.cache.CacheListener in project geode by apache.
the class RemoteTransactionDUnitTest method doTestListeners.
private void doTestListeners(final OP op) {
Host host = Host.getHost(0);
VM acc = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(acc, datastore, 0);
VM accessor = getVMForTransactions(acc, datastore);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region ref = getCache().getRegion(D_REFERENCE);
ref.getAttributesMutator().addCacheListener(new TestCacheListener(true));
ref.getAttributesMutator().setCacheWriter(new TestCacheWriter(true));
Region cust = getCache().getRegion(CUSTOMER);
cust.getAttributesMutator().addCacheListener(new TestCacheListener(true));
cust.getAttributesMutator().setCacheWriter(new TestCacheWriter(true));
Region order = getCache().getRegion(ORDER);
order.getAttributesMutator().addCacheListener(new TestCacheListener(true));
order.getAttributesMutator().setCacheWriter(new TestCacheWriter(true));
getGemfireCache().getTxManager().addListener(new TestTxListener(true));
if (!getGemfireCache().isClient()) {
getGemfireCache().getTxManager().setWriter(new TestTxWriter(true));
}
return null;
}
});
SerializableCallable addListenersToDataStore = new SerializableCallable() {
public Object call() throws Exception {
Region ref = getCache().getRegion(D_REFERENCE);
ref.getAttributesMutator().addCacheListener(new TestCacheListener(false));
ref.getAttributesMutator().setCacheWriter(new TestCacheWriter(false));
Region cust = getCache().getRegion(CUSTOMER);
cust.getAttributesMutator().addCacheListener(new TestCacheListener(false));
cust.getAttributesMutator().setCacheWriter(new TestCacheWriter(false));
Region order = getCache().getRegion(ORDER);
order.getAttributesMutator().addCacheListener(new TestCacheListener(false));
order.getAttributesMutator().setCacheWriter(new TestCacheWriter(false));
getGemfireCache().getTxManager().addListener(new TestTxListener(false));
if (!getGemfireCache().isClient()) {
getGemfireCache().getTxManager().setWriter(new TestTxWriter(false));
}
return null;
}
};
datastore.invoke(addListenersToDataStore);
accessor.invoke(new DoOpsInTX(op));
// Invalidate operations don't fire cache writers, so don't assert they were fired.
if (op != OP.INVALIDATE) {
// Ensure the cache writer was not fired in accessor
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region cust = getCache().getRegion(CUSTOMER);
assertFalse(((TestCacheWriter) cust.getAttributes().getCacheWriter()).wasFired);
Region order = getCache().getRegion(ORDER);
assertFalse(((TestCacheWriter) order.getAttributes().getCacheWriter()).wasFired);
Region ref = getCache().getRegion(D_REFERENCE);
assertFalse(((TestCacheWriter) ref.getAttributes().getCacheWriter()).wasFired);
return null;
}
});
// Ensure the cache writer was fired in the primary
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region cust = getCache().getRegion(CUSTOMER);
assertTrue(((TestCacheWriter) cust.getAttributes().getCacheWriter()).wasFired);
Region order = getCache().getRegion(ORDER);
assertTrue(((TestCacheWriter) order.getAttributes().getCacheWriter()).wasFired);
Region ref = getCache().getRegion(D_REFERENCE);
assertTrue(((TestCacheWriter) ref.getAttributes().getCacheWriter()).wasFired);
return null;
}
});
}
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.commit();
return null;
}
});
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
TestTxListener l = (TestTxListener) getGemfireCache().getTxManager().getListener();
assertTrue(l.isListenerInvoked());
return null;
}
});
SerializableCallable verifyListeners = new SerializableCallable() {
public Object call() throws Exception {
Region cust = getCache().getRegion(CUSTOMER);
Region order = getCache().getRegion(ORDER);
throwListenerException(cust);
throwListenerException(order);
throwWriterException(cust);
throwWriterException(order);
if (!getGemfireCache().isClient()) {
throwTransactionCallbackException();
}
return null;
}
private void throwTransactionCallbackException() throws Exception {
TestTxListener l = (TestTxListener) getGemfireCache().getTxManager().getListener();
if (l.ex != null) {
throw l.ex;
}
TestTxWriter w = (TestTxWriter) getGemfireCache().getTxManager().getWriter();
if (w.ex != null) {
throw w.ex;
}
}
private void throwListenerException(Region r) throws Exception {
Exception e = null;
CacheListener listener = r.getAttributes().getCacheListeners()[0];
if (listener instanceof TestCacheListener) {
e = ((TestCacheListener) listener).ex;
} else {
// e = ((ClientListener)listener).???
}
if (e != null) {
throw e;
}
}
private void throwWriterException(Region r) throws Exception {
Exception e = null;
CacheListener listener = r.getAttributes().getCacheListeners()[0];
if (listener instanceof TestCacheListener) {
e = ((TestCacheListener) listener).ex;
} else {
// e = ((ClientListener)listener).???
}
if (e != null) {
throw e;
}
}
};
accessor.invoke(verifyListeners);
datastore.invoke(verifyListeners);
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class HAValidateDuplicateListener method createClientCache.
public static void createClientCache(String hostName, Integer port1, Integer port2) throws Exception {
int PORT1 = port1.intValue();
int PORT2 = port2.intValue();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new HADuplicateDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
ClientServerTestCase.configureConnectionPool(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null);
factory.setScope(Scope.DISTRIBUTED_ACK);
CacheListener clientListener = new HAValidateDuplicateListener();
factory.setCacheListener(clientListener);
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
assertNotNull(region);
region.registerInterest("ALL_KEYS", InterestResultPolicy.NONE);
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class HAExpiryDUnitTest method createRegionQueue.
public static void createRegionQueue(Boolean isDurable) throws Exception {
new HAExpiryDUnitTest().createCache(new Properties());
HARegionQueueAttributes hattr = new HARegionQueueAttributes();
// setting expiry time for the regionqueue.
hattr.setExpiryTime(4);
RegionQueue regionqueue = HARegionQueue.getHARegionQueueInstance(regionQueueName, cache, hattr, HARegionQueue.NON_BLOCKING_HA_QUEUE, isDurable.booleanValue());
assertNotNull(regionqueue);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
CacheListener serverListener = new VMListener();
factory.setCacheListener(serverListener);
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class vmListenerToCheckHARegionQueue method createRegionQueue.
public static void createRegionQueue() throws Exception {
new HAGIIBugDUnitTest().createCache(new Properties());
HARegionQueueAttributes hattr = new HARegionQueueAttributes();
// setting expiry time for the regionqueue.
hattr.setExpiryTime(12000000);
RegionQueue regionqueue = HARegionQueue.getHARegionQueueInstance(regionQueueName, cache, hattr, HARegionQueue.NON_BLOCKING_HA_QUEUE, false);
assertNotNull(regionqueue);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
CacheListener regionListener = new vmListenerToPutInHARegionQueue();
factory.setCacheListener(regionListener);
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class WANTestBase method addCacheListenerOnQueueBucketRegion.
private void addCacheListenerOnQueueBucketRegion(String senderId, int numBuckets) {
Set<GatewaySender> senders = cache.getGatewaySenders();
GatewaySender sender = null;
for (GatewaySender s : senders) {
if (s.getId().equals(senderId)) {
sender = s;
break;
}
}
RegionQueue parallelQueue = ((AbstractGatewaySender) sender).getQueues().toArray(new RegionQueue[1])[0];
PartitionedRegion region = (PartitionedRegion) parallelQueue.getRegion();
for (int i = 0; i < numBuckets; i++) {
BucketRegion br = region.getBucketRegion(i);
if (br != null) {
AttributesMutator mutator = br.getAttributesMutator();
CacheListener listener = new QueueListener();
mutator.addCacheListener(listener);
}
}
}
Aggregations