The Map-based reference implementation
of our example application does not implement any housekeeping, which
will eventually lead to OutOfMemory errors as the

This part shows how to replace the ConcurrentMap with a Cache and utilize the
Cache’s eviction to limit the amount of memory used.

The following describes the Infinispan example.


In the implementation below, Infinispan’s
is used to obtain a Cache
instance. The Infinispan cache extends ConcurrentMap.

This corresponds one-to-one to the architecture described in the
JSR 107 overview.

public void contextInitialized(ServletContextEvent servletContextEvent) {
    try {
        DefaultCacheManager cacheManager = new DefaultCacheManager("infinispan.xml");
        ConcurrentMap<String, UserEventList> map = cacheManager.getCache();
        ServletContext context = servletContextEvent.getServletContext();
        context.setAttribute(CACHE, map);
        context.setAttribute(CACHE_MANAGER, cacheManager);
    } catch (IOException e) {
        throw new WebApplicationException(e);

The initialization code above reads Infinispan’s configuration from
an XML file called inifnispan.xml, which is loaded via the classpath.
The contents of infinispan.xml is as follows:

<infinispan xsi:schemaLocation="urn:infinispan:config:5.2 infinispan-config-5.2.xsd"
        <eviction maxEntries="1000" strategy="LRU"/>

The REST Interface loads the map from the

// ...
private ServletContext context;
private ConcurrentMap<String, UserEventList> map;

public void init() {
    map = (ConcurrentMap) context.getAttribute(CACHE);


As with Ehcache and
Hazelcast, the cache
is shut down using the cache manager.

public void contextDestroyed(ServletContextEvent servletContextEvent) {
    ServletContext context = servletContextEvent.getServletContext();
    DefaultCacheManager cacheManager = (DefaultCacheManager) context.getAttribute(CACHE_MANAGER);


As Infinispan’s Cache
implements ConcurrentMap, the implementation of the REST interface can be
taken one-to-one from the Map-based reference implementation:

public void appendEvent(@PathParam("user") String user, String msg) {
    boolean success;
    map.putIfAbsent(user, UserEventList.emptyList());
    do {
        UserEventList oldMsgList = map.get(user);
        UserEventList newMsgList = UserEventList.append(oldMsgList, msg);
        success = map.replace(user, oldMsgList, newMsgList);
    while ( ! success );


Like the write method, the read method is one-to-one the same as in the
Map-based implementation:

public List<String> searchByUser(@PathParam("user") String user) {
    UserEventList result = map.get(user);
    if ( result == null ) {
        return new ArrayList<>();
    return result.getMessages();



How To Run

Our example code is hosted on GitHub.
The example can be run with maven:

mvn tomcat7:run-war -pl part02.infinispan -am verify

The Web interface is then accessible via


This was the last page of part 02 of our series, which showed how the
ConcurrentMap in our example can be replaced with a local cache.

Part 03 and part 04 will show how the cache can be clustered and shared among
multiple application servers: