use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ConflationDUnitTest method createClientCache2CommonWriter.
/**
* create client 2 with 2 regions with sharing a common writer and having a common listener
*
* @throws Exception
*/
public static void createClientCache2CommonWriter(String host, Integer port) throws Exception {
ConflationDUnitTest test = new ConflationDUnitTest();
cache = test.createCache(createProperties1());
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setPoolName(createPool(host, "p1", port, true).getName());
factory.addCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
LogWriterUtils.getLogWriter().info("Listener received event " + event);
String val = (String) event.getNewValue();
synchronized (ConflationDUnitTest.class) {
if (val.equals(MARKER)) {
count++;
} else {
counterCreate++;
}
if (2 == count) {
ConflationDUnitTest.class.notify();
}
}
}
public void afterUpdate(EntryEvent event) {
LogWriterUtils.getLogWriter().info("Listener received event " + event);
synchronized (this) {
counterUpdate++;
}
}
public void afterDestroy(EntryEvent event) {
LogWriterUtils.getLogWriter().info("Listener received event " + event);
synchronized (this) {
if (!event.getKey().equals(MARKER)) {
counterDestroy++;
}
}
}
});
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME1, attrs);
cache.createRegion(REGION_NAME2, attrs);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ClientConflationDUnitTest method createClientCache.
/**
* create client 2 with 2 regions each with a unique writer and unique listeners
*
* @throws Exception
*/
public static void createClientCache(String host, Integer port, String conflation) throws Exception {
ClientConflationDUnitTest test = new ClientConflationDUnitTest();
cacheClient = test.createCache(createProperties1(conflation));
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
createPool2(host, factory, port);
factory.setCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
synchronized (ClientConflationDUnitTest.class) {
counterCreate1++;
}
}
public void afterUpdate(EntryEvent event) {
// getLogWriter().info("afterUpdate event = " + event, new Exception());
synchronized (this) {
counterUpdate1++;
}
}
});
RegionAttributes attrs = factory.create();
cacheClient.createRegion(REGION_NAME1, attrs);
createPool2(host, factory, port);
factory.setCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
synchronized (ClientConflationDUnitTest.class) {
counterCreate2++;
}
}
public void afterUpdate(EntryEvent event) {
synchronized (this) {
counterUpdate2++;
}
}
});
attrs = factory.create();
cacheClient.createRegion(REGION_NAME2, attrs);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class MyGatewayEventSubstitutionFilter method addCacheListenerAndCloseCache.
public static void addCacheListenerAndCloseCache(String regionName) {
final Region region = cache.getRegion(Region.SEPARATOR + regionName);
assertNotNull(region);
CacheListenerAdapter cl = new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
if ((Long) event.getKey() == 900) {
cache.getLogger().fine(" Gateway sender is killed by a test");
cache.close();
cache.getDistributedSystem().disconnect();
}
}
};
region.getAttributesMutator().addCacheListener(cl);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.
@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "Region";
final String key = "testKey";
int port = createServerRegion(vm0, name, true);
vm0.invoke(new SerializableCallable("create old entry") {
public Object call() throws Exception {
LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
r.put(key, "value");
AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
// set an old timestamp in the entry - thirty minutes ago
entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
return null;
}
});
createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
vm1.invoke(new SerializableCallable("fetch entry and validate") {
public Object call() throws Exception {
final Long[] expirationTimeMillis = new Long[1];
int expirationSeconds = 15;
LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
AttributesMutator mutator = r.getAttributesMutator();
mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
mutator.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterDestroy(EntryEvent event) {
expirationTimeMillis[0] = System.currentTimeMillis();
}
});
// fetch the entry from the server and make sure it doesn't expire early
if (!r.containsKey(key)) {
r.get(key);
}
final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
return expirationTimeMillis[0] != null;
});
disconnectFromDS();
assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
return null;
}
});
vm0.invoke(new SerializableRunnable() {
public void run() {
disconnectFromDS();
}
});
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class PartitionedRegionDUnitTest method testRegionInvalidationWithAdjunctMessages.
/**
* Bug #47235 concerns assertion failures being thrown when there is a member that receives
* adjunct messages (as in a WAN gateway, a peer with clients, etc).
*/
@Test
public void testRegionInvalidationWithAdjunctMessages() throws Exception {
final String name = getUniqueName();
VM vm1 = Host.getHost(0).getVM(1);
Cache cache = getCache();
RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
Region pr = fact.create(name + "Region");
pr.put("Object1", "Value1");
vm1.invoke(new SerializableRunnable("create PR") {
@Override
public void run() {
RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
fact.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
fact.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterInvalidate(EntryEvent event) {
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("afterInvalidate invoked with " + event);
InvalidateInvoked = true;
}
});
fact.create(name + "Region");
}
});
try {
pr.invalidateRegion();
assertTrue("vm1 should have invoked the listener for an invalidateRegion operation", (Boolean) vm1.invoke(new SerializableCallable("getStatus") {
public Object call() {
return InvalidateInvoked;
}
}));
} finally {
disconnectAllFromDS();
}
}
Aggregations