Search in sources :

Example 1 with CacheListener

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

the class CacheXmlParser method endCacheListener.

/**
   * When a <code>cache-listener</code> element is finished, the {@link Parameter}s and class names
   * are popped off the stack. The cache listener is instantiated and initialized with the
   * parameters, if appropriate.
   */
private void endCacheListener() {
    Declarable d = createDeclarable();
    if (!(d instanceof CacheListener)) {
        throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_CACHELISTENER.toLocalizedString(d.getClass().getName()));
    }
    RegionAttributesCreation attrs = peekRegionAttributesContext(CACHE_LISTENER);
    attrs.addCacheListener((CacheListener) d);
}
Also used : Declarable(org.apache.geode.cache.Declarable) CacheXmlException(org.apache.geode.cache.CacheXmlException) CacheListener(org.apache.geode.cache.CacheListener)

Example 2 with CacheListener

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

the class RegionMBeanCompositeDataFactory method getRegionAttributesData.

public static RegionAttributesData getRegionAttributesData(RegionAttributes regAttrs) {
    String cacheLoaderClassName = null;
    if (regAttrs.getCacheLoader() != null) {
        cacheLoaderClassName = regAttrs.getCacheLoader().getClass().getCanonicalName();
    }
    String cacheWriteClassName = null;
    if (regAttrs.getCacheWriter() != null) {
        cacheWriteClassName = regAttrs.getCacheWriter().getClass().getCanonicalName();
    }
    String keyConstraintClassName = null;
    if (regAttrs.getKeyConstraint() != null) {
        keyConstraintClassName = regAttrs.getKeyConstraint().getName();
    }
    String valueContstraintClassName = null;
    if (regAttrs.getValueConstraint() != null) {
        valueContstraintClassName = regAttrs.getValueConstraint().getName();
    }
    CacheListener[] listeners = regAttrs.getCacheListeners();
    String[] cacheListeners = null;
    if (listeners != null && listeners.length > 0) {
        cacheListeners = new String[listeners.length];
        int j = 0;
        for (CacheListener l : listeners) {
            cacheListeners[j] = l.getClass().getName();
            j++;
        }
    } else {
        cacheListeners = ManagementConstants.NO_DATA_STRING;
    }
    int regionTimeToLive = regAttrs.getRegionTimeToLive().getTimeout();
    int regionIdleTimeout = regAttrs.getRegionIdleTimeout().getTimeout();
    int entryTimeToLive = regAttrs.getEntryTimeToLive().getTimeout();
    int entryIdleTimeout = regAttrs.getEntryIdleTimeout().getTimeout();
    String customEntryTimeToLive = null;
    Object o1 = regAttrs.getCustomEntryTimeToLive();
    if (o1 != null) {
        customEntryTimeToLive = o1.toString();
    }
    String customEntryIdleTimeout = null;
    Object o2 = regAttrs.getCustomEntryIdleTimeout();
    if (o2 != null) {
        customEntryIdleTimeout = o2.toString();
    }
    boolean ignoreJTA = regAttrs.getIgnoreJTA();
    String dataPolicy = regAttrs.getDataPolicy().toString();
    String scope = regAttrs.getScope().toString();
    int initialCapacity = regAttrs.getInitialCapacity();
    float loadFactor = regAttrs.getLoadFactor();
    boolean lockGrantor = regAttrs.isLockGrantor();
    boolean multicastEnabled = regAttrs.getMulticastEnabled();
    int concurrencyLevel = regAttrs.getConcurrencyLevel();
    boolean indexMaintenanceSynchronous = regAttrs.getIndexMaintenanceSynchronous();
    boolean statisticsEnabled = regAttrs.getStatisticsEnabled();
    boolean subsciptionConflationEnabled = regAttrs.getEnableSubscriptionConflation();
    boolean asyncConflationEnabled = regAttrs.getEnableAsyncConflation();
    String poolName = regAttrs.getPoolName();
    boolean isCloningEnabled = regAttrs.getCloningEnabled();
    String diskStoreName = regAttrs.getDiskStoreName();
    String interestPolicy = null;
    if (regAttrs.getSubscriptionAttributes() != null) {
        interestPolicy = regAttrs.getSubscriptionAttributes().getInterestPolicy().toString();
    }
    String compressorClassName = null;
    if (regAttrs.getCompressor() != null) {
        compressorClassName = regAttrs.getCompressor().getClass().getCanonicalName();
    }
    boolean diskSynchronus = regAttrs.isDiskSynchronous();
    boolean offheap = regAttrs.getOffHeap();
    RegionAttributesData regionAttributesData = new RegionAttributesData(cacheLoaderClassName, cacheWriteClassName, keyConstraintClassName, valueContstraintClassName, regionTimeToLive, regionIdleTimeout, entryTimeToLive, entryIdleTimeout, customEntryTimeToLive, customEntryIdleTimeout, ignoreJTA, dataPolicy, scope, initialCapacity, loadFactor, lockGrantor, multicastEnabled, concurrencyLevel, indexMaintenanceSynchronous, statisticsEnabled, subsciptionConflationEnabled, asyncConflationEnabled, poolName, isCloningEnabled, diskStoreName, interestPolicy, diskSynchronus, cacheListeners, compressorClassName, offheap);
    return regionAttributesData;
}
Also used : RegionAttributesData(org.apache.geode.management.RegionAttributesData) CacheListener(org.apache.geode.cache.CacheListener)

Example 3 with CacheListener

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

the class AbstractRegion method setCacheListener.

@Override
public CacheListener setCacheListener(CacheListener aListener) {
    checkReadiness();
    CacheListener[] oldListeners;
    synchronized (this.clSync) {
        oldListeners = this.cacheListeners;
        if (oldListeners != null && oldListeners.length > 1) {
            throw new IllegalStateException(LocalizedStrings.AbstractRegion_MORE_THAN_ONE_CACHE_LISTENER_EXISTS.toLocalizedString());
        }
        this.cacheListeners = new CacheListener[] { aListener };
    }
    // moved the following out of the sync for bug 34512
    CacheListener result = null;
    if (oldListeners != null && oldListeners.length > 0) {
        if (oldListeners.length == 1) {
            result = oldListeners[0];
        }
        for (CacheListener oldListener : oldListeners) {
            if (aListener != oldListener) {
                closeCacheCallback(oldListener);
            }
        }
        if (aListener == null) {
            cacheListenersChanged(false);
        }
    } else {
        // we have no old listeners
        if (aListener != null) {
            // we have added a new listener
            cacheListenersChanged(true);
        }
    }
    return result;
}
Also used : CacheListener(org.apache.geode.cache.CacheListener)

Example 4 with CacheListener

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

the class AbstractRegion method initPostCreateRegionMembershipListeners.

/**
   * Initialize any wrapped RegionMembershipListeners in the cache listener list
   */
void initPostCreateRegionMembershipListeners(Set initialMembers) {
    synchronized (this.clSync) {
        DistributedMember[] members = null;
        CacheListener[] newListeners = null;
        for (int i = 0; i < this.cacheListeners.length; i++) {
            CacheListener cl = this.cacheListeners[i];
            if (cl instanceof WrappedRegionMembershipListener) {
                WrappedRegionMembershipListener wrml = (WrappedRegionMembershipListener) cl;
                if (!wrml.isInitialized()) {
                    if (members == null) {
                        members = (DistributedMember[]) initialMembers.toArray(new DistributedMember[initialMembers.size()]);
                    }
                    wrml.initialMembers(this, members);
                    if (newListeners == null) {
                        newListeners = new CacheListener[this.cacheListeners.length];
                        System.arraycopy(this.cacheListeners, 0, newListeners, 0, newListeners.length);
                    }
                    newListeners[i] = wrml.getWrappedListener();
                }
            }
        }
        if (newListeners != null) {
            this.cacheListeners = newListeners;
        }
    }
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) CacheListener(org.apache.geode.cache.CacheListener) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint)

Example 5 with CacheListener

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

the class RegionAlterFunction method alterRegion.

private <K, V> Region<?, ?> alterRegion(Cache cache, RegionFunctionArgs regionAlterArgs) {
    final String regionPathString = regionAlterArgs.getRegionPath();
    RegionPath regionPath = new RegionPath(regionPathString);
    AbstractRegion region = (AbstractRegion) cache.getRegion(regionPathString);
    if (region == null) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.ALTER_REGION__MSG__REGION_DOESNT_EXIST_0, new Object[] { regionPath }));
    }
    AttributesMutator mutator = region.getAttributesMutator();
    if (regionAlterArgs.isCloningEnabled() != null) {
        mutator.setCloningEnabled(regionAlterArgs.isCloningEnabled());
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cloning");
        }
    }
    if (regionAlterArgs.getEvictionMax() != null) {
        mutator.getEvictionAttributesMutator().setMaximum(regionAlterArgs.getEvictionMax());
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - eviction attributes max");
        }
    }
    // Alter expiration attributes
    final RegionFunctionArgs.ExpirationAttrs newEntryExpirationIdleTime = regionAlterArgs.getEntryExpirationIdleTime();
    if (newEntryExpirationIdleTime != null) {
        mutator.setEntryIdleTimeout(parseExpirationAttributes(newEntryExpirationIdleTime, region.getEntryIdleTimeout()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - entry idle timeout");
        }
    }
    final RegionFunctionArgs.ExpirationAttrs newEntryExpirationTTL = regionAlterArgs.getEntryExpirationTTL();
    if (newEntryExpirationTTL != null) {
        mutator.setEntryTimeToLive(parseExpirationAttributes(newEntryExpirationTTL, region.getEntryTimeToLive()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - entry TTL");
        }
    }
    final RegionFunctionArgs.ExpirationAttrs newRegionExpirationIdleTime = regionAlterArgs.getRegionExpirationIdleTime();
    if (newRegionExpirationIdleTime != null) {
        mutator.setRegionIdleTimeout(parseExpirationAttributes(newRegionExpirationIdleTime, region.getRegionIdleTimeout()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - region idle timeout");
        }
    }
    final RegionFunctionArgs.ExpirationAttrs newRegionExpirationTTL = regionAlterArgs.getRegionExpirationTTL();
    if (newRegionExpirationTTL != null) {
        mutator.setRegionTimeToLive(parseExpirationAttributes(newRegionExpirationTTL, region.getRegionTimeToLive()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - region TTL");
        }
    }
    // Alter Gateway Sender Ids
    final Set<String> newGatewaySenderIds = regionAlterArgs.getGatewaySenderIds();
    if (newGatewaySenderIds != null) {
        // Remove old gateway sender ids that aren't in the new list
        Set<String> oldGatewaySenderIds = region.getGatewaySenderIds();
        if (!oldGatewaySenderIds.isEmpty()) {
            for (String gatewaySenderId : oldGatewaySenderIds) {
                if (!newGatewaySenderIds.contains(gatewaySenderId)) {
                    mutator.removeGatewaySenderId(gatewaySenderId);
                }
            }
        }
        // Add new gateway sender ids that don't already exist
        for (String gatewaySenderId : newGatewaySenderIds) {
            if (!oldGatewaySenderIds.contains(gatewaySenderId)) {
                mutator.addGatewaySenderId(gatewaySenderId);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - gateway sender IDs");
        }
    }
    // Alter Async Queue Ids
    final Set<String> newAsyncEventQueueIds = regionAlterArgs.getAsyncEventQueueIds();
    if (newAsyncEventQueueIds != null) {
        // Remove old async event queue ids that aren't in the new list
        Set<String> oldAsyncEventQueueIds = region.getAsyncEventQueueIds();
        if (!oldAsyncEventQueueIds.isEmpty()) {
            for (String asyncEventQueueId : oldAsyncEventQueueIds) {
                if (!newAsyncEventQueueIds.contains(asyncEventQueueId)) {
                    mutator.removeAsyncEventQueueId(asyncEventQueueId);
                }
            }
        }
        // Add new async event queue ids that don't already exist
        for (String asyncEventQueueId : newAsyncEventQueueIds) {
            if (!oldAsyncEventQueueIds.contains(asyncEventQueueId)) {
                mutator.addAsyncEventQueueId(asyncEventQueueId);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - async event queue IDs");
        }
    }
    // Alter Cache Listeners
    final Set<String> newCacheListenerNames = regionAlterArgs.getCacheListeners();
    if (newCacheListenerNames != null) {
        // Remove old cache listeners that aren't in the new list
        CacheListener[] oldCacheListeners = region.getCacheListeners();
        for (CacheListener oldCacheListener : oldCacheListeners) {
            if (!newCacheListenerNames.contains(oldCacheListener.getClass().getName())) {
                mutator.removeCacheListener(oldCacheListener);
            }
        }
        // Add new cache listeners that don't already exist
        for (String newCacheListenerName : newCacheListenerNames) {
            if (newCacheListenerName.isEmpty()) {
                continue;
            }
            boolean nameFound = false;
            for (CacheListener oldCacheListener : oldCacheListeners) {
                if (oldCacheListener.getClass().getName().equals(newCacheListenerName)) {
                    nameFound = true;
                    break;
                }
            }
            if (!nameFound) {
                Class<CacheListener<K, V>> cacheListenerKlass = forName(newCacheListenerName, CliStrings.ALTER_REGION__CACHELISTENER);
                mutator.addCacheListener(newInstance(cacheListenerKlass, CliStrings.ALTER_REGION__CACHELISTENER));
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cache listeners");
        }
    }
    final String cacheLoader = regionAlterArgs.getCacheLoader();
    if (cacheLoader != null) {
        if (cacheLoader.isEmpty()) {
            mutator.setCacheLoader(null);
        } else {
            Class<CacheLoader<K, V>> cacheLoaderKlass = forName(cacheLoader, CliStrings.ALTER_REGION__CACHELOADER);
            mutator.setCacheLoader(newInstance(cacheLoaderKlass, CliStrings.ALTER_REGION__CACHELOADER));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cache loader");
        }
    }
    final String cacheWriter = regionAlterArgs.getCacheWriter();
    if (cacheWriter != null) {
        if (cacheWriter.isEmpty()) {
            mutator.setCacheWriter(null);
        } else {
            Class<CacheWriter<K, V>> cacheWriterKlass = forName(cacheWriter, CliStrings.ALTER_REGION__CACHEWRITER);
            mutator.setCacheWriter(newInstance(cacheWriterKlass, CliStrings.ALTER_REGION__CACHEWRITER));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cache writer");
        }
    }
    return region;
}
Also used : AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) CacheListener(org.apache.geode.cache.CacheListener) RegionPath(org.apache.geode.management.internal.cli.util.RegionPath) CacheWriter(org.apache.geode.cache.CacheWriter) CacheLoader(org.apache.geode.cache.CacheLoader) 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