use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class EventIDVerificationDUnitTest method createServerCache.
public static Integer createServerCache() throws Exception {
new EventIDVerificationDUnitTest().createCache(new Properties());
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterUpdate(EntryEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterDestroy(EntryEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterRegionDestroy(RegionEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((RegionEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterRegionClear(RegionEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
// verifyEventIDsDuringRegionDestroy(event);
testEventIDResult = ((RegionEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
});
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
CacheServer server1 = cache.addCacheServer();
server1.setPort(port);
server1.setNotifyBySubscription(true);
server1.start();
return new Integer(server1.getPort());
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class CallbackArgDUnitTest method doCommitOtherVm.
private void doCommitOtherVm() {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
AttributesFactory af = new AttributesFactory();
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(callbackArg, e.getCallbackArgument());
}
};
af.addCacheListener(cl1);
af.setScope(Scope.DISTRIBUTED_ACK);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
Region r3 = r2.createSubregion("r3", af.create());
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
}
};
ctm.addListener(tl1);
ctm.begin();
r2.put("b", "value1", callbackArg);
r3.put("c", "value2", callbackArg);
r1.put("a", "value3", callbackArg);
r1.put("a2", "value4", callbackArg);
r3.put("c2", "value5", callbackArg);
r2.put("b2", "value6", callbackArg);
ctm.commit();
}
});
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ClientServerTransactionDUnitTest method testCallbacks.
// Disabled due to bug 47083
@Ignore
@Test
public void testCallbacks() {
Host host = Host.getHost(0);
VM datastore = host.getVM(1);
VM client = host.getVM(2);
int port = createRegionsAndStartServer(datastore, false);
createClientRegion(client, port, true);
class ClientTxListener extends TransactionListenerAdapter {
private boolean afterRollbackInvoked = false;
boolean afterCommitInvoked = false;
@Override
public void afterCommit(TransactionEvent event) {
afterCommitInvoked = true;
verifyEvent(event);
}
@Override
public void afterRollback(TransactionEvent event) {
afterRollbackInvoked = true;
verifyEvent(event);
}
protected void verifyEvent(TransactionEvent event) {
Iterator it = event.getEvents().iterator();
int i = 0;
while (it.hasNext()) {
EntryEvent ev = (EntryEvent) it.next();
if (i == 0)
assertNull(ev.getNewValue());
if (i > 1) {
assertEquals(new CustId(i), ev.getKey());
assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
}
assertTrue(ev.isOriginRemote());
i++;
}
assertEquals(5, event.getEvents().size());
}
}
class ClientTxWriter implements TransactionWriter {
boolean txWriterCalled = false;
public void close() {
}
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
txWriterCalled = true;
Iterator it = event.getEvents().iterator();
int i = 0;
while (it.hasNext()) {
EntryEvent ev = (EntryEvent) it.next();
if (i == 0)
assertNull(ev.getNewValue());
if (i > 1) {
assertEquals(new CustId(i), ev.getKey());
assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
}
assertTrue(ev.isOriginRemote());
i++;
}
assertEquals(5, event.getEvents().size());
}
}
class ClientListener extends CacheListenerAdapter {
boolean invoked = false;
@Override
public void afterCreate(EntryEvent event) {
CustId c = (CustId) event.getKey();
if (c.getCustId() > 1) {
invoked = true;
}
// we document that client transaction are proxied to the server
// and that the callback should be handled appropriately (hence true)
assertTrue(event.isOriginRemote());
assertTrue(event.isOriginRemote());
}
@Override
public void afterUpdate(EntryEvent event) {
assertFalse(event.isOriginRemote());
}
}
class ClientWriter extends CacheWriterAdapter {
@Override
public void beforeCreate(EntryEvent event) throws CacheWriterException {
CustId c = (CustId) event.getKey();
if (c.getCustId() < 2) {
return;
}
fail("cache writer should not be invoked");
}
@Override
public void beforeUpdate(EntryEvent event) throws CacheWriterException {
fail("cache writer should not be invoked");
}
}
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.addListener(new ClientTxListener());
mgr.setWriter(new ClientTxWriter());
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
pr.getAttributesMutator().addCacheListener(new ServerListener());
pr.getAttributesMutator().setCacheWriter(new ServerWriter());
return null;
}
});
client.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.addListener(new ClientTxListener());
try {
mgr.setWriter(new ClientTxWriter());
fail("expected exception not thrown");
} catch (IllegalStateException e) {
}
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
pr.getAttributesMutator().addCacheListener(new ClientListener());
pr.getAttributesMutator().setCacheWriter(new ClientWriter());
return null;
}
});
class doOps extends SerializableCallable {
public doOps(boolean commit) {
this.commit = commit;
}
boolean commit = false;
public Object call() throws Exception {
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
pr.put(new CustId(0), new Customer("name0", "address0"));
pr.put(new CustId(1), new Customer("name1", "address1"));
mgr.begin();
pr.invalidate(new CustId(0));
pr.destroy(new CustId(1));
for (int i = 2; i < 5; i++) {
pr.put(new CustId(i), new Customer("name" + i, "address" + i));
}
if (commit) {
mgr.commit();
} else {
mgr.rollback();
}
return null;
}
}
client.invoke(new doOps(false));
datastore.invoke(new SerializableCallable() {
@SuppressWarnings("synthetic-access")
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
assertTrue(l.afterRollbackInvoked);
return null;
}
});
client.invoke(new doOps(true));
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
assertTrue(l.afterCommitInvoked);
ClientTxWriter w = (ClientTxWriter) mgr.getWriter();
assertTrue(w.txWriterCalled);
return null;
}
});
client.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
assertFalse(l.afterCommitInvoked);
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
ClientListener cl = (ClientListener) pr.getAttributes().getCacheListeners()[0];
assertTrue(cl.invoked);
return null;
}
});
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class PartitionedRegionEvictionDUnitTest method testEntryLRUDeadlock.
/**
* Test that gets do not need to acquire a lock on the region entry when LRU is enabled. This is
* bug 42265
*/
@Test
public void testEntryLRUDeadlock() {
final Host host = Host.getHost(0);
final VM vm0 = host.getVM(0);
final VM vm1 = host.getVM(1);
final String uniqName = getUniqueName();
final int redundantCopies = 0;
final int maxBuckets = 8;
final int maxEntries = 16;
final String name = uniqName + "-PR";
final int extraEntries = 4;
// final int heapPercentage = 66;
// final int evictorInterval = 100;
final SerializableRunnable create = new CacheSerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {
public void run2() {
final AttributesFactory factory = new AttributesFactory();
factory.setOffHeap(isOffHeap());
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setTotalNumBuckets(maxBuckets).create());
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries, EvictionAction.LOCAL_DESTROY));
final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
assertNotNull(pr);
}
};
vm0.invoke(create);
final SerializableRunnable create2 = new SerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {
public void run() {
try {
final AttributesFactory factory = new AttributesFactory();
factory.setOffHeap(isOffHeap());
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setLocalMaxMemory(0).setTotalNumBuckets(maxBuckets).create());
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries));
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
// this listener will cause a deadlock if the get ends up
// locking the entry.
factory.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
Region region = event.getRegion();
Object key = event.getKey();
region.get(key);
}
});
final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
assertNotNull(pr);
} catch (final CacheException ex) {
Assert.fail("While creating Partitioned region", ex);
}
}
};
vm1.invoke(create2);
final SerializableRunnable doPuts = new SerializableRunnable("Do Puts") {
public void run() {
final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
assertNotNull(pr);
for (int counter = 0; counter <= maxEntries + extraEntries; counter++) {
pr.put(new Integer(counter), "value");
}
}
};
vm0.invoke(doPuts);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class HASlowReceiverDUnitTest method createClientCache.
public static void createClientCache(String host, Integer port1, Integer port2, Integer port3, Integer rLevel, Boolean addListener) throws Exception {
CacheServerTestUtil.disableShufflingOfEndpoints();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new HASlowReceiverDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer("localhost", port1).addServer("localhost", port2).addServer("localhost", port3).setSubscriptionEnabled(true).setSubscriptionRedundancy(rLevel.intValue()).setThreadLocalConnections(true).setMinConnections(6).setReadTimeout(20000).setPingInterval(1000).setRetryAttempts(5).create("HASlowRecieverDUnitTestPool");
factory.setScope(Scope.LOCAL);
factory.setPoolName(p.getName());
if (addListener.booleanValue()) {
factory.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterUpdate(EntryEvent event) {
if (event.getNewValue().equals("v20")) {
try {
Thread.sleep(120000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
RegionAttributes attrs = factory.create();
cache.createRegion(regionName, attrs);
pool = p;
}
Aggregations