3 Replies Latest reply on Nov 24, 2016 2:21 PM by sebastian.laskawiec

    Programmatic replicated cache

    stephan.zaulig

      Hi All,

       

      in our project we use WildFly 10.1 in domain mode.

      Currently I am trying to programmatically create replicated caches via the EmbeddedCacheManager.

      As i read in the forums not to inject the cache directly I inject a default cache like so:

       

      web.xml:

      <resource-env-ref> 

           <resource-env-ref-name>xyz/defaultCache</resource-env-ref-name> 

           <resource-env-ref-type>org.infinispan.Cache</resource-env-ref-type> 

           <lookup-name>java:jboss/infinispan/cache/xyz/mydefaultcache</lookup-name> 

      </resource-env-ref> 

       

      Bean:

      @Resource(name = "xyz/defaultCache")

      private Cache<String, String> defaultCache;

       

      This cache is configured as a replicated cache in domain.xml.

      Then I try to create an new cache via the CacheManager:

       

      ConfigurationBuilder cb = new ConfigurationBuilder();

      cb.read(defaultCache.getCacheConfiguration()); 

      defaultCache.getCacheManager().defineConfiguration("myNewCache", cb.build);

      getCacheManager().getCache("myNewCache", true);

       

      This works, but the cache is build on every node and does not replicate.

      Is this supposed to work? What am I doing wrong?

       

      Regards and Thanks!

        • 1. Re: Programmatic replicated cache
          vdzhuvinov

          You may need to declare a transport method (for the underlying JGroups lib) which should suit the network where you have your Infinispan nodes deployed.

           

          With XML it's done like this, by pointing Infinispan to a suitable JGroups config file, here for UDP:

           

          <jgroups>
            <stack-file name="jgroups-config" path="default-configs/default-jgroups-udp.xml"/>
          </jgroups>

          • 2. Re: Programmatic replicated cache
            stephan.zaulig

            As a cache that is configured in domain.xml works, I think JGroups is configured correctly.

            Also the session replication works via UPD protocol.

             

            Here  is the relevant configuration from domain.xml:

             

            <subsystem xmlns="urn:jboss:domain:jgroups:4.0">

                            <channels default="ee">

                                <channel name="ee" stack="udp"/>

                            </channels>

                            <stacks>

                                <stack name="udp">

                                    <transport type="UDP" socket-binding="jgroups-udp"/>

                                    <protocol type="PING"/>

                                    <protocol type="MERGE3"/>

                                    <protocol type="FD_SOCK" socket-binding="jgroups-udp-fd"/>

                                    <protocol type="FD_ALL"/>

                                    <protocol type="VERIFY_SUSPECT"/>

                                    <protocol type="pbcast.NAKACK2"/>

                                    <protocol type="UNICAST3"/>

                                    <protocol type="pbcast.STABLE"/>

                                    <protocol type="pbcast.GMS"/>

                                    <protocol type="UFC"/>

                                    <protocol type="MFC"/>

                                    <protocol type="FRAG2"/>

                                </stack>

             

            As I understand it, if there is no default stack, the udp stack from "ee" chanel is used.

            • 3. Re: Programmatic replicated cache
              sebastian.laskawiec

              One possible reason this doesn't work is that only one node knows about newly created cache. The configuration change is done by you programatically and it's not propagated to the rest of the servers.

               

              The only way this might work (I'm not entirely sure but it won't hurt to try it out) is to deploy the app on all servers in the domain. This way the configuration should be in sync and replication should work. However you might get into troubles when undeploying/redeploying your app.

               

              In Hot Rod Server we implemented our own version of ispn-cli.sh script which allows to create caches on the fly. It's much better solution but it would require having a separate cluster only for Infinispan. This would be a suggested solution.