Skip to content

Instantly share code, notes, and snippets.

Created August 30, 2017 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e8752cc8457301c2c70b3255e5c3bbc5 to your computer and use it in GitHub Desktop.
Save anonymous/e8752cc8457301c2c70b3255e5c3bbc5 to your computer and use it in GitHub Desktop.
package com.foo.bar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
public class ResourceInitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Resource(lookup = "java:jboss/infinispan/container/clustered")
private EmbeddedCacheManager manager;
/**
* Initialize this servlet
*/
public void init(ServletConfig aServeletConfig) {
System.out.println(ResourceInitServlet.class.getCanonicalName() + " started loading ..");
try {
super.init(aServeletConfig);
initInfinispanService();
loadCache();
} catch (Exception exp) {
exp.printStackTrace();
}
}
private void loadCache() {
addCache("SampleCache", getCacheData());
}
private Map<String, SamplePOJO> getCacheData() {
Map<String, SamplePOJO> buffer = new HashMap<>();
buffer.put("111", new SamplePOJO("key1", "value1"));
buffer.put("222", new SamplePOJO("key2", "value2"));
buffer.put("333", new SamplePOJO("key3", "value3"));
buffer.put("444", new SamplePOJO("key4", "value4"));
buffer.put("555", new SamplePOJO("key5", "value5"));
return buffer;
}
// container is already initialized
private void initInfinispanService() {
System.out.println("Manager class is :" + manager.getClass().getCanonicalName());
}
protected Configuration getCacheManagerConfig() {
Configuration config = new ConfigurationBuilder().clustering().cacheMode(CacheMode.REPL_SYNC).compatibility()
.enable().invocationBatching().enable()
// .transaction().lockingMode(LockingMode.OPTIMISTIC).autoCommit(true).transactionMode(org.infinispan.transaction.TransactionMode.TRANSACTIONAL)
// .locking().useLockStriping(false).lockAcquisitionTimeout(10000)
// .isolationLevel(IsolationLevel.READ_COMMITTED)
// .marshaller(new GenericJBossMarshaller())
// .marshaller(new MapExternalizer())
// .storeAsBinary()
.build();
return config;
}
public <K, V> Map<K, V> addCache(String name, Map<K, V> map) {
if (!manager.cacheExists(name)) {
System.out.println("Cache " + name + "does not exists so defining new config.");
manager.defineConfiguration(name, getCacheManagerConfig());
}
Map<K, V> aMap = manager.getCache(name);
aMap.putAll(map);
SamplePOJO element1 = (SamplePOJO) aMap.get("111");
System.out.println("element1.getClass(): " + element1.getClass());
System.out.println("element1.getClass().getCanonicalName(): " + element1.getClass().getCanonicalName());
System.out.println(" element1.getClass().getClassLoader(): " + element1.getClass().getClassLoader());
return aMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment