Search in sources :

Example 26 with CacheListenerAdapter

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());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DiskRegionStats(org.apache.geode.internal.cache.DiskRegionStats) ObjectSizer(org.apache.geode.cache.util.ObjectSizer) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedSystem(org.apache.geode.distributed.DistributedSystem) AttributesFactory(org.apache.geode.cache.AttributesFactory) DiskRegion(org.apache.geode.internal.cache.DiskRegion) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) BufferedReader(java.io.BufferedReader) Cache(org.apache.geode.cache.Cache)

Example 27 with CacheListenerAdapter

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;
        }
    });
}
Also used : Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionId(org.apache.geode.cache.TransactionId) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 28 with CacheListenerAdapter

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);
}
Also used : Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 29 with CacheListenerAdapter

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());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Example 30 with CacheListenerAdapter

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");
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Aggregations

CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)66 EntryEvent (org.apache.geode.cache.EntryEvent)55 AttributesFactory (org.apache.geode.cache.AttributesFactory)40 Region (org.apache.geode.cache.Region)30 Test (org.junit.Test)25 RegionAttributes (org.apache.geode.cache.RegionAttributes)21 Properties (java.util.Properties)20 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)15 VM (org.apache.geode.test.dunit.VM)14 CacheException (org.apache.geode.cache.CacheException)11 CacheListener (org.apache.geode.cache.CacheListener)11 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 Host (org.apache.geode.test.dunit.Host)9 RegionEvent (org.apache.geode.cache.RegionEvent)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)7 CacheServer (org.apache.geode.cache.server.CacheServer)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 SubscriptionAttributes (org.apache.geode.cache.SubscriptionAttributes)6