use of org.apache.geode.cache.util.TransactionListenerAdapter in project geode by apache.
the class TXJUnitTest method testNoopInvalidates.
@Test
public void testNoopInvalidates() throws CacheException {
final CachePerfStats stats = this.cache.getCachePerfStats();
TransactionListener tl = new TransactionListenerAdapter() {
@Override
public void afterRollback(TransactionEvent event) {
te = event;
}
};
this.txMgr.addListener(tl);
// Make sure invalidates done on invalid entries are noops
{
// distributed invalidate
// first make sure invalidate is counted as a change
int txRollbackChanges = stats.getTxRollbackChanges();
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.invalidate("key1");
this.txMgr.rollback();
assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
assertEquals(1, te.getEvents().size());
this.region.destroy("key1");
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.invalidate("key1");
this.txMgr.commit();
assertEquals(1, te.getEvents().size());
this.region.destroy("key1");
// now make sure a committed entry that is invalid is not counted as a change
txRollbackChanges = stats.getTxRollbackChanges();
this.region.create("key1", "value1");
this.region.invalidate("key1");
this.txMgr.begin();
this.region.invalidate("key1");
this.txMgr.rollback();
assertEquals(txRollbackChanges, stats.getTxRollbackChanges());
assertEquals(0, te.getEvents().size());
this.region.destroy("key1");
this.region.create("key1", "value1");
this.region.invalidate("key1");
this.txMgr.begin();
this.region.invalidate("key1");
this.txMgr.commit();
assertEquals(0, te.getEvents().size());
this.region.destroy("key1");
// now make sure that multiple invalidates of same entry are a single change
txRollbackChanges = stats.getTxRollbackChanges();
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.invalidate("key1");
this.region.invalidate("key1");
this.region.invalidate("key1");
this.txMgr.rollback();
assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
assertEquals(1, te.getEvents().size());
this.region.destroy("key1");
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.invalidate("key1");
this.region.invalidate("key1");
this.region.invalidate("key1");
this.txMgr.commit();
assertEquals(1, te.getEvents().size());
this.region.destroy("key1");
}
{
// local invalidate
// first make sure invalidate is counted as a change
int txRollbackChanges = stats.getTxRollbackChanges();
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.localInvalidate("key1");
this.txMgr.rollback();
assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
this.region.destroy("key1");
// now make sure a committed entry that is invalid is not counted as a change
txRollbackChanges = stats.getTxRollbackChanges();
this.region.create("key1", "value1");
this.region.localInvalidate("key1");
this.txMgr.begin();
this.region.localInvalidate("key1");
this.txMgr.rollback();
assertEquals(txRollbackChanges, stats.getTxRollbackChanges());
this.region.destroy("key1");
// now make sure that multiple localInvalidates of same entry are a single change
txRollbackChanges = stats.getTxRollbackChanges();
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.localInvalidate("key1");
this.region.localInvalidate("key1");
this.region.localInvalidate("key1");
this.txMgr.rollback();
assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
this.region.destroy("key1");
}
}
use of org.apache.geode.cache.util.TransactionListenerAdapter in project geode by apache.
the class CallbackArgDUnitTest method doCommitOtherVm.
private void doCommitOtherVm() {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
public void run2() throws CacheException {
AttributesFactory af = new AttributesFactory();
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(callbackArg, e.getCallbackArgument());
}
};
af.addCacheListener(cl1);
af.setScope(Scope.DISTRIBUTED_ACK);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
Region r3 = r2.createSubregion("r3", af.create());
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
}
};
ctm.addListener(tl1);
ctm.begin();
r2.put("b", "value1", callbackArg);
r3.put("c", "value2", callbackArg);
r1.put("a", "value3", callbackArg);
r1.put("a2", "value4", callbackArg);
r3.put("c2", "value5", callbackArg);
r2.put("b2", "value6", callbackArg);
ctm.commit();
}
});
}
use of org.apache.geode.cache.util.TransactionListenerAdapter in project geode by apache.
the class ClientServerTransactionDUnitTest method testCallbacks.
// Disabled due to bug 47083
@Ignore
@Test
public void testCallbacks() {
Host host = Host.getHost(0);
VM datastore = host.getVM(1);
VM client = host.getVM(2);
int port = createRegionsAndStartServer(datastore, false);
createClientRegion(client, port, true);
class ClientTxListener extends TransactionListenerAdapter {
private boolean afterRollbackInvoked = false;
boolean afterCommitInvoked = false;
@Override
public void afterCommit(TransactionEvent event) {
afterCommitInvoked = true;
verifyEvent(event);
}
@Override
public void afterRollback(TransactionEvent event) {
afterRollbackInvoked = true;
verifyEvent(event);
}
protected void verifyEvent(TransactionEvent event) {
Iterator it = event.getEvents().iterator();
int i = 0;
while (it.hasNext()) {
EntryEvent ev = (EntryEvent) it.next();
if (i == 0)
assertNull(ev.getNewValue());
if (i > 1) {
assertEquals(new CustId(i), ev.getKey());
assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
}
assertTrue(ev.isOriginRemote());
i++;
}
assertEquals(5, event.getEvents().size());
}
}
class ClientTxWriter implements TransactionWriter {
boolean txWriterCalled = false;
public void close() {
}
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
txWriterCalled = true;
Iterator it = event.getEvents().iterator();
int i = 0;
while (it.hasNext()) {
EntryEvent ev = (EntryEvent) it.next();
if (i == 0)
assertNull(ev.getNewValue());
if (i > 1) {
assertEquals(new CustId(i), ev.getKey());
assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
}
assertTrue(ev.isOriginRemote());
i++;
}
assertEquals(5, event.getEvents().size());
}
}
class ClientListener extends CacheListenerAdapter {
boolean invoked = false;
@Override
public void afterCreate(EntryEvent event) {
CustId c = (CustId) event.getKey();
if (c.getCustId() > 1) {
invoked = true;
}
// we document that client transaction are proxied to the server
// and that the callback should be handled appropriately (hence true)
assertTrue(event.isOriginRemote());
assertTrue(event.isOriginRemote());
}
@Override
public void afterUpdate(EntryEvent event) {
assertFalse(event.isOriginRemote());
}
}
class ClientWriter extends CacheWriterAdapter {
@Override
public void beforeCreate(EntryEvent event) throws CacheWriterException {
CustId c = (CustId) event.getKey();
if (c.getCustId() < 2) {
return;
}
fail("cache writer should not be invoked");
}
@Override
public void beforeUpdate(EntryEvent event) throws CacheWriterException {
fail("cache writer should not be invoked");
}
}
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.addListener(new ClientTxListener());
mgr.setWriter(new ClientTxWriter());
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
pr.getAttributesMutator().addCacheListener(new ServerListener());
pr.getAttributesMutator().setCacheWriter(new ServerWriter());
return null;
}
});
client.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
mgr.addListener(new ClientTxListener());
try {
mgr.setWriter(new ClientTxWriter());
fail("expected exception not thrown");
} catch (IllegalStateException e) {
}
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
pr.getAttributesMutator().addCacheListener(new ClientListener());
pr.getAttributesMutator().setCacheWriter(new ClientWriter());
return null;
}
});
class doOps extends SerializableCallable {
public doOps(boolean commit) {
this.commit = commit;
}
boolean commit = false;
public Object call() throws Exception {
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
CacheTransactionManager mgr = getCache().getCacheTransactionManager();
pr.put(new CustId(0), new Customer("name0", "address0"));
pr.put(new CustId(1), new Customer("name1", "address1"));
mgr.begin();
pr.invalidate(new CustId(0));
pr.destroy(new CustId(1));
for (int i = 2; i < 5; i++) {
pr.put(new CustId(i), new Customer("name" + i, "address" + i));
}
if (commit) {
mgr.commit();
} else {
mgr.rollback();
}
return null;
}
}
client.invoke(new doOps(false));
datastore.invoke(new SerializableCallable() {
@SuppressWarnings("synthetic-access")
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
assertTrue(l.afterRollbackInvoked);
return null;
}
});
client.invoke(new doOps(true));
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
assertTrue(l.afterCommitInvoked);
ClientTxWriter w = (ClientTxWriter) mgr.getWriter();
assertTrue(w.txWriterCalled);
return null;
}
});
client.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
assertFalse(l.afterCommitInvoked);
Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
ClientListener cl = (ClientListener) pr.getAttributes().getCacheListeners()[0];
assertTrue(cl.invoked);
return null;
}
});
}
use of org.apache.geode.cache.util.TransactionListenerAdapter in project geode by apache.
the class CallbackArgDUnitTest method doTest.
private void doTest() throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertEquals(getCurrentExpectedKey(), e.getKey());
assertEquals(callbackArg, e.getCallbackArgument());
assertEquals(true, e.isCallbackArgumentAvailable());
}
};
af.addCacheListener(cl1);
Region r1 = createRootRegion("r1", af.create());
Region r2 = r1.createSubregion("r2", af.create());
r2.createSubregion("r3", af.create());
TransactionListener tl1 = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
assertEquals(6, e.getEvents().size());
ArrayList keys = new ArrayList();
Iterator it = e.getEvents().iterator();
while (it.hasNext()) {
EntryEvent ee = (EntryEvent) it.next();
keys.add(ee.getKey());
assertEquals(callbackArg, ee.getCallbackArgument());
assertEquals(true, ee.isCallbackArgumentAvailable());
}
assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
CallbackArgDUnitTest.this.invokeCount = 1;
}
};
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ctm.addListener(tl1);
this.invokeCount = 0;
this.clCount = 0;
this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
doCommitOtherVm();
assertEquals(1, this.invokeCount);
assertEquals(6, this.clCount);
}
use of org.apache.geode.cache.util.TransactionListenerAdapter in project geode by apache.
the class ProxyJUnitTest method setCallbacks.
private void setCallbacks(AttributesFactory af) {
CacheListener cl1 = new CacheListener() {
public void afterUpdate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterCreate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterInvalidate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterDestroy(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionInvalidate(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionDestroy(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionClear(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionCreate(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionLive(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void close() {
clClosed = true;
}
};
CacheWriter cw = new CacheWriter() {
public void beforeUpdate(EntryEvent e) throws CacheWriterException {
cwLastEvent = e;
cwInvokeCount++;
}
public void beforeCreate(EntryEvent e) throws CacheWriterException {
cwLastEvent = e;
cwInvokeCount++;
}
public void beforeDestroy(EntryEvent e) throws CacheWriterException {
cwLastEvent = e;
cwInvokeCount++;
}
public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
cwLastEvent = e;
cwInvokeCount++;
}
public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
cwLastEvent = e;
cwInvokeCount++;
}
public void close() {
cwClosed = true;
}
};
af.addCacheListener(cl1);
af.setCacheWriter(cw);
{
TransactionListener tl = new TransactionListenerAdapter() {
public void afterCommit(TransactionEvent e) {
tlLastEvents = e.getEvents();
tlInvokeCount++;
}
public void close() {
tlClosed = true;
}
;
};
CacheTransactionManager ctm = this.c.getCacheTransactionManager();
ctm.addListener(tl);
}
}
Aggregations