3 Replies Latest reply on Aug 25, 2011 8:08 AM by Sanne Grinovero

    Retrieve cache from another node

    James Cameron Newbie

      Hi,

       

      There are two machines which are linked together via a local network. Infinispan was installed on each of the machines and the same configuration is used on the two machines. Machine 1 puts a cache to its server. My goal is to read the cache from machine 2 but my approach fails.

       

      Both machines run the following code to get named cache.

       

              EmbeddedCacheManager ecm = null;
              
              try {
                  ecm = new DefaultCacheManager(...);
              } catch (IOException e) {}
      
             Cache<String, String> cache = ecm.getCache("myCache");
      

       

      Although they pass the same cache name to ecm.getCache(...), two different cache instances are actually returned because two different EmbeddedCacheManager were created.

       

      My question is how to achieve my goal. Is there a way of using the EmbeddedCacheManager created in machine 1 from machine 2?

       

      The configuration used on the two machines is below.

       

      <infinispan
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="urn:infinispan:config:5.0 http://www.infinispan.org/schemas/infinispan-config-5.0.xsd"
            xmlns="urn:infinispan:config:5.0">
          
          <global>
              <transport 
                  clusterName="hotMessageCluster" 
                  transportClass="org.infinispan.remoting.transport.jgroups.JGroupsTransport" 
                  machineId="m1" rackId="r1" nodeName="Node-A">
                  <properties>
                      <property name="configurationFile" value="jgroups-udp.xml" />
                   </properties>
              </transport>
              <globalJmxStatistics enabled="true" allowDuplicateDomains="true"/><!-- allow multiple cache managers -->
          </global>
      
          <default>
              <transaction
                  transactionManagerLookupClass="org.infinispan.transaction.lookup.GenericTransactionManagerLookup"
                  syncRollbackPhase="false"
                  syncCommitPhase="false"
                  useEagerLocking="false"
                  eagerLockSingleNode="false"
                  cacheStopTimeout="30000" />
              <expiration wakeUpInterval="-1"/>
          </default>
      
          <namedCache name="myCache">
              <eviction maxEntries="1000" strategy="LRU"/>
              <expiration wakeUpInterval="60000" lifespan="-1" maxIdle="-1"/>
                <clustering mode="invalidation">
                    <async/>
                    <hash
                      numOwners="3"
                      rehashWait="120000"
                      rehashRpcTimeout="600000"/>
                   <l1
                      enabled="true"
                      lifespan="600000"/>
                </clustering>
          </namedCache>
      
      </infinispan>
      

       

      The jgroups-udp.xml is copied from JGroups and is not touched.

       

      Thank you for any suggestions and directions.

        • 1. Re: Retrieve cache from another node
          Sanne Grinovero Master

          Hi, what do you mean with "same instance" ?

          The two caches will always be different instances, what you can achieve with infinispan is that they contain the same values (not same instances, but copies of the same values).

          You will get the same instance when asking the same cache by name from the same CacheManager instance.

           

          Two objects living in two different JVMs can not be the "same instance", even less if the two JVMs are running on different network nodes.

           

          Maybe I could help better if you could explain what you need to do?

          1 of 1 people found this helpful
          • 2. Re: Retrieve cache from another node
            James Cameron Newbie

            Hi Sanne,

             

            Even my question is not clear, your explanation about Infinispan is still very helpful. Thanks.

             

            What I'm tring to do is to retrieve a cache value stored in machine 1's server from machine 2. Specifically, in machine 1, "value 1" is cached associated with "key 1" key:

            Cache<String, String> c1 = ecm1.getCache("myCache");
                       //ecm1 is an EmbeddedCacheManager created in machine 1
            c1.putAsync("key 1", "value 1");

             

            In machine 2, I try to read the above cache by using this code:

            Cache<String, String> c2 = ecm2.getCache("myCache");
                       //ecm2 is an EmbeddedCacheManager created in machine 2
            System.out.println(c2.get("key 1")); // null

            but fails because c2 is empty. My goal is to get "value 1" from machine 2.

             

            Sanne Grinovero wrote:

             

            what you can achieve with infinispan is that they contain the same values (not same instances, but copies of the same values).

             

            In my case, you mean c1 can be copied to c2? If so, could you direct me how to?

             

            Thanks again for your help.

            • 3. Re: Retrieve cache from another node
              Sanne Grinovero Master
              In my case, you mean c1 can be copied to c2? If so, could you direct me how to?

              Yes you don't need to copy them yourself, Infinispan will replicate the values to the other nodes when configured with replication, or even if the values aren't copied to all nodes make sure that a get() is able to find the value from other nodes when asked to.

               

              So it all boils down to configure it properly: you have to enable a clustered configuration, use either replication or distribution; then when using clustered configurations you might have to configure JGroups as well: default configurations are provided which should work in most cases, but you might have to change for example to which network interface it should bind to.