use of org.apache.geode.cache.AttributesMutator in project geode by apache.
the class SnapshotDUnitTest method testExportAndImportWithInvokeCallbacksEnabled.
@Test
public void testExportAndImportWithInvokeCallbacksEnabled() throws Exception {
File dir = new File(getDiskDirs()[0], "callbacks");
dir.mkdir();
// save all regions
CacheSnapshotService service = getCache().getSnapshotService();
service.save(dir, SnapshotFormat.GEMFIRE);
// update regions with data to be overwritten by import
updateRegions();
SerializableCallable callbacks = new SerializableCallable() {
@Override
public Object call() throws Exception {
for (final RegionType rt : RegionType.values()) {
for (final SerializationType st : SerializationType.values()) {
String name = "test-" + rt.name() + "-" + st.name();
Cache cache = getCache();
Region<Integer, MyObject> region = cache.getRegion(name);
// add CacheWriter and CacheListener
AttributesMutator mutator = region.getAttributesMutator();
mutator.setCacheWriter(new CountingCacheWriter());
mutator.addCacheListener(new CountingCacheListener());
// add AsyncEventQueue
addAsyncEventQueue(region, name);
}
}
return null;
}
};
// add callbacks
forEachVm(callbacks, true);
// load all regions with invoke callbacks enabled
SnapshotOptions options = service.createOptions();
options.invokeCallbacks(true);
loadRegions(dir, options);
// verify callbacks were invoked
verifyCallbacksInvoked();
}
use of org.apache.geode.cache.AttributesMutator in project geode by apache.
the class ParallelGatewaySenderQueue method addCacheListener.
@Override
public void addCacheListener(CacheListener listener) {
for (PartitionedRegion prQ : this.userRegionNameToshadowPRMap.values()) {
AttributesMutator mutator = prQ.getAttributesMutator();
mutator.addCacheListener(listener);
}
}
use of org.apache.geode.cache.AttributesMutator 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;
}
use of org.apache.geode.cache.AttributesMutator in project geode by apache.
the class IndexMaintenanceJUnitTest method testIndexMaintenanceOnCacheLoadedData.
/**
* Tests Index maintenance on data loaded via cache loader
*/
@Test
public void testIndexMaintenanceOnCacheLoadedData() {
try {
IndexManager.TEST_RANGEINDEX_ONLY = true;
Cache cache = CacheUtils.getCache();
qs = cache.getQueryService();
region = CacheUtils.createRegion("portfolio1", null);
AttributesMutator am = region.getAttributesMutator();
am.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
String key = (String) helper.getKey();
Portfolio p = new Portfolio(Integer.parseInt(key));
return p;
}
public void close() {
// TODO Auto-generated method stub
}
});
Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID()", "/portfolio1 pf");
List keys = new ArrayList();
keys.add("1");
keys.add("2");
keys.add("3");
keys.add("4");
region.getAll(keys);
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
}
}
use of org.apache.geode.cache.AttributesMutator in project geode by apache.
the class CacheXml66DUnitTest method testMultipleCacheListener.
/**
* Tests multiple cache listeners on one region
*
* @since GemFire 5.0
*/
@Test
public void testMultipleCacheListener() throws Exception {
CacheCreation cache = new CacheCreation();
RegionAttributesCreation attrs = new RegionAttributesCreation(cache);
CacheListener l1 = new MyTestCacheListener();
CacheListener l2 = new MySecondTestCacheListener();
attrs.addCacheListener(l1);
attrs.addCacheListener(l2);
cache.createRegion("root", attrs);
testXml(cache);
{
Cache c = getCache();
Region r = c.getRegion("root");
assertEquals(Arrays.asList(new CacheListener[] { l1, l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
AttributesMutator am = r.getAttributesMutator();
am.removeCacheListener(l2);
assertEquals(Arrays.asList(new CacheListener[] { l1 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.removeCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] {}), Arrays.asList(r.getAttributes().getCacheListeners()));
am.addCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l1 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.addCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l1 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.addCacheListener(l2);
assertEquals(Arrays.asList(new CacheListener[] { l1, l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.removeCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.removeCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.initCacheListeners(new CacheListener[] { l1, l2 });
assertEquals(Arrays.asList(new CacheListener[] { l1, l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
}
}
Aggregations