Skip to content

Instantly share code, notes, and snippets.

@pferraro
Created September 22, 2016 16:44
Show Gist options
  • Save pferraro/8fe91fb564d13a3d98fc76216dae13c4 to your computer and use it in GitHub Desktop.
Save pferraro/8fe91fb564d13a3d98fc76216dae13c4 to your computer and use it in GitHub Desktop.
@Listener
public class Test {
@org.junit.Test
public void test() throws InterruptedException {
GlobalConfiguration global1 = new GlobalConfigurationBuilder().transport().defaultTransport().addProperty("configurationFile", "fast.xml").globalJmxStatistics().allowDuplicateDomains(true).build();
GlobalConfiguration global2 = new GlobalConfigurationBuilder().transport().defaultTransport().addProperty("configurationFile", "fast.xml").globalJmxStatistics().allowDuplicateDomains(true).build();
Configuration config = new ConfigurationBuilder().clustering().cacheMode(CacheMode.REPL_SYNC).transaction().transactionMode(TransactionMode.NON_TRANSACTIONAL).expiration().wakeUpInterval(5, TimeUnit.SECONDS).build();
EmbeddedCacheManager manager1 = new DefaultCacheManager(global1, config, false);
manager1.start();
try {
EmbeddedCacheManager manager2 = new DefaultCacheManager(global2, config, false);
manager2.start();
try {
Cache<Integer, String> cache1 = manager1.getCache();
cache1.start();
cache1.addListener(this);
try {
Cache<Integer, String> cache2 = manager2.getCache();
cache2.start();
cache2.addListener(this);
try {
cache1.getAdvancedCache().put(1, "test", -1L, TimeUnit.SECONDS, 2, TimeUnit.SECONDS);
System.out.println("Value in local cache: " + cache1.get(1));
System.out.println("Value in remote cache: " + cache2.get(1));
for (int i = 1; i < 10; ++i) {
TimeUnit.SECONDS.sleep(1);
System.out.println("Access #" + i);
System.out.println("Value in local cache: " + cache1.get(1));
}
System.out.println("Value in remote cache: " + cache2.get(1));
} finally {
cache2.removeListener(this);
cache2.stop();
}
} finally {
cache1.removeListener(this);
cache1.stop();
}
} finally {
manager2.stop();
}
} finally {
manager1.stop();
}
}
@CacheEntryVisited
public void visited(CacheEntryEvent<Integer, String> event) {
System.out.println(event);
}
@CacheEntryExpired
@CacheEntryRemoved
public void removed(CacheEntryEvent<Integer, String> event) {
System.out.println(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment