use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class TestDiskRegion method main1.
public static void main1(String[] args) throws Exception {
DistributedSystem system = DistributedSystem.connect(new java.util.Properties());
Cache cache = CacheFactory.create(system);
AttributesFactory factory = new AttributesFactory();
factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK));
factory.setCacheListener(new CacheListenerAdapter() {
public void afterUpdate(EntryEvent event) {
System.out.println("UPDATE: " + event.getKey() + " -> (" + event.getOldValue() + " -> " + event.getNewValue() + ")");
}
});
LocalRegion region = (LocalRegion) cache.createRegion("TestDiskRegion", factory.create());
DiskRegion dr = region.getDiskRegion();
DiskRegionStats diskStats = dr.getStats();
LRUStatistics lruStats = getLRUStats(region);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Hit enter to perform action");
for (int i = 0; true; i++) {
br.readLine();
// Thread.sleep(500);
Object key = new Integer(i);
Object value = new byte[200000];
region.put(key, value);
System.out.println(key + " -> " + value + " evictions = " + lruStats.getEvictions() + ", writes = " + diskStats.getWrites());
}
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class RemoteTransactionDUnitTest method testBug45556.
@Test
public void testBug45556() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
final String name = getName();
class CountingListener extends CacheListenerAdapter {
private int count;
@Override
public void afterCreate(EntryEvent event) {
LogWriterUtils.getLogWriter().info("afterCreate invoked for " + event);
count++;
}
@Override
public void afterUpdate(EntryEvent event) {
LogWriterUtils.getLogWriter().info("afterUpdate invoked for " + event);
count++;
}
}
accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE_PROXY).create(name);
r.getAttributesMutator().addCacheListener(new CountingListener());
return null;
}
});
datastore.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(name);
r.getAttributesMutator().addCacheListener(new CountingListener());
r.put("key1", "value1");
return null;
}
});
final TransactionId txid = (TransactionId) accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region r = getCache().getRegion(name);
CacheTransactionManager tm = getCache().getCacheTransactionManager();
getCache().getLogger().fine("SWAP:BeginTX");
tm.begin();
r.put("txkey", "txvalue");
return tm.suspend();
}
});
datastore.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region rgn = getCache().getRegion(name);
assertNull(rgn.get("txkey"));
TXManagerImpl txMgr = getGemfireCache().getTxManager();
TXStateProxy tx = txMgr.getHostedTXState((TXId) txid);
assertEquals(1, tx.getRegions().size());
for (LocalRegion r : tx.getRegions()) {
assertTrue(r instanceof DistributedRegion);
TXRegionState rs = tx.readRegion(r);
for (Object key : rs.getEntryKeys()) {
TXEntryState es = rs.readEntry(key);
assertEquals("txkey", key);
assertNotNull(es.getValue(key, r, false));
if (key.equals("txkey"))
assertTrue(es.isDirty());
}
}
return null;
}
});
accessor.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region rgn = getCache().getRegion(name);
assertNull(rgn.get("txkey"));
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.resume(txid);
mgr.commit();
CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
assertEquals(0, cl.count);
assertEquals("txvalue", rgn.get("txkey"));
return null;
}
});
datastore.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region rgn = getCache().getRegion(name);
CountingListener cl = (CountingListener) rgn.getAttributes().getCacheListeners()[0];
assertEquals(2, cl.count);
return null;
}
});
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class RemoteTransactionDUnitTest method testNonInlineRemoteEvents.
/**
* Install Listeners and verify that they are invoked after all tx events have been applied to the
* cache see GEODE-278
*/
@Test
public void testNonInlineRemoteEvents() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String key1 = "nonInline-1";
final String key2 = "nonInline-2";
class NonInlineListener extends CacheListenerAdapter {
boolean assertException = false;
@Override
public void afterCreate(EntryEvent event) {
if (event.getKey().equals(key1)) {
if (getCache().getRegion(D_REFERENCE).get(key2) == null) {
assertException = true;
}
}
}
}
SerializableCallable createRegionWithListener = new SerializableCallable() {
@Override
public Object call() throws Exception {
createRegion(false, 0, null);
getCache().getRegion(D_REFERENCE).getAttributesMutator().addCacheListener(new NonInlineListener());
return null;
}
};
vm0.invoke(createRegionWithListener);
vm1.invoke(createRegionWithListener);
vm0.invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
Region region = getCache().getRegion(D_REFERENCE);
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.begin();
region.put(key1, "nonInlineValue-1");
region.put(key2, "nonInlineValue-2");
mgr.commit();
return null;
}
});
SerializableCallable verifyAssert = new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheListener[] listeners = getCache().getRegion(D_REFERENCE).getAttributes().getCacheListeners();
for (CacheListener listener : listeners) {
if (listener instanceof NonInlineListener) {
NonInlineListener l = (NonInlineListener) listener;
assertFalse(l.assertException);
}
}
return null;
}
};
vm0.invoke(verifyAssert);
vm1.invoke(verifyAssert);
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class FailoverDUnitTest method createClientCache.
public static void createClientCache(String hostName, Integer port1, Integer port2) throws Exception {
PORT1 = port1.intValue();
PORT2 = port2.intValue();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new FailoverDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
ClientServerTestCase.configureConnectionPoolWithName(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null, "FailoverPool");
factory.setCacheListener(new CacheListenerAdapter() {
public void afterUpdate(EntryEvent event) {
synchronized (this) {
cache.getLogger().info("Event Received : key..." + event.getKey());
cache.getLogger().info("Event Received : value..." + event.getNewValue());
}
}
});
cache.createRegion(regionName, factory.create());
}
use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.
the class Bug36853EventsExpiryDUnitTest method createClientCache.
/**
* Creates the client cache
*
* @param hostName the name of the server's machine
* @param port - bridgeserver port
* @throws Exception - thrown if any problem occurs in setting up the client
*/
private static void createClientCache(String hostName, Integer port) throws Exception {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new Bug36853EventsExpiryDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
ClientServerTestCase.configureConnectionPool(factory, hostName, port.intValue(), -1, true, -1, 2, null);
factory.addCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
String key = (String) event.getKey();
LogWriterUtils.getLogWriter().info("client2 : afterCreate : key =" + key);
if (key.equals(LAST_KEY)) {
synchronized (Bug36853EventsExpiryDUnitTest.class) {
LogWriterUtils.getLogWriter().info("Notifying client2 to proceed for validation");
proceedForValidation = true;
Bug36853EventsExpiryDUnitTest.class.notify();
}
} else {
putsRecievedByClient++;
}
}
});
RegionAttributes attrs = factory.create();
Region region = cache.createRegion(REGION_NAME, attrs);
region.registerInterest("ALL_KEYS");
}
Aggregations