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 RemoteTransactionDUnitTest method testPRTXGetOnLocalWithLoader.
@Test
public void testPRTXGetOnLocalWithLoader() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesMutator am = getCache().getRegion(CUSTOMER).getAttributesMutator();
am.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return new Customer("sup dawg", "addr");
}
public void close() {
}
});
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.begin();
Region cust = getCache().getRegion(CUSTOMER);
CustId custId = new CustId(6);
Customer s = (Customer) cust.get(custId);
mgr.commit();
Customer s2 = (Customer) cust.get(custId);
Customer expectedCust = new Customer("sup dawg", "addr");
assertEquals(s, expectedCust);
assertEquals(s2, expectedCust);
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
return null;
}
});
}
use of org.apache.geode.cache.AttributesMutator in project geode by apache.
the class RemoteTransactionDUnitTest method testPRTXGetOnRemoteWithLoader.
@Test
public void testPRTXGetOnRemoteWithLoader() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesMutator am = getCache().getRegion(CUSTOMER).getAttributesMutator();
am.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return new Customer("sup dawg", "add");
}
public void close() {
}
});
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.begin();
Region cust = getCache().getRegion(CUSTOMER);
Customer s = (Customer) cust.get(new CustId(8));
assertEquals(new Customer("sup dawg", "add"), s);
assertTrue(cust.containsKey(new CustId(8)));
TXStateProxy tx = ((TXManagerImpl) mgr).internalSuspend();
assertFalse(cust.containsKey(new CustId(8)));
((TXManagerImpl) mgr).internalResume(tx);
mgr.commit();
Customer s2 = (Customer) cust.get(new CustId(8));
Customer ex = new Customer("sup dawg", "add");
assertEquals(ex, s);
assertEquals(ex, s2);
return null;
}
});
}
Aggregations