BAEL-1109 Introduction to JCache
This commit is contained in:
parent
a1b7c270dd
commit
d56dbc081d
@ -226,6 +226,10 @@
|
|||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
<groupId>commons-logging</groupId>
|
<groupId>commons-logging</groupId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -237,6 +241,10 @@
|
|||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
<groupId>commons-logging</groupId>
|
<groupId>commons-logging</groupId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -244,6 +252,12 @@
|
|||||||
<artifactId>flink-test-utils_2.10</artifactId>
|
<artifactId>flink-test-utils_2.10</artifactId>
|
||||||
<version>${flink.version}</version>
|
<version>${flink.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
@ -472,6 +486,18 @@
|
|||||||
<artifactId>eclipse-collections</artifactId>
|
<artifactId>eclipse-collections</artifactId>
|
||||||
<version>${eclipse-collections.version}</version>
|
<version>${eclipse-collections.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.cache</groupId>
|
||||||
|
<artifactId>cache-api</artifactId>
|
||||||
|
<version>${cache.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.hazelcast</groupId>
|
||||||
|
<artifactId>hazelcast</artifactId>
|
||||||
|
<version>${hazelcast.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<multiverse.version>0.7.0</multiverse.version>
|
<multiverse.version>0.7.0</multiverse.version>
|
||||||
@ -514,5 +540,8 @@
|
|||||||
<pcollections.version>2.1.2</pcollections.version>
|
<pcollections.version>2.1.2</pcollections.version>
|
||||||
<rome.version>1.0</rome.version>
|
<rome.version>1.0</rome.version>
|
||||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||||
|
<cache.version>1.0.0</cache.version>
|
||||||
|
<cache-ri.version>1.0.0</cache-ri.version>
|
||||||
|
<hazelcast.version>3.8.4</hazelcast.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import javax.cache.Cache;
|
||||||
|
import javax.cache.CacheManager;
|
||||||
|
import javax.cache.Caching;
|
||||||
|
import javax.cache.configuration.FactoryBuilder;
|
||||||
|
import javax.cache.configuration.MutableConfiguration;
|
||||||
|
import javax.cache.spi.CachingProvider;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class CacheLoaderTest {
|
||||||
|
|
||||||
|
private Cache<Integer, String> cache;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||||
|
CacheManager cacheManager = cachingProvider.getCacheManager();
|
||||||
|
MutableConfiguration<Integer, String> config = new MutableConfiguration<Integer, String>().setReadThrough(true)
|
||||||
|
.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader()));
|
||||||
|
this.cache = cacheManager.createCache("SimpleCache", config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReadingFromStorage_thenCorrect() {
|
||||||
|
for (int i = 1; i < 4; i++) {
|
||||||
|
String value = cache.get(i);
|
||||||
|
assertEquals("fromCache" + i, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import javax.cache.Cache;
|
||||||
|
import javax.cache.CacheManager;
|
||||||
|
import javax.cache.Caching;
|
||||||
|
import javax.cache.configuration.MutableConfiguration;
|
||||||
|
import javax.cache.spi.CachingProvider;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EntryProcessorTest {
|
||||||
|
|
||||||
|
private Cache<String, String> cache;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void instantiateCache() {
|
||||||
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||||
|
CacheManager cacheManager = cachingProvider.getCacheManager();
|
||||||
|
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>();
|
||||||
|
this.cache = cacheManager.createCache("MyCache", config);
|
||||||
|
this.cache.put("key", "value");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenModifyValue_thenCorrect() {
|
||||||
|
this.cache.invoke("key", new SimpleEntryProcessor());
|
||||||
|
assertEquals("value - modified", cache.get("key"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import javax.cache.Cache;
|
||||||
|
import javax.cache.CacheManager;
|
||||||
|
import javax.cache.Caching;
|
||||||
|
import javax.cache.configuration.FactoryBuilder;
|
||||||
|
import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
|
||||||
|
import javax.cache.configuration.MutableConfiguration;
|
||||||
|
import javax.cache.spi.CachingProvider;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EventListenerTest {
|
||||||
|
|
||||||
|
private Cache<String, String> cache;
|
||||||
|
private SimpleCacheEntryListener listener;
|
||||||
|
MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||||
|
CacheManager cacheManager = cachingProvider.getCacheManager();
|
||||||
|
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>();
|
||||||
|
this.cache = cacheManager.createCache("MyCache", config);
|
||||||
|
this.listener = new SimpleCacheEntryListener();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRunEvent_thenCorrect() throws InterruptedException {
|
||||||
|
this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(this.listener), null, false, true);
|
||||||
|
this.cache.registerCacheEntryListener(this.listenerConfiguration);
|
||||||
|
|
||||||
|
assertEquals(false, this.listener.getCreated());
|
||||||
|
|
||||||
|
this.cache.put("key", "value");
|
||||||
|
assertEquals(true, this.listener.getCreated());
|
||||||
|
assertEquals(false, this.listener.getUpdated());
|
||||||
|
|
||||||
|
this.cache.put("key", "newValue");
|
||||||
|
assertEquals(true, this.listener.getUpdated());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
libraries/src/test/java/com/baeldung/jcache/JCacheTest.java
Normal file
27
libraries/src/test/java/com/baeldung/jcache/JCacheTest.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import javax.cache.Cache;
|
||||||
|
import javax.cache.CacheManager;
|
||||||
|
import javax.cache.Caching;
|
||||||
|
import javax.cache.configuration.MutableConfiguration;
|
||||||
|
import javax.cache.spi.CachingProvider;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class JCacheTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void instantiateCache() {
|
||||||
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
||||||
|
CacheManager cacheManager = cachingProvider.getCacheManager();
|
||||||
|
MutableConfiguration<String, String> config = new MutableConfiguration<>();
|
||||||
|
Cache<String, String> cache = cacheManager.createCache("simpleCache", config);
|
||||||
|
cache.put("key1", "value1");
|
||||||
|
cache.put("key2", "value2");
|
||||||
|
assertEquals("value1", cache.get("key1"));
|
||||||
|
assertEquals("value2", cache.get("key2"));
|
||||||
|
cacheManager.close();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import javax.cache.event.CacheEntryCreatedListener;
|
||||||
|
import javax.cache.event.CacheEntryEvent;
|
||||||
|
import javax.cache.event.CacheEntryListenerException;
|
||||||
|
import javax.cache.event.CacheEntryUpdatedListener;
|
||||||
|
|
||||||
|
public class SimpleCacheEntryListener implements CacheEntryCreatedListener<String, String>, CacheEntryUpdatedListener<String, String>, Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -712657810462878763L;
|
||||||
|
private boolean updated;
|
||||||
|
private boolean created;
|
||||||
|
|
||||||
|
public boolean getUpdated() {
|
||||||
|
return this.updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getCreated() {
|
||||||
|
return this.created;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onUpdated(Iterable<CacheEntryEvent<? extends String, ? extends String>> events) throws CacheEntryListenerException {
|
||||||
|
this.updated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onCreated(Iterable<CacheEntryEvent<? extends String, ? extends String>> events) throws CacheEntryListenerException {
|
||||||
|
this.created = true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.cache.integration.CacheLoader;
|
||||||
|
import javax.cache.integration.CacheLoaderException;
|
||||||
|
|
||||||
|
public class SimpleCacheLoader implements CacheLoader<Integer, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String load(Integer key) throws CacheLoaderException {
|
||||||
|
return "fromCache" + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Integer, String> loadAll(Iterable<? extends Integer> keys) throws CacheLoaderException {
|
||||||
|
Map<Integer, String> data = new HashMap<>();
|
||||||
|
for (int key : keys) {
|
||||||
|
data.put(key, load(key));
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.jcache;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import javax.cache.processor.EntryProcessor;
|
||||||
|
import javax.cache.processor.EntryProcessorException;
|
||||||
|
import javax.cache.processor.MutableEntry;
|
||||||
|
|
||||||
|
public class SimpleEntryProcessor implements EntryProcessor<String, String, String>, Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -5616476363722945132L;
|
||||||
|
|
||||||
|
public String process(MutableEntry<String, String> entry, Object... args) throws EntryProcessorException {
|
||||||
|
|
||||||
|
if (entry.exists()) {
|
||||||
|
String current = entry.getValue();
|
||||||
|
entry.setValue(current + " - modified");
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user