use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ConflationDUnitTest method createClientCache2UniqueWriter.
/**
* create client 2 with 2 regions each with a unique writer but both having a common listener
*
* @throws Exception
*/
public static void createClientCache2UniqueWriter(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) {
String val = (String) event.getNewValue();
LogWriterUtils.getLogWriter().info("Listener received event " + event);
synchronized (ConflationDUnitTest.class) {
if (val.equals(MARKER)) {
count++;
} else {
counterCreate++;
}
if (2 == count) {
ConflationDUnitTest.class.notify();
}
}
}
public void afterUpdate(EntryEvent event) {
synchronized (this) {
counterUpdate++;
}
}
public void afterDestroy(EntryEvent event) {
synchronized (this) {
if (!event.getKey().equals(MARKER)) {
counterDestroy++;
}
}
}
});
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME1, attrs);
factory.setPoolName(createPool(host, "p2", port, true).getName());
attrs = factory.create();
cache.createRegion(REGION_NAME2, attrs);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class ConflationDUnitTest method createClientCache2CommonWriterTest3.
public static void createClientCache2CommonWriterTest3(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 ClientToServerDeltaDUnitTest method createClientCache.
/*
* create client cache
*/
public static void createClientCache(String host, Integer port, Boolean attachListener, Boolean isEmpty, Boolean isCq, String[] cqQueryString, Boolean registerInterestAll, Boolean enableSubscription) throws Exception {
updates = 0;
create = 0;
firstUpdate = null;
secondUpdate = null;
error = false;
lastKeyReceived = false;
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new ClientToServerDeltaDUnitTest().createCache(props);
pool = (PoolImpl) PoolManager.createFactory().addServer(host, port.intValue()).setThreadLocalConnections(true).setMinConnections(2).setSubscriptionEnabled(enableSubscription).setSubscriptionRedundancy(0).setReadTimeout(10000).setPingInterval(1000).setSocketBufferSize(32768).create("ClientToServerDeltaDunitTestPool");
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setConcurrencyChecksEnabled(true);
if (isEmpty) {
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
factory.setDataPolicy(DataPolicy.EMPTY);
} else {
factory.setDataPolicy(DataPolicy.NORMAL);
}
factory.setPoolName(pool.getName());
factory.setCloningEnabled(false);
// region with empty data policy
RegionAttributes attrs = factory.create();
region = cache.createRegion(REGION_NAME, attrs);
if (attachListener) {
region.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
create++;
if (LAST_KEY.equals(event.getKey())) {
lastKeyReceived = true;
}
;
}
@Override
public void afterUpdate(EntryEvent event) {
switch(updates) {
case 0:
// first delta
validateUpdates(event, firstUpdate, "FIRST");
updates++;
break;
case 1:
// combine delta
validateUpdates(event, firstUpdate, "FIRST");
validateUpdates(event, secondUpdate, "SECOND");
updates++;
break;
default:
break;
}
}
});
}
if (registerInterestAll) {
region.registerInterest("ALL_KEYS");
}
if (isCq) {
CqAttributesFactory cqf = new CqAttributesFactory();
CqListenerAdapter cqlist = new CqListenerAdapter() {
@Override
public void onEvent(CqEvent cqEvent) {
Object key = cqEvent.getKey();
if (LAST_KEY.equals(key)) {
lastKeyReceived = true;
}
logger.fine("CQ event received for (key, value): (" + key + ", " + cqEvent.getNewValue() + ")");
}
@Override
public void onError(CqEvent cqEvent) {
logger.fine("CQ error received for key: " + cqEvent.getKey());
}
};
cqf.addCqListener(cqlist);
CqAttributes cqa = cqf.create();
for (int i = 0; i < cqQueryString.length; i++) {
CqQuery cq = cache.getQueryService().newCq("Delta_Query_" + i, cqQueryString[i], cqa);
cq.execute();
}
}
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class P2PDeltaPropagationDUnitTest method createServerCache.
public static void createServerCache(Boolean flag, DataPolicy policy, Scope scope, Boolean listener, Boolean interestPolicyAll, Integer port) throws Exception {
P2PDeltaPropagationDUnitTest test = new P2PDeltaPropagationDUnitTest();
Properties props = new Properties();
if (!flag) {
props.setProperty(DELTA_PROPAGATION, "false");
}
cache = test.createCache(props);
AttributesFactory factory = new AttributesFactory();
factory.setScope(scope);
factory.setDataPolicy(policy);
if (policy == DataPolicy.EMPTY && interestPolicyAll) {
factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
}
if (listener) {
factory.addCacheListener(new CacheListenerAdapter() {
@SuppressWarnings("synthetic-access")
public void afterUpdate(EntryEvent event) {
numOfUpdates++;
cache.getLoggerI18n().fine("afterUpdate(): numOfUpdates = " + numOfUpdates);
cache.getLoggerI18n().fine("(key, val): " + event.getKey() + ", " + event.getNewValue());
if (event.getOldValue() != null) {
if (event.getOldValue() == event.getNewValue()) {
check = Boolean.TRUE;
}
}
if (((EntryEventImpl) event).getDeltaBytes() != null) {
cache.getLoggerI18n().fine("delta bytes received. " + hasDeltaBytes);
assertTrue("No full value received for event " + event, ((EntryEventImpl) event).getNewValue() != null);
hasDeltaBytes++;
} else {
cache.getLoggerI18n().fine("delta bytes not received.");
}
}
});
}
Region region = cache.createRegion(REGION_NAME, factory.create());
if (!policy.isReplicate()) {
region.create("KEY", "KEY");
}
if (port != null) {
CacheServer server1 = cache.addCacheServer();
server1.setPort(port);
server1.start();
}
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class HARegionJUnitTest method testEventIdSetForEvictDestroy.
/**
* Test to verify event id exists when evict destroy happens.
*/
@Test
public void testEventIdSetForEvictDestroy() throws Exception {
AttributesFactory factory = new AttributesFactory();
factory.setCacheListener(new CacheListenerAdapter() {
public void afterDestroy(EntryEvent event) {
assertTrue("eventId has not been set for " + event, ((EntryEventImpl) event).getEventId() != null);
}
});
EvictionAttributes evAttr = EvictionAttributes.createLRUEntryAttributes(1, EvictionAction.LOCAL_DESTROY);
factory.setEvictionAttributes(evAttr);
RegionAttributes attrs = factory.createRegionAttributes();
Region region = cache.createVMRegion("TEST_REGION", attrs);
region.put("key1", "value1");
region.put("key2", "value2");
}
Aggregations