Post-transaction event listeners
zenluca Sep 18, 2012 1:50 PMHi,
I'm using Infinispan as a replicated cache (replicated/synchronous mode).
I need the cluster nodes to be informed when something changes in the cache and I used cache.addListener().
My problem is that in many cases I need my listeners to get notified when they can actually find the cache in the new state. I already pass the created/changed/removed object to them, but in many circumstances this is only triggering a "refresh" which involves reading the cache using get operations.
From the FAQ section of the documentation I understand this is not possible (https://docs.jboss.org/author/pages/viewpage.action?pageId=5832860) and I assume it is related with the transactional behaviour of Infinispan (that I'm not using, mostly for performance reasons).
To better contextualize my question, I'd like the following test case to pass:
public class SampleTest {
private Cache<String, String> cache;
@Listener
public class MyListener {
@CacheEntryModified
public void entryUpdated(CacheEntryModifiedEvent<String, String> event) {
if(!event.isPre()) {
assertSame(event.getValue(), cache.get(event.getKey()));
}
}
}
@Before
public void setup() throws Exception {
DefaultCacheManager cacheManager = new DefaultCacheManager(GlobalConfigurationBuilder.defaultClusteredBuilder().transport()
.build(), new ConfigurationBuilder().clustering().cacheMode(CacheMode.REPL_SYNC).sync().build());
cache = cacheManager.getCache();
}
@Test
public void must_notify_listeners_after_change_has_been_applied() {
cache.addListener(new MyListener());
cache.put("a", "a");
}
}
Is there a way to achieve this?
I already tried with an async listener, but the behaviour is quite random, depending (I guess) on thread interleaving it may work or not.
I also tried using transactions, my intention was to build a queue of notifications and fire them once I get the transactionComplete event, but this is quite complicated and bug prone and I verified it is not a viable solution because of my performance requirements.
Thank you very much.
-
SampleTest.java.zip 665 bytes