Creature
Creature

Reputation: 141

infinispan: clustred instances not sharing cache

I've spent over 2 days doing nothing but trying to get Infinispan to work in a clustered environment and it's not working. I don't want to run a separate infinispan server, I just want to embed it in my application that runs on a clustered Glassfish. Is that not possible? I got a sample JSF app where you can just load values into a map that's supposed to sit in cache. I pull up one clustered instance, add the values, they show up. But when I go to the other clustered instance, it shows the map as empty.

I know I'm doing something wrong, I just don't know what. Been searching the internet and there is no comprehensive tutorial on how to get it to work.

config (coppied from a tutorial that supposedly shows clustering http://www.mastertheboss.com/infinispan/infinispan-tutorial-part-2/page-2 ):

<infinispan>

    <global>
      <transport clusterName="demoCluster"/>
      <globalJmxStatistics enabled="true"/>
   </global>

   <default>
      <jmxStatistics enabled="true"/>
      <clustering mode="distribution">
         <hash numOwners="2" rehashRpcTimeout="120000"/>
         <sync/>
      </clustering>
   </default>

</infinispan>

Context listener:

package hazelcache.test;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;

@WebListener()
public class Listener implements ServletContextListener
{
    EmbeddedCacheManager manager;

    @Override
    public void contextInitialized(ServletContextEvent sce)
    {
        try
        {
            manager = new DefaultCacheManager("config.xml");
            manager.start();
            sce.getServletContext().setAttribute("cacheManager", manager);
        }
        catch (IOException ex)
        {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce)
    {
        manager.stop();
    }
}

Bean:

package hazelcache.test;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;

@ManagedBean(name="clusterTest")
public class ClusteredCacheBean extends CacheTestBean
{
    EmbeddedCacheManager manager;

    public ClusteredCacheBean() throws IOException
    {
        System.out.println("Before setStuffz()");

        manager = (EmbeddedCacheManager) ((ServletContext)FacesContext.getCurrentInstance().
                getExternalContext().getContext()).getAttribute("cacheManager");

        setStuffz(manager.getCache("stuffz"));
        System.out.println("After setStuffz()");

    }// end ClusteredCacheBean()

    private static EmbeddedCacheManager createCacheManagerProgramatically() {
        return new DefaultCacheManager(GlobalConfigurationBuilder.defaultClusteredBuilder().build());
     }

    @Override
    public String addToCache()
    {
        String forwardTo = null;

        manager.getCache("stuffz").put(getId(), getName());

        return forwardTo;

    }// end addToCache()

    @Override
    public List getStuffzList()
    {
        System.out.println("Stuffz: " + getStuffz().size());
        return new LinkedList(manager.getCache("stuffz").entrySet());
    }
}// end class ClusteredCacheBean

I really don't know what to do at this point...

Upvotes: 1

Views: 1768

Answers (1)

Creature
Creature

Reputation: 141

A wonderful person on another forum helped me figure it out:

1) set this jvm option: -Djava.net.preferIPv4Stack=true

asadmin> create-jvm-options --target ClusterName -Djava.net.preferIPv4Stack=true

2) Call getCache in the listener once just to create the cache as the thing is starting up:

setStuffz(manager.getCache("stuffz"));

3) put namespace on the configuration file:

<infinispan
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="urn:infinispan:config:5.1 http://docs.jboss.org/infinispan/schemas/infinispan-config-5.1.xsd"
      xmlns="urn:infinispan:config:5.1">

Thank you, Tristan from the jBoss forums (https://community.jboss.org/community/infinispan)!

Upvotes: 1

Related Questions