title: Spring, Infinispan and JBoss 7 integration
body: <p>I'm trying to use JBoss 7 Infinispan cache as a communication form (something more later) of two war-deployed spring-based apps. I'm having a problem with accessing the JBoss managed cache managers.</p>
  
  <p>When I use</p>
  
  <pre><code>DefaultCacheManager cacheManager = new DefaultCacheManager();
  cache = cacheManager.getCache();
  </code></pre>
  
  <p>on each of two applications, I get two separate caches. Is there any way to access the cache created by JBoss server without using the @ManagedBean annotation and Java EE standard at all ?</p>
  
  <hr>
  
  <p>It's done. Thanks to Kazaag, I used JNDI. </p>
  
  <pre><code>JndiTemplate jndiTemplate = new JndiTemplate();
  jndiTemplate.lookup("java:jboss/infinispan/container/cluster");
  </code></pre>
  
  <p>I had the well known problem with a DefaultEmbeddedCacheManager Class Cast Exception. I used reflections.</p>
  
  <pre><code>Map&lt;Object, Object&gt; cache;
  JndiTemplate jndiTemplate = new JndiTemplate();
  Object cacheManager;
  try {
      cacheManager = (Object) jndiTemplate.lookup("java:jboss/infinispan/container/cluster");
      Method method = cacheManager.getClass().getMethod("getCache");
      cache = (Map) method.invoke(cacheManager);
  } catch (Exception e) {
      e.printStackTrace();
      return;
  }
  </code></pre>
  
  <p>Moreover I had to mark container as started eagerly.</p>
  
  <pre><code>    &lt;cache-container name="cluster" aliases="ha-partition" default-cache="default"&gt;
          &lt;transport lock-timeout="60000"/&gt;
          &lt;replicated-cache name="default" mode="SYNC" start="EAGER" batching="true"&gt;
              &lt;locking isolation="REPEATABLE_READ"/&gt;
          &lt;/replicated-cache&gt;
      &lt;/cache-container&gt;
  </code></pre>
  
  <p>The cache is replicated although different class loaders.</p>