14 Replies Latest reply on Nov 29, 2012 11:26 AM by Rats Chaks

    JBOSS AS 6 RC1 and  using Infispan directly

    Fin Steenbjerg Newbie

      I just started using jboss (jboss AS 6 RC1) and built-in Infinispan. I am setting up a JBOSS cluster and I am trying to get my application to be cluster aware (whatever that means). So far I tried session replication and hibernate 2LC and both of these works out of the box. I also need to have a distributed cache in which the application can store non-container-managed POJOs. I have read http://community.jboss.org/wiki/InfinispanIntegrationinAS6 (the section named Using Infinispan Directly) but I cant get et working (probably because I dont understand the details). Does anyone have a working sample that I can get access to?

        • 1. Re: JBOSS AS 6 RC1 and  using Infispan directly
          Galder Zamarreño Master

          Fin, I don't have an example right now but if you can zip up what you have done and explain what your problem is, maybe we can help further

          • 2. Re: JBOSS AS 6 RC1 and  using Infispan directly
            Paul Ferraro Master

            The "Using Infinispan Directly" section illustrates 2 ways to reference a cache for use in your application.  The first configuration example show how to configure and deploy a new cache container.  The second configuration example shows how to deploy a cache container whose configuration is already defined in the cache container registry.  Which method did you attempt?

            • 3. Re: JBOSS AS 6 RC1 and  using Infispan directly
              Fin Steenbjerg Newbie

              First of all, I am new to both infinispan and jboss bean configuration. That may explain the level of my questions. What I want is to make a distributed cache available to all the JEE applications installed at the jboss cluster. Each application must only be able to see the cache that belongs to it. The developers of the applications should need to now as little as possible about the configuration. Secondly I want to be able to keep an eye on the behaviour of the individual caches during operational (check the size of the caches, clear caches etc.). Thats the basic requirements.

               

              I really have not tried that much yet. I have added the xml snippet as you describe to the deploy/cluster/infinispan-cache-registry.sar/cache-container-registry-jboss-beans.xml file. Now I got the first question. How do I validate that the new cache container is added ok (either via the admin console or the jmx-console)? What I want now is to have application specific caches inside the new cachecontainer and secondly having the caches injected into a JEE managed objects via annotations (hopefully with CDI annotations or something else). How do I do that?

              • 4. Re: JBOSS AS 6 RC1 and  using Infispan directly
                Paul Ferraro Master

                Let me take a step back a second...

                What , specifically, are you trying to achieve?

                1. Create a cache container and cache instance per application?

                2. Create a cache instance per application from a shared cache container?

                 

                If (1), then you would not want to append the the xml snippet from the "Using Infinispan Directly" section (i.e. the "Factory created cache container" snippet) to the global cache-container-registry-jboss-beans.xml file, but rather to an application specific bean deployment descriptor (a *-jboss-beans.xml file within the META-INF directory of your application archive).  This way your application will create its own cache container which will start on application deploy and stop on undeploy.

                 

                If (2), then you should first decide if a new cache container configuration is genuinely warranted, or if any of the existing cache container configurations are sufficient.  If you need to add a new cache container configuration, you can either add a new <infinispan-config> block to the deploy/cluster/infinispan-cache-registry.sar/infinispan-configs.xml file, or inject a configuration into the registry (an example is provided in the document in question).

                Once complete, you can reference your cache container as a bean using the "Cache contianer obtained from registry" snippet.

                 

                Any microcontainer bean can then be injected into your application objects.

                 

                OK, now to answer your specific question - you can validate that your new cache container by any number of ways:

                1. You were able to inject a cache container or cache instance into your application. i.e. you application deployed successfully.

                2. The cache container or cache instances appear in the admin and/or jmx consoles.

                • 5. Re: JBOSS AS 6 RC1 and  using Infispan directly
                  Glen Miller Newbie

                  If I create a cache container by adding the following to the the infinispan-cache-registry.sar/cache-container-registry-jboss-beans.xml file, how would i inject it into my stateless session bean?

                   

                  <!-- Cache container obtained from registry -->
                  <bean name="CustomCacheContainer" class="org.infinispan.manager.CacheContainer">
                    <constructor factoryMethod="getCacheContainer">
                      <factory bean="CacheContainerRegistry"/>
                      <parameter>custom</parameter>
                    </constructor>
                    <!-- Let registry control container's lifecycle -->

                    <start ignored="true"/>
                    <stop ignored="true"/>
                  </bean>

                   

                   

                  --- Example Bean

                   

                  @Stateless

                  public class FooImpl implements Foo

                  {

                       @Override

                       public void test1()

                       {

                       }

                      

                       @Resource(name = "CustomCacheContainer")

                       private org.infinispan.manager.CacheContainer customCacheContainer;

                  }

                  • 6. Re: JBOSS AS 6 RC1 and  using Infispan directly
                    Paul Ferraro Master

                    This should work:

                     

                    @Stateless

                    public class FooImpl implements Foo

                    {

                         @Override

                         public void test1()

                         {

                         }

                       

                         private org.infinispan.manager.CacheContainer customCacheContainer;

                     

                         @Inject(bean = "CustomCacheContainer")

                         public void setCustomCacheContainer(org.infinispan.manager.CacheContainer container)

                          {

                              this.customCacheContainer = container;

                         }

                    }

                     

                    If not (and it may not, since we're mixing and EJB w/MC stuff), you should be able to use JNDI as the mediating location.

                    So, you would have the CustomCacheContainer bean bound to a jndi location, and use the @Resource annotation to lookup the container using its jndi name.

                    • 7. Re: JBOSS AS 6 RC1 and  using Infispan directly
                      Glen Miller Newbie

                      Anyone else trying to figure this out who stumbles on this -- here is how i got it going

                       

                      copy from inside

                      infinispan-cache-registry.sar/infinispan-configs.xml the section similar to below and customize to what you need, at least changing the name and jndi-name.

                       

                      <infinispan-config name="hibernate" jndi-name="java:CacheManager/entity">

                          <infinispan xmlns="urn:infinispan:config:4.2">

                            <global>

                              <transport clusterName="${jboss.partition.name:DefaultPartition}-Hibernate" distributedSyncTimeout="17500">

                                <properties>

                                  <property name="stack" value="${jboss.default.jgroups.stack:udp}"/>

                                </properties>

                              </transport>

                              <globalJmxStatistics enabled="true"/>

                              <shutdown hookBehavior="DONT_REGISTER"/>

                            </global>

                            <default>

                              <!-- Used to register JMX statistics in any available MBean server -->

                              <jmxStatistics enabled="false"/>

                              <!--transaction transactionManagerLookupClass="org.infinispan.transaction.lookup.JBossTransactionManagerLookup"/-->

                            </default>     

                          </infinispan>

                        </infinispan-config>

                       

                      than you can inject into your code using something like

                       

                      @Resource(name = "CacheManager/entity", mappedName = "java:CacheManager/entity")

                          private EmbeddedCacheManager cacheManager;

                      • 8. Re: JBOSS AS 6 RC1 and  using Infispan directly
                        Glen Miller Newbie

                        oops didnt see the other reply before i did mine.  oh well i think both will work

                        • 9. Re: JBOSS AS 6 RC1 and  using Infispan directly
                          Madan Narra Newbie

                          Hi Glen,

                           

                          I was trying to integrate a simple app with Infinispan. Followed your instructions as specified above.

                           

                          Made a copy of 'web' in infinispan-config.xml and modified its name, jndi and few other places where 'web' is used

                           

                           

                          <infinispan-config name="infinispanTest" jndi-name="java:CacheManager/infinispanTest">
                              <alias>custom-testinfinispan-cache</alias>
                              <infinispan xmlns="urn:infinispan:config:4.2">
                                <global>
                                  <transport clusterName="${jboss.partition.name:DefaultPartition}-TestInfinispan" distributedSyncTimeout="17500">
                                    <properties>
                                      <property name="stack" value="${jboss.default.jgroups.stack:udp}"/>
                                    </properties>
                                  </transport>
                                  <globalJmxStatistics enabled="true"/>
                                  <shutdown hookBehavior="DONT_REGISTER"/>
                                </global>
                                <default>
                                  <locking isolationLevel="REPEATABLE_READ" lockAcquisitionTimeout="15000" useLockStriping="false" concurrencyLevel="1000"/>
                                  <jmxStatistics enabled="true"/>
                                  <lazyDeserialization enabled="true"/>
                                  <invocationBatching enabled="true"/>
                                  <clustering mode="replication">
                                    <!-- ISPN-835 workaround, use cluster cache loader in place of state transfer -->
                                    <!--stateRetrieval timeout="60000" fetchInMemoryState="true"/-->
                                    <async useReplQueue="false"/>
                                  </clustering>
                                  <loaders passivation="true">
                                    <loader class="org.infinispan.loaders.file.FileCacheStore" fetchPersistentState="true" purgeOnStartup="true">
                                      <properties>
                                        <property name="location" value="${jboss.server.data.dir}${/}infinispantest"/>
                                      </properties>
                                    </loader>
                                    <loader class="org.infinispan.loaders.cluster.ClusterCacheLoader"/>
                                  </loaders>
                                </default>
                                <namedCache name="numbersCache">
                                  <clustering mode="distribution">
                                    <!-- ISPN-835 workaround, use cluster cache loader in place of state transfer -->
                                    <!--stateRetrieval timeout="60000" fetchInMemoryState="false"/-->
                                    <hash numOwners="2"/>
                                    <async useReplQueue="false"/>
                                  </clustering>
                                  <loaders passivation="true">
                                    <loader class="org.infinispan.loaders.file.FileCacheStore" fetchPersistentState="false" purgeOnStartup="true">
                                      <properties>
                                        <property name="location" value="${jboss.server.data.dir}${/}infinispantest"/>
                                      </properties>
                                    </loader>
                                    <loader class="org.infinispan.loaders.cluster.ClusterCacheLoader">
                                      <properties>
                                        <property name="remoteCallTimeout" value="60000"/>
                                      </properties>
                                    </loader>
                                  </loaders>
                                </namedCache>
                              </infinispan>
                            </infinispan-config>
                          

                           

                          Mapped the JNDI using @Resource in my servlet

                           

                          @Resource(name = "CacheManager/infinispanTest", mappedName = "java:CacheManager/infinispanTest")
                          private EmbeddedCacheManager cacheManager;
                          

                           

                          This doesnt seem to work as am getting NullPointerException when accessing cacheManager.

                           

                          Attached is sample project archive with a single servlet to test infinispan cache.

                           

                          Accessing the servlet with type=put, key=1 and value=one, an entry should be made to cache with key as 1 and value as one. (http://10.2.84.60:8080/infiniwebtest/test?type=put&key=1&value=one)

                           

                          When accessing the cache with type=get and key=1, should get the value one from cache.(http://10.2.84.60:8080/infiniwebtest/test?type=get&key=1)

                           

                          Main goal of this test is to get this sample work in a cluster with 3 nodes using shared cache.

                           

                          What else am missing which is causing the problem. Please do the needful.

                           

                          Madan Narra

                          • 10. Re: JBOSS AS 6 RC1 and  using Infispan directly
                            Madan Narra Newbie

                            Hi Paul,

                             

                            If not (and it may not, since we're mixing and EJB w/MC stuff), you should be able to use JNDI as the mediating location.

                            So, you would have the CustomCacheContainer bean bound to a jndi location, and use the @Resource annotation to lookup the container using its jndi name.

                             

                            Can you please explain how can I get reference to cache container if accessed via jndi? Piece of code would be really helpful.

                             

                            Also In your article Infinispan Integration with AS 6 , you mentioned

                             

                            the cache container is deployed as a bean using one of the above mechanisms, it can be injected into any application object.

                            The injection is done using JBoss MC. What If I want this bean to be injected into a simple servlet deployed in JBoss ?

                             

                            Am new to jboss and struggling to execute an example with JBoss built in Infinispan cache.

                             

                            Thanks,

                            Madan Narra

                            • 11. Re: JBOSS AS 6 RC1 and  using Infispan directly
                              Madan Narra Newbie

                              Hi..

                               

                              On further trying, was able to fetch JNDI resource for 'java:CacheManager/infinispanTest'. This returns instance of 'org.jboss.ha.ispn.DefaultCacheContainer'.

                               

                              Configured Spring to access the jndi resource and inject that to a service bean as below :

                               

                              <bean id="testInfinispanServlet" class="com.infinispan.test.InfinispanTestServlet">
                                  <property name="cacheManager" ref="cacheManager"/>
                              </bean>
                              
                              <!-- JNDI Configuration -->
                              <jee:jndi-lookup id="cacheManager" jndi-name="java:CacheManager/infinispanTest"/>
                              

                               

                              cacheManager is injected as type 'EmbeddedCacheManager'

                               

                              private EmbeddedCacheManager cacheManager;
                              
                              public void setCacheManager(EmbeddedCacheManager cacheManager) {
                                                  this.cacheManager = cacheManager;
                              
                                                  System.out.println("------------------------> cacheManager :: "+cacheManager);
                              }
                              

                               

                              When started jboss, getting the below exception

                               

                              Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.jboss.ha.ispn.DefaultCacheContainer' to required type 'org.infinispan.manager.EmbeddedCacheManager' for property 'cacheManager'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.jboss.ha.ispn.DefaultCacheContainer] to required type [org.infinispan.manager.EmbeddedCacheManager] for property 'cacheManager': no matching editors or conversion strategy found

                               

                              As DefaultCacheContainer implements EmbeddedCacheManager, This should work. But why is this getting errored out?

                               

                              Its been 3 days to get a sample to work with Infinispan and Jboss AS 6.

                               

                              Please do the needful.

                               

                              Thanks,

                              Madan Narra

                              • 12. Re: JBOSS AS 6 RC1 and  using Infispan directly
                                Madan Narra Newbie

                                The issue got resolved when not including the infinispan and dependant jars in webapp WEB-INF/lib. Integration with inbuilt inifinispan in jboss is working as expected and aswell in cluster mode.

                                 

                                Thanks,

                                Madan Narra

                                • 13. Re: JBOSS AS 6 RC1 and  using Infispan directly
                                  Megha Shah Newbie

                                  Hi Madan,

                                   

                                   

                                  We are using Jboss 6 with Infinispan and for this we have an XML under $JBOSS_HOME/server/all/deploy/cluster/infinispan-cache-registry.sar/cache-container-registry-jboss-beans.xml (given below)

                                  Also modified the infinispan-configs.xml (attached in the post)

                                   

                                  I have added the bean as mentioned by you in the cache-container-registry-jboss-beans.xml. (highlighted in bold) - rest of the xml remains as provided out of the box

                                   

                                  Yet it does not seem to be working. Still getting a NULLPointerException

                                  Am I modifying the correct XMLs?

                                   

                                   

                                  <?xml version="1.0" encoding="UTF-8"?>
                                  <deployment xmlns="urn:jboss:bean-deployer:2.0">
                                  
                                    <bean name="CacheContainerFactory" class="org.jboss.ha.ispn.DefaultCacheContainerFactory">
                                      <constructor>
                                        <parameter><inject bean="JChannelFactory"/></parameter>
                                        <parameter><inject bean="JMXKernel" property="mbeanServer"/></parameter>
                                      </constructor>
                                    </bean>
                                  
                                  
                                    <bean name="CacheContainerRegistry" class="org.jboss.ha.ispn.DefaultCacheContainerRegistry">
                                      <constructor>
                                        <parameter><inject bean="CacheContainerFactory"/></parameter>
                                        <parameter>
                                          <bean class="org.jboss.ha.ispn.config.xml.XMLCacheContainerRegistryConfigurationSource">
                                            <constructor>
                                              <!-- Location of infinispan configuration file -->
                                              <parameter class="java.lang.String">infinispan-configs.xml</parameter>
                                            </constructor>
                                          </bean>
                                        </parameter>
                                      </constructor>
                                      <!-- If a jndiNamePrefix is defined, all cache containers will be bound to jndi by name within the specified parent context, unless name is explicitly defined -->
                                      <!-- By default, only those cache containers that define an explicit jndi-name will be bound to jndi -->
                                      <!--property name="jndiNamePrefix">java:infinispan</property-->
                                    </bean>
                                    <bean id="testInfinispanServlet" class="com.tsky.selfcare.cache.InfinispanServlet">
                                        <property name="cacheManager" ref="cacheManager"/>
                                    </bean>
                                  
                                  
                                    <!-- JNDI Configuration -->
                                    <jee:jndi-lookup id="cacheManager" jndi-name="java:CacheManager/infinispanTest"/>
                                  
                                  
                                  </deployment>
                                  
                                  

                                   

                                   

                                   

                                  infinispan-config.xml (portion of which was added)
                                  
                                  <!-- **************** infinispanTest *********************** -->
                                    <infinispan-config name="infinispanTest" jndi-name="java:CacheManager/infinispanTest">
                                        <alias>custom-testinfinispan-cache</alias>
                                        <infinispan xmlns="urn:infinispan:config:4.2">
                                          <global>
                                            <transport clusterName="${jboss.partition.name:DefaultPartition}-TestInfinispan" distributedSyncTimeout="17500">
                                              <properties>
                                                <property name="stack" value="${jboss.default.jgroups.stack:udp}"/>
                                              </properties>
                                            </transport>
                                            <globalJmxStatistics enabled="true"/>
                                            <shutdown hookBehavior="DONT_REGISTER"/>
                                          </global>
                                          <default>
                                            <locking isolationLevel="REPEATABLE_READ" lockAcquisitionTimeout="15000" useLockStriping="false" concurrencyLevel="1000"/>
                                            <jmxStatistics enabled="true"/>
                                            <lazyDeserialization enabled="true"/>
                                            <invocationBatching enabled="true"/>
                                            <clustering mode="replication">
                                              <!-- ISPN-835 workaround, use cluster cache loader in place of state transfer -->
                                              <!--stateRetrieval timeout="60000" fetchInMemoryState="true"/-->
                                              <async useReplQueue="false"/>
                                            </clustering>
                                            <loaders passivation="true">
                                              <loader class="org.infinispan.loaders.file.FileCacheStore" fetchPersistentState="true" purgeOnStartup="true">
                                                <properties>
                                                  <property name="location" value="${jboss.server.data.dir}${/}infinispantest"/>
                                                </properties>
                                              </loader>
                                              <loader class="org.infinispan.loaders.cluster.ClusterCacheLoader"/>
                                            </loaders>
                                          </default>
                                          <namedCache name="numbersCache">
                                            <clustering mode="distribution">
                                              <!-- ISPN-835 workaround, use cluster cache loader in place of state transfer -->
                                              <!--stateRetrieval timeout="60000" fetchInMemoryState="false"/-->
                                              <hash numOwners="2"/>
                                              <async useReplQueue="false"/>
                                            </clustering>
                                            <loaders passivation="true">
                                              <loader class="org.infinispan.loaders.file.FileCacheStore" fetchPersistentState="false" purgeOnStartup="true">
                                                <properties>
                                                  <property name="location" value="${jboss.server.data.dir}${/}infinispantest"/>
                                                </properties>
                                              </loader>
                                              <loader class="org.infinispan.loaders.cluster.ClusterCacheLoader">
                                                <properties>
                                                  <property name="remoteCallTimeout" value="60000"/>
                                                </properties>
                                              </loader>
                                            </loaders>
                                          </namedCache>
                                        </infinispan>
                                      </infinispan-config>
                                  
                                  
                                  
                                  

                                   

                                  Thanks in advance,

                                  Megha

                                  • 14. Re: JBOSS AS 6 RC1 and  using Infispan directly
                                    Rats Chaks Newbie

                                    Hi Madan,

                                     

                                    Thx for sharing the details of your web application. I tried building a similar one and got it working with two nodes in a clustered set up. However I tried to use the out-of-the box cache eviction strategy of infinispan with LRU strategy. I have used <eviction> </eviction> tag in the infinispan-configs.xml; I have defined maxentries and other attributes; however the eviction is not working. Cache keeps on growing as the entries are added.

                                     

                                    Can you please help with the working example config/code in this regard?

                                     

                                    Is it mandatory to use <expiration> tag along with <eviction> tag?

                                     

                                    Any response/working example from the group would be really appreciated here.

                                     

                                    Thanks.