BAEL-1368: infinispan article (#3633)
This commit is contained in:
committed by
Predrag Maric
parent
1c043dc202
commit
6eeff13c45
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.infinispan;
|
||||
|
||||
import com.baeldung.infinispan.listener.CacheListener;
|
||||
import org.infinispan.Cache;
|
||||
import org.infinispan.configuration.cache.Configuration;
|
||||
import org.infinispan.configuration.cache.ConfigurationBuilder;
|
||||
import org.infinispan.eviction.EvictionType;
|
||||
import org.infinispan.manager.DefaultCacheManager;
|
||||
import org.infinispan.transaction.LockingMode;
|
||||
import org.infinispan.transaction.TransactionMode;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CacheConfiguration {
|
||||
|
||||
public static final String SIMPLE_HELLO_WORLD_CACHE = "simple-hello-world-cache";
|
||||
public static final String EXPIRING_HELLO_WORLD_CACHE = "expiring-hello-world-cache";
|
||||
public static final String EVICTING_HELLO_WORLD_CACHE = "evicting-hello-world-cache";
|
||||
public static final String PASSIVATING_HELLO_WORLD_CACHE = "passivating-hello-world-cache";
|
||||
public static final String TRANSACTIONAL_CACHE = "transactional-cache";
|
||||
|
||||
public DefaultCacheManager cacheManager() {
|
||||
DefaultCacheManager cacheManager = new DefaultCacheManager();
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
public Cache<String, Integer> transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) {
|
||||
return this.buildCache(TRANSACTIONAL_CACHE, cacheManager, listener, transactionalConfiguration());
|
||||
}
|
||||
|
||||
public Cache<String, String> simpleHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
|
||||
return this.buildCache(SIMPLE_HELLO_WORLD_CACHE, cacheManager, listener, new ConfigurationBuilder().build());
|
||||
}
|
||||
|
||||
public Cache<String, String> expiringHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
|
||||
return this.buildCache(EXPIRING_HELLO_WORLD_CACHE, cacheManager, listener, expiringConfiguration());
|
||||
}
|
||||
|
||||
public Cache<String, String> evictingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
|
||||
return this.buildCache(EVICTING_HELLO_WORLD_CACHE, cacheManager, listener, evictingConfiguration());
|
||||
}
|
||||
|
||||
public Cache<String, String> passivatingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
|
||||
return this.buildCache(PASSIVATING_HELLO_WORLD_CACHE, cacheManager, listener, passivatingConfiguration());
|
||||
}
|
||||
|
||||
private <K, V> Cache<K, V> buildCache(String cacheName, DefaultCacheManager cacheManager,
|
||||
CacheListener listener, Configuration configuration) {
|
||||
|
||||
cacheManager.defineConfiguration(cacheName, configuration);
|
||||
Cache<K, V> cache = cacheManager.getCache(cacheName);
|
||||
cache.addListener(listener);
|
||||
return cache;
|
||||
}
|
||||
|
||||
private Configuration expiringConfiguration() {
|
||||
return new ConfigurationBuilder().expiration().lifespan(1, TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Configuration evictingConfiguration() {
|
||||
return new ConfigurationBuilder()
|
||||
.memory().evictionType(EvictionType.COUNT).size(1)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Configuration passivatingConfiguration() {
|
||||
return new ConfigurationBuilder()
|
||||
.memory().evictionType(EvictionType.COUNT).size(1)
|
||||
.persistence()
|
||||
.passivation(true)
|
||||
.addSingleFileStore()
|
||||
.purgeOnStartup(true)
|
||||
.location(System.getProperty("java.io.tmpdir"))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Configuration transactionalConfiguration() {
|
||||
return new ConfigurationBuilder()
|
||||
.transaction().transactionMode(TransactionMode.TRANSACTIONAL)
|
||||
.lockingMode(LockingMode.PESSIMISTIC)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.infinispan.listener;
|
||||
|
||||
import org.infinispan.notifications.Listener;
|
||||
import org.infinispan.notifications.cachelistener.annotation.*;
|
||||
import org.infinispan.notifications.cachelistener.event.*;
|
||||
|
||||
@Listener
|
||||
public class CacheListener {
|
||||
|
||||
@CacheEntryCreated
|
||||
public void entryCreated(CacheEntryCreatedEvent<String, String> event) {
|
||||
this.printLog("Adding key '" + event.getKey() + "' to cache", event);
|
||||
}
|
||||
|
||||
@CacheEntryExpired
|
||||
public void entryExpired(CacheEntryExpiredEvent<String, String> event) {
|
||||
this.printLog("Expiring key '" + event.getKey() + "' from cache", event);
|
||||
}
|
||||
|
||||
@CacheEntryVisited
|
||||
public void entryVisited(CacheEntryVisitedEvent<String, String> event) {
|
||||
this.printLog("Key '" + event.getKey() + "' was visited", event);
|
||||
}
|
||||
|
||||
@CacheEntryActivated
|
||||
public void entryActivated(CacheEntryActivatedEvent<String, String> event) {
|
||||
this.printLog("Activating key '" + event.getKey() + "' on cache", event);
|
||||
}
|
||||
|
||||
@CacheEntryPassivated
|
||||
public void entryPassivated(CacheEntryPassivatedEvent<String, String> event) {
|
||||
this.printLog("Passivating key '" + event.getKey() + "' from cache", event);
|
||||
}
|
||||
|
||||
@CacheEntryLoaded
|
||||
public void entryLoaded(CacheEntryLoadedEvent<String, String> event) {
|
||||
this.printLog("Loading key '" + event.getKey() + "' to cache", event);
|
||||
}
|
||||
|
||||
@CacheEntriesEvicted
|
||||
public void entriesEvicted(CacheEntriesEvictedEvent<String, String> event) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
event.getEntries().entrySet().forEach((e) ->
|
||||
builder.append(e.getKey() + ", ")
|
||||
);
|
||||
System.out.println("Evicting following entries from cache: " + builder.toString());
|
||||
}
|
||||
|
||||
private void printLog(String log, CacheEntryEvent event) {
|
||||
if (!event.isPre()) {
|
||||
System.out.println(log);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.infinispan.repository;
|
||||
|
||||
public class HelloWorldRepository {
|
||||
|
||||
public String getHelloWorld() {
|
||||
try {
|
||||
System.out.println("Executing some heavy query");
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.infinispan.service;
|
||||
|
||||
import com.baeldung.infinispan.listener.CacheListener;
|
||||
import com.baeldung.infinispan.repository.HelloWorldRepository;
|
||||
import org.infinispan.Cache;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HelloWorldService {
|
||||
|
||||
private final HelloWorldRepository repository;
|
||||
|
||||
private final Cache<String, String> simpleHelloWorldCache;
|
||||
private final Cache<String, String> expiringHelloWorldCache;
|
||||
private final Cache<String, String> evictingHelloWorldCache;
|
||||
private final Cache<String, String> passivatingHelloWorldCache;
|
||||
|
||||
public HelloWorldService(HelloWorldRepository repository, CacheListener listener,
|
||||
Cache<String, String> simpleHelloWorldCache,
|
||||
Cache<String, String> expiringHelloWorldCache,
|
||||
Cache<String, String> evictingHelloWorldCache,
|
||||
Cache<String, String> passivatingHelloWorldCache) {
|
||||
|
||||
this.repository = repository;
|
||||
|
||||
this.simpleHelloWorldCache = simpleHelloWorldCache;
|
||||
this.expiringHelloWorldCache = expiringHelloWorldCache;
|
||||
this.evictingHelloWorldCache = evictingHelloWorldCache;
|
||||
this.passivatingHelloWorldCache = passivatingHelloWorldCache;
|
||||
}
|
||||
|
||||
public String findSimpleHelloWorld() {
|
||||
String cacheKey = "simple-hello";
|
||||
String helloWorld = simpleHelloWorldCache.get(cacheKey);
|
||||
if (helloWorld == null) {
|
||||
helloWorld = repository.getHelloWorld();
|
||||
simpleHelloWorldCache.put(cacheKey, helloWorld);
|
||||
}
|
||||
return helloWorld;
|
||||
}
|
||||
|
||||
public String findExpiringHelloWorld() {
|
||||
String cacheKey = "expiring-hello";
|
||||
String helloWorld = simpleHelloWorldCache.get(cacheKey);
|
||||
if (helloWorld == null) {
|
||||
helloWorld = repository.getHelloWorld();
|
||||
simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
return helloWorld;
|
||||
}
|
||||
|
||||
public String findIdleHelloWorld() {
|
||||
String cacheKey = "idle-hello";
|
||||
String helloWorld = simpleHelloWorldCache.get(cacheKey);
|
||||
if (helloWorld == null) {
|
||||
helloWorld = repository.getHelloWorld();
|
||||
simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
return helloWorld;
|
||||
}
|
||||
|
||||
public String findSimpleHelloWorldInExpiringCache() {
|
||||
String cacheKey = "simple-hello";
|
||||
String helloWorld = expiringHelloWorldCache.get(cacheKey);
|
||||
if (helloWorld == null) {
|
||||
helloWorld = repository.getHelloWorld();
|
||||
expiringHelloWorldCache.put(cacheKey, helloWorld);
|
||||
}
|
||||
return helloWorld;
|
||||
}
|
||||
|
||||
public String findEvictingHelloWorld(String key) {
|
||||
String value = evictingHelloWorldCache.get(key);
|
||||
if(value == null) {
|
||||
value = repository.getHelloWorld();
|
||||
evictingHelloWorldCache.put(key, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public String findPassivatingHelloWorld(String key) {
|
||||
String value = passivatingHelloWorldCache.get(key);
|
||||
if(value == null) {
|
||||
value = repository.getHelloWorld();
|
||||
passivatingHelloWorldCache.put(key, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.infinispan.service;
|
||||
|
||||
import org.infinispan.Cache;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
public class TransactionalService {
|
||||
|
||||
private final Cache<String, Integer> transactionalCache;
|
||||
|
||||
private static final String KEY = "key";
|
||||
|
||||
public TransactionalService(Cache<String, Integer> transactionalCache) {
|
||||
this.transactionalCache = transactionalCache;
|
||||
|
||||
transactionalCache.put(KEY, 0);
|
||||
}
|
||||
|
||||
public Integer getQuickHowManyVisits() {
|
||||
try {
|
||||
TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager();
|
||||
tm.begin();
|
||||
Integer howManyVisits = transactionalCache.get(KEY);
|
||||
howManyVisits++;
|
||||
System.out.println("Ill try to set HowManyVisits to " + howManyVisits);
|
||||
StopWatch watch = new StopWatch();
|
||||
watch.start();
|
||||
transactionalCache.put(KEY, howManyVisits);
|
||||
watch.stop();
|
||||
System.out.println("I was able to set HowManyVisits to " + howManyVisits +
|
||||
" after waiting " + watch.getTotalTimeSeconds() + " seconds");
|
||||
|
||||
tm.commit();
|
||||
return howManyVisits;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void startBackgroundBatch() {
|
||||
try {
|
||||
TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager();
|
||||
tm.begin();
|
||||
transactionalCache.put(KEY, 1000);
|
||||
System.out.println("HowManyVisits should now be 1000, " +
|
||||
"but we are holding the transaction");
|
||||
Thread.sleep(1000L);
|
||||
tm.rollback();
|
||||
System.out.println("The slow batch suffered a rollback");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user