Skip to content

Commit 6eeff13

Browse files
vitallanpedja4
authored andcommitted
BAEL-1368: infinispan article (eugenp#3633)
1 parent 1c043dc commit 6eeff13

9 files changed

Lines changed: 494 additions & 1 deletion

File tree

‎libraries/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,11 @@
649649
<artifactId>google-http-client-gson</artifactId>
650650
<version>${googleclient.version}</version>
651651
</dependency>
652+
<dependency>
653+
<groupId>org.infinispan</groupId>
654+
<artifactId>infinispan-core</artifactId>
655+
<version>${infinispan.version}</version>
656+
</dependency>
652657

653658
<!--Java Docker API Client-->
654659
<dependency>
@@ -811,5 +816,6 @@
811816
<docker.version>3.0.14</docker.version>
812817
<tomcat.version>8.5.24</tomcat.version>
813818
<async.http.client.version>2.2.0</async.http.client.version>
819+
<infinispan.version>9.1.5.Final</infinispan.version>
814820
</properties>
815-
</project>
821+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.infinispan;
2+
3+
import com.baeldung.infinispan.listener.CacheListener;
4+
import org.infinispan.Cache;
5+
import org.infinispan.configuration.cache.Configuration;
6+
import org.infinispan.configuration.cache.ConfigurationBuilder;
7+
import org.infinispan.eviction.EvictionType;
8+
import org.infinispan.manager.DefaultCacheManager;
9+
import org.infinispan.transaction.LockingMode;
10+
import org.infinispan.transaction.TransactionMode;
11+
12+
import java.util.concurrent.TimeUnit;
13+
14+
public class CacheConfiguration {
15+
16+
public static final String SIMPLE_HELLO_WORLD_CACHE = "simple-hello-world-cache";
17+
public static final String EXPIRING_HELLO_WORLD_CACHE = "expiring-hello-world-cache";
18+
public static final String EVICTING_HELLO_WORLD_CACHE = "evicting-hello-world-cache";
19+
public static final String PASSIVATING_HELLO_WORLD_CACHE = "passivating-hello-world-cache";
20+
public static final String TRANSACTIONAL_CACHE = "transactional-cache";
21+
22+
public DefaultCacheManager cacheManager() {
23+
DefaultCacheManager cacheManager = new DefaultCacheManager();
24+
return cacheManager;
25+
}
26+
27+
public Cache<String, Integer> transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) {
28+
return this.buildCache(TRANSACTIONAL_CACHE, cacheManager, listener, transactionalConfiguration());
29+
}
30+
31+
public Cache<String, String> simpleHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
32+
return this.buildCache(SIMPLE_HELLO_WORLD_CACHE, cacheManager, listener, new ConfigurationBuilder().build());
33+
}
34+
35+
public Cache<String, String> expiringHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
36+
return this.buildCache(EXPIRING_HELLO_WORLD_CACHE, cacheManager, listener, expiringConfiguration());
37+
}
38+
39+
public Cache<String, String> evictingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
40+
return this.buildCache(EVICTING_HELLO_WORLD_CACHE, cacheManager, listener, evictingConfiguration());
41+
}
42+
43+
public Cache<String, String> passivatingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) {
44+
return this.buildCache(PASSIVATING_HELLO_WORLD_CACHE, cacheManager, listener, passivatingConfiguration());
45+
}
46+
47+
private <K, V> Cache<K, V> buildCache(String cacheName, DefaultCacheManager cacheManager,
48+
CacheListener listener, Configuration configuration) {
49+
50+
cacheManager.defineConfiguration(cacheName, configuration);
51+
Cache<K, V> cache = cacheManager.getCache(cacheName);
52+
cache.addListener(listener);
53+
return cache;
54+
}
55+
56+
private Configuration expiringConfiguration() {
57+
return new ConfigurationBuilder().expiration().lifespan(1, TimeUnit.SECONDS)
58+
.build();
59+
}
60+
61+
private Configuration evictingConfiguration() {
62+
return new ConfigurationBuilder()
63+
.memory().evictionType(EvictionType.COUNT).size(1)
64+
.build();
65+
}
66+
67+
private Configuration passivatingConfiguration() {
68+
return new ConfigurationBuilder()
69+
.memory().evictionType(EvictionType.COUNT).size(1)
70+
.persistence()
71+
.passivation(true)
72+
.addSingleFileStore()
73+
.purgeOnStartup(true)
74+
.location(System.getProperty("java.io.tmpdir"))
75+
.build();
76+
}
77+
78+
private Configuration transactionalConfiguration() {
79+
return new ConfigurationBuilder()
80+
.transaction().transactionMode(TransactionMode.TRANSACTIONAL)
81+
.lockingMode(LockingMode.PESSIMISTIC)
82+
.build();
83+
}
84+
85+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.infinispan.listener;
2+
3+
import org.infinispan.notifications.Listener;
4+
import org.infinispan.notifications.cachelistener.annotation.*;
5+
import org.infinispan.notifications.cachelistener.event.*;
6+
7+
@Listener
8+
public class CacheListener {
9+
10+
@CacheEntryCreated
11+
public void entryCreated(CacheEntryCreatedEvent<String, String> event) {
12+
this.printLog("Adding key '" + event.getKey() + "' to cache", event);
13+
}
14+
15+
@CacheEntryExpired
16+
public void entryExpired(CacheEntryExpiredEvent<String, String> event) {
17+
this.printLog("Expiring key '" + event.getKey() + "' from cache", event);
18+
}
19+
20+
@CacheEntryVisited
21+
public void entryVisited(CacheEntryVisitedEvent<String, String> event) {
22+
this.printLog("Key '" + event.getKey() + "' was visited", event);
23+
}
24+
25+
@CacheEntryActivated
26+
public void entryActivated(CacheEntryActivatedEvent<String, String> event) {
27+
this.printLog("Activating key '" + event.getKey() + "' on cache", event);
28+
}
29+
30+
@CacheEntryPassivated
31+
public void entryPassivated(CacheEntryPassivatedEvent<String, String> event) {
32+
this.printLog("Passivating key '" + event.getKey() + "' from cache", event);
33+
}
34+
35+
@CacheEntryLoaded
36+
public void entryLoaded(CacheEntryLoadedEvent<String, String> event) {
37+
this.printLog("Loading key '" + event.getKey() + "' to cache", event);
38+
}
39+
40+
@CacheEntriesEvicted
41+
public void entriesEvicted(CacheEntriesEvictedEvent<String, String> event) {
42+
final StringBuilder builder = new StringBuilder();
43+
event.getEntries().entrySet().forEach((e) ->
44+
builder.append(e.getKey() + ", ")
45+
);
46+
System.out.println("Evicting following entries from cache: " + builder.toString());
47+
}
48+
49+
private void printLog(String log, CacheEntryEvent event) {
50+
if (!event.isPre()) {
51+
System.out.println(log);
52+
}
53+
}
54+
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.infinispan.repository;
2+
3+
public class HelloWorldRepository {
4+
5+
public String getHelloWorld() {
6+
try {
7+
System.out.println("Executing some heavy query");
8+
Thread.sleep(1000);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
12+
return "Hello World!";
13+
}
14+
15+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.infinispan.service;
2+
3+
import com.baeldung.infinispan.listener.CacheListener;
4+
import com.baeldung.infinispan.repository.HelloWorldRepository;
5+
import org.infinispan.Cache;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
public class HelloWorldService {
10+
11+
private final HelloWorldRepository repository;
12+
13+
private final Cache<String, String> simpleHelloWorldCache;
14+
private final Cache<String, String> expiringHelloWorldCache;
15+
private final Cache<String, String> evictingHelloWorldCache;
16+
private final Cache<String, String> passivatingHelloWorldCache;
17+
18+
public HelloWorldService(HelloWorldRepository repository, CacheListener listener,
19+
Cache<String, String> simpleHelloWorldCache,
20+
Cache<String, String> expiringHelloWorldCache,
21+
Cache<String, String> evictingHelloWorldCache,
22+
Cache<String, String> passivatingHelloWorldCache) {
23+
24+
this.repository = repository;
25+
26+
this.simpleHelloWorldCache = simpleHelloWorldCache;
27+
this.expiringHelloWorldCache = expiringHelloWorldCache;
28+
this.evictingHelloWorldCache = evictingHelloWorldCache;
29+
this.passivatingHelloWorldCache = passivatingHelloWorldCache;
30+
}
31+
32+
public String findSimpleHelloWorld() {
33+
String cacheKey = "simple-hello";
34+
String helloWorld = simpleHelloWorldCache.get(cacheKey);
35+
if (helloWorld == null) {
36+
helloWorld = repository.getHelloWorld();
37+
simpleHelloWorldCache.put(cacheKey, helloWorld);
38+
}
39+
return helloWorld;
40+
}
41+
42+
public String findExpiringHelloWorld() {
43+
String cacheKey = "expiring-hello";
44+
String helloWorld = simpleHelloWorldCache.get(cacheKey);
45+
if (helloWorld == null) {
46+
helloWorld = repository.getHelloWorld();
47+
simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS);
48+
}
49+
return helloWorld;
50+
}
51+
52+
public String findIdleHelloWorld() {
53+
String cacheKey = "idle-hello";
54+
String helloWorld = simpleHelloWorldCache.get(cacheKey);
55+
if (helloWorld == null) {
56+
helloWorld = repository.getHelloWorld();
57+
simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
58+
}
59+
return helloWorld;
60+
}
61+
62+
public String findSimpleHelloWorldInExpiringCache() {
63+
String cacheKey = "simple-hello";
64+
String helloWorld = expiringHelloWorldCache.get(cacheKey);
65+
if (helloWorld == null) {
66+
helloWorld = repository.getHelloWorld();
67+
expiringHelloWorldCache.put(cacheKey, helloWorld);
68+
}
69+
return helloWorld;
70+
}
71+
72+
public String findEvictingHelloWorld(String key) {
73+
String value = evictingHelloWorldCache.get(key);
74+
if(value == null) {
75+
value = repository.getHelloWorld();
76+
evictingHelloWorldCache.put(key, value);
77+
}
78+
return value;
79+
}
80+
81+
public String findPassivatingHelloWorld(String key) {
82+
String value = passivatingHelloWorldCache.get(key);
83+
if(value == null) {
84+
value = repository.getHelloWorld();
85+
passivatingHelloWorldCache.put(key, value);
86+
}
87+
return value;
88+
}
89+
90+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.infinispan.service;
2+
3+
import org.infinispan.Cache;
4+
import org.springframework.util.StopWatch;
5+
6+
import javax.transaction.TransactionManager;
7+
8+
public class TransactionalService {
9+
10+
private final Cache<String, Integer> transactionalCache;
11+
12+
private static final String KEY = "key";
13+
14+
public TransactionalService(Cache<String, Integer> transactionalCache) {
15+
this.transactionalCache = transactionalCache;
16+
17+
transactionalCache.put(KEY, 0);
18+
}
19+
20+
public Integer getQuickHowManyVisits() {
21+
try {
22+
TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager();
23+
tm.begin();
24+
Integer howManyVisits = transactionalCache.get(KEY);
25+
howManyVisits++;
26+
System.out.println("Ill try to set HowManyVisits to " + howManyVisits);
27+
StopWatch watch = new StopWatch();
28+
watch.start();
29+
transactionalCache.put(KEY, howManyVisits);
30+
watch.stop();
31+
System.out.println("I was able to set HowManyVisits to " + howManyVisits +
32+
" after waiting " + watch.getTotalTimeSeconds() + " seconds");
33+
34+
tm.commit();
35+
return howManyVisits;
36+
} catch (Exception e) {
37+
e.printStackTrace();
38+
return 0;
39+
}
40+
}
41+
42+
public void startBackgroundBatch() {
43+
try {
44+
TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager();
45+
tm.begin();
46+
transactionalCache.put(KEY, 1000);
47+
System.out.println("HowManyVisits should now be 1000, " +
48+
"but we are holding the transaction");
49+
Thread.sleep(1000L);
50+
tm.rollback();
51+
System.out.println("The slow batch suffered a rollback");
52+
} catch (Exception e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
57+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.infinispan;
2+
3+
import com.baeldung.infinispan.listener.CacheListener;
4+
import com.baeldung.infinispan.repository.HelloWorldRepository;
5+
import com.baeldung.infinispan.service.HelloWorldService;
6+
import com.baeldung.infinispan.service.TransactionalService;
7+
import org.infinispan.Cache;
8+
import org.infinispan.manager.DefaultCacheManager;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
12+
public class ConfigurationTest {
13+
14+
private DefaultCacheManager cacheManager;
15+
16+
private HelloWorldRepository repository = new HelloWorldRepository();
17+
18+
protected HelloWorldService helloWorldService;
19+
protected TransactionalService transactionalService;
20+
21+
@Before
22+
public void setup() {
23+
CacheConfiguration configuration = new CacheConfiguration();
24+
CacheListener listener = new CacheListener();
25+
26+
cacheManager = configuration.cacheManager();
27+
28+
Cache<String, Integer> transactionalCache =
29+
configuration.transactionalCache(cacheManager, listener);
30+
31+
Cache<String, String> simpleHelloWorldCache =
32+
configuration.simpleHelloWorldCache(cacheManager, listener);
33+
34+
Cache<String, String> expiringHelloWorldCache =
35+
configuration.expiringHelloWorldCache(cacheManager, listener);
36+
37+
Cache<String, String> evictingHelloWorldCache =
38+
configuration.evictingHelloWorldCache(cacheManager, listener);
39+
40+
Cache<String, String> passivatingHelloWorldCache =
41+
configuration.passivatingHelloWorldCache(cacheManager, listener);
42+
43+
this.helloWorldService = new HelloWorldService(repository,
44+
listener, simpleHelloWorldCache, expiringHelloWorldCache, evictingHelloWorldCache,
45+
passivatingHelloWorldCache);
46+
47+
this.transactionalService = new TransactionalService(transactionalCache);
48+
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
cacheManager.stop();
54+
}
55+
56+
}

0 commit comments

Comments
 (0)