Search in sources :

Example 51 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class RemoteTransactionDUnitTest method doTestListeners.

private void doTestListeners(final OP op) {
    Host host = Host.getHost(0);
    VM acc = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(acc, datastore, 0);
    VM accessor = getVMForTransactions(acc, datastore);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region ref = getCache().getRegion(D_REFERENCE);
            ref.getAttributesMutator().addCacheListener(new TestCacheListener(true));
            ref.getAttributesMutator().setCacheWriter(new TestCacheWriter(true));
            Region cust = getCache().getRegion(CUSTOMER);
            cust.getAttributesMutator().addCacheListener(new TestCacheListener(true));
            cust.getAttributesMutator().setCacheWriter(new TestCacheWriter(true));
            Region order = getCache().getRegion(ORDER);
            order.getAttributesMutator().addCacheListener(new TestCacheListener(true));
            order.getAttributesMutator().setCacheWriter(new TestCacheWriter(true));
            getGemfireCache().getTxManager().addListener(new TestTxListener(true));
            if (!getGemfireCache().isClient()) {
                getGemfireCache().getTxManager().setWriter(new TestTxWriter(true));
            }
            return null;
        }
    });
    SerializableCallable addListenersToDataStore = new SerializableCallable() {

        public Object call() throws Exception {
            Region ref = getCache().getRegion(D_REFERENCE);
            ref.getAttributesMutator().addCacheListener(new TestCacheListener(false));
            ref.getAttributesMutator().setCacheWriter(new TestCacheWriter(false));
            Region cust = getCache().getRegion(CUSTOMER);
            cust.getAttributesMutator().addCacheListener(new TestCacheListener(false));
            cust.getAttributesMutator().setCacheWriter(new TestCacheWriter(false));
            Region order = getCache().getRegion(ORDER);
            order.getAttributesMutator().addCacheListener(new TestCacheListener(false));
            order.getAttributesMutator().setCacheWriter(new TestCacheWriter(false));
            getGemfireCache().getTxManager().addListener(new TestTxListener(false));
            if (!getGemfireCache().isClient()) {
                getGemfireCache().getTxManager().setWriter(new TestTxWriter(false));
            }
            return null;
        }
    };
    datastore.invoke(addListenersToDataStore);
    accessor.invoke(new DoOpsInTX(op));
    // Invalidate operations don't fire cache writers, so don't assert they were fired.
    if (op != OP.INVALIDATE) {
        // Ensure the cache writer was not fired in accessor
        accessor.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                Region cust = getCache().getRegion(CUSTOMER);
                assertFalse(((TestCacheWriter) cust.getAttributes().getCacheWriter()).wasFired);
                Region order = getCache().getRegion(ORDER);
                assertFalse(((TestCacheWriter) order.getAttributes().getCacheWriter()).wasFired);
                Region ref = getCache().getRegion(D_REFERENCE);
                assertFalse(((TestCacheWriter) ref.getAttributes().getCacheWriter()).wasFired);
                return null;
            }
        });
        // Ensure the cache writer was fired in the primary
        datastore.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                Region cust = getCache().getRegion(CUSTOMER);
                assertTrue(((TestCacheWriter) cust.getAttributes().getCacheWriter()).wasFired);
                Region order = getCache().getRegion(ORDER);
                assertTrue(((TestCacheWriter) order.getAttributes().getCacheWriter()).wasFired);
                Region ref = getCache().getRegion(D_REFERENCE);
                assertTrue(((TestCacheWriter) ref.getAttributes().getCacheWriter()).wasFired);
                return null;
            }
        });
    }
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.commit();
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TestTxListener l = (TestTxListener) getGemfireCache().getTxManager().getListener();
            assertTrue(l.isListenerInvoked());
            return null;
        }
    });
    SerializableCallable verifyListeners = new SerializableCallable() {

        public Object call() throws Exception {
            Region cust = getCache().getRegion(CUSTOMER);
            Region order = getCache().getRegion(ORDER);
            throwListenerException(cust);
            throwListenerException(order);
            throwWriterException(cust);
            throwWriterException(order);
            if (!getGemfireCache().isClient()) {
                throwTransactionCallbackException();
            }
            return null;
        }

        private void throwTransactionCallbackException() throws Exception {
            TestTxListener l = (TestTxListener) getGemfireCache().getTxManager().getListener();
            if (l.ex != null) {
                throw l.ex;
            }
            TestTxWriter w = (TestTxWriter) getGemfireCache().getTxManager().getWriter();
            if (w.ex != null) {
                throw w.ex;
            }
        }

        private void throwListenerException(Region r) throws Exception {
            Exception e = null;
            CacheListener listener = r.getAttributes().getCacheListeners()[0];
            if (listener instanceof TestCacheListener) {
                e = ((TestCacheListener) listener).ex;
            } else {
            // e = ((ClientListener)listener).???
            }
            if (e != null) {
                throw e;
            }
        }

        private void throwWriterException(Region r) throws Exception {
            Exception e = null;
            CacheListener listener = r.getAttributes().getCacheListeners()[0];
            if (listener instanceof TestCacheListener) {
                e = ((TestCacheListener) listener).ex;
            } else {
            // e = ((ClientListener)listener).???
            }
            if (e != null) {
                throw e;
            }
        }
    };
    accessor.invoke(verifyListeners);
    datastore.invoke(verifyListeners);
}
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) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region)

Example 52 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class HAValidateDuplicateListener method createClientCache.

public static void createClientCache(String hostName, Integer port1, Integer port2) throws Exception {
    int PORT1 = port1.intValue();
    int PORT2 = port2.intValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new HADuplicateDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    ClientServerTestCase.configureConnectionPool(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null);
    factory.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener clientListener = new HAValidateDuplicateListener();
    factory.setCacheListener(clientListener);
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
    Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    assertNotNull(region);
    region.registerInterest("ALL_KEYS", InterestResultPolicy.NONE);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CacheListener(org.apache.geode.cache.CacheListener)

Example 53 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class HAExpiryDUnitTest method createRegionQueue.

public static void createRegionQueue(Boolean isDurable) throws Exception {
    new HAExpiryDUnitTest().createCache(new Properties());
    HARegionQueueAttributes hattr = new HARegionQueueAttributes();
    // setting expiry time for the regionqueue.
    hattr.setExpiryTime(4);
    RegionQueue regionqueue = HARegionQueue.getHARegionQueueInstance(regionQueueName, cache, hattr, HARegionQueue.NON_BLOCKING_HA_QUEUE, isDurable.booleanValue());
    assertNotNull(regionqueue);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    CacheListener serverListener = new VMListener();
    factory.setCacheListener(serverListener);
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Properties(java.util.Properties) RegionQueue(org.apache.geode.internal.cache.RegionQueue) CacheListener(org.apache.geode.cache.CacheListener)

Example 54 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class vmListenerToCheckHARegionQueue method createRegionQueue.

public static void createRegionQueue() throws Exception {
    new HAGIIBugDUnitTest().createCache(new Properties());
    HARegionQueueAttributes hattr = new HARegionQueueAttributes();
    // setting expiry time for the regionqueue.
    hattr.setExpiryTime(12000000);
    RegionQueue regionqueue = HARegionQueue.getHARegionQueueInstance(regionQueueName, cache, hattr, HARegionQueue.NON_BLOCKING_HA_QUEUE, false);
    assertNotNull(regionqueue);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    CacheListener regionListener = new vmListenerToPutInHARegionQueue();
    factory.setCacheListener(regionListener);
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Properties(java.util.Properties) RegionQueue(org.apache.geode.internal.cache.RegionQueue) CacheListener(org.apache.geode.cache.CacheListener)

Example 55 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class WANTestBase method addCacheListenerOnQueueBucketRegion.

private void addCacheListenerOnQueueBucketRegion(String senderId, int numBuckets) {
    Set<GatewaySender> senders = cache.getGatewaySenders();
    GatewaySender sender = null;
    for (GatewaySender s : senders) {
        if (s.getId().equals(senderId)) {
            sender = s;
            break;
        }
    }
    RegionQueue parallelQueue = ((AbstractGatewaySender) sender).getQueues().toArray(new RegionQueue[1])[0];
    PartitionedRegion region = (PartitionedRegion) parallelQueue.getRegion();
    for (int i = 0; i < numBuckets; i++) {
        BucketRegion br = region.getBucketRegion(i);
        if (br != null) {
            AttributesMutator mutator = br.getAttributesMutator();
            CacheListener listener = new QueueListener();
            mutator.addCacheListener(listener);
        }
    }
}
Also used : GatewaySender(org.apache.geode.cache.wan.GatewaySender) BucketRegion(org.apache.geode.internal.cache.BucketRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RegionQueue(org.apache.geode.internal.cache.RegionQueue) CacheListener(org.apache.geode.cache.CacheListener) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Aggregations

CacheListener (org.apache.geode.cache.CacheListener)55 AttributesFactory (org.apache.geode.cache.AttributesFactory)28 Region (org.apache.geode.cache.Region)24 RegionAttributes (org.apache.geode.cache.RegionAttributes)17 EntryEvent (org.apache.geode.cache.EntryEvent)13 Test (org.junit.Test)13 CacheException (org.apache.geode.cache.CacheException)12 Properties (java.util.Properties)11 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)11 VM (org.apache.geode.test.dunit.VM)11 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)11 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)8 CacheWriter (org.apache.geode.cache.CacheWriter)7 AttributesMutator (org.apache.geode.cache.AttributesMutator)6 Host (org.apache.geode.test.dunit.Host)6 Iterator (java.util.Iterator)5 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)5 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)5 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)5 CacheLoader (org.apache.geode.cache.CacheLoader)4