I am trying to implement EhCache Replicated Caching using RMI. I have a Spring Web application wich uses EhCache for data caching. Ehcache holds few Java.Util.Map, that is used through out the application view pages. When a record (say some business JAVA POJO object) is created by the user from front end screen, that record will be inserted into the database and subsequently, Maps held by the EhCache is updated.
Later, we deployed this web application in 3 tomcat instances in the same machine. Application is accessible via the Apache HTTP Load Balancer.
Promblem I am facing is, EhCache data is loading on one tomcat instance. But not on other two. When the application is individually accessed with port numbers, the application is running fine.
Same Spring web application runs on Tomcat instances (9001, 9002, 9003). EhCache RMI configured to listen (4001, 4002, 4003).
Please find the ehCache.xml files that I configured,
On Tomcat Instance 1
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="WidgetCache" updateCheck="false">
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=10.237.31.33, port=40001, socketTimeoutMillis=2000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//10.237.31.33:40002/widgets|//10.237.31.33:40002/lobCache|//10.237.31.33:40002/lobFilterCache|//10.237.31.33:40002/glossarylobFilterCache|//10.237.31.33:40002/bbpUserListCache|//10.237.31.33:40002/userRoleCache|//10.237.31.33:40002/glossary|//10.237.31.33:40003/widgets|//10.237.31.33:40003/lobCache|//10.237.31.33:40003/lobFilterCache|//10.237.31.33:40003/glossarylobFilterCache|//10.237.31.33:40003/bbpUserListCache|//10.237.31.33:40003/userRoleCache|//10.237.31.33:40003/glossary" />
<cache name="userRoleCache" maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<diskStore path="java.io.tmpdir" />
On Tomcat Instance 2
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="WidgetCache" updateCheck="false">
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=10.237.31.33, port=40002, socketTimeoutMillis=2000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//10.237.31.33:40001/widgets|//10.237.31.33:40001/lobCache|//10.237.31.33:40001/lobFilterCache|//10.237.31.33:40001/glossarylobFilterCache|//10.237.31.33:40001/bbpUserListCache|//10.237.31.33:40001/userRoleCache|//10.237.31.33:40001/glossary|//10.237.31.33:40003/widgets|//10.237.31.33:40003/lobCache|//10.237.31.33:40003/lobFilterCache|//10.237.31.33:40003/glossarylobFilterCache|//10.237.31.33:40003/bbpUserListCache|//10.237.31.33:40003/userRoleCache|//10.237.31.33:40003/glossary" />
<cache name="userRoleCache" maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<diskStore path="java.io.tmpdir" />
On Tomcat Instance 3
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="WidgetCache" updateCheck="false">
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=10.237.31.33, port=40003, socketTimeoutMillis=2000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//10.237.31.33:40001/widgets|//10.237.31.33:40001/lobCache|//10.237.31.33:40001/lobFilterCache|//10.237.31.33:40001/glossarylobFilterCache|//10.237.31.33:40001/bbpUserListCache|//10.237.31.33:40001/userRoleCache|//10.237.31.33:40001/glossary|//10.237.31.33:40002/widgets|//10.237.31.33:40002/lobCache|//10.237.31.33:40002/lobFilterCache|//10.237.31.33:40002/glossarylobFilterCache|//10.237.31.33:40002/bbpUserListCache|//10.237.31.33:40002/userRoleCache|//10.237.31.33:40002/glossary" />
<cache name="userRoleCache" maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true" />
</cache>
<diskStore path="java.io.tmpdir" />
Kindly advise what I am missing here.
The below is configured in spring-servlet.xml
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" p:shared="true"/>
And from the Java business class
public class PortfolioUserDetailsServiceImpl implements PortfolioUserDetailsService {
private Logger logger = LoggerFactory
.getLogger(PortfolioUserDetailsServiceImpl.class);
private UserDao userDao;
@Autowired
private CacheManager cacheManager;
private Ehcache userRoleCache;
@PostConstruct
public void init() {
// Load our widgets cache:
userRoleCache = cacheManager.getEhcache("userRoleCache");
// Create an EHCache Element to hold the widget
Element element = new Element("getAllRoles", userDao.getAllRoles());
// Add the element to the cache
userRoleCache.put(element);
}
I am getting NullPointerException on the following method
@Override
public List<BbpUser> loadUsersfromUserListCache() throws InventoryException {
// TODO Auto-generated method stub
Cache cache = cacheManager.getCache("userRoleCache");
Element elementt = cache.get("getAllRoles");
return (List<BbpUser>) elementt.getObjectValue();
}