11 Replies Latest reply on Jul 28, 2011 6:31 AM by pmuir

    Infinispan caching in portlets within same portal

    meghaattcs

      Hi,

      I have a portal application, which hosts several portlets within it.

      According to my business scenario, we need to store certain user related information within a cache to enable seemless usage within the application.

      For this I have been using Jboss Cache and POJO Cache in JBoss AS 4.0.5 and JBoss Portal 2.4

       

      Now we are migrating to JBoss 6.x with Gatein 3.1

      I am also using Infinispan for caching with the above new setup.

       

      From what I have understood of Infinispan is that it supports caching only in clustered environments ? Correct me if I am wrong.

       

      What led me to the above conclusion is when I tried testing the Infinispan cache as a standalone, it works fine. I am able to set and retrieve values from the cache. But when I try the same within different modules (portlets) it does'nt work. The cache object returned is NULL.

       

      Here is the sample code.

      ===== 1st Portlet======
      
      
      SubscriberCache subscriberCache = SubscriberCache.getSubscriberCacheInstance();
       Cache cache=subscriberCache.startCache();
       System.out.println("cache :::"+cache);
       //log.info(subscriberID + "~Creating the instance of SubscriberInfo");
       SubscriberInfo subInfo = new SubscriberInfo();
       //log.info(subscriberID + "~Getting the Object from cache instance");
       subInfo = (SubscriberInfo)cache.get("100000000");
       System.out.println("SubscriberInfo:::"+subInfo.getMessage()); 
      
      
      ==== 2nd Portlet====
      
      
      
      
         SubscriberCache subscriberCache = SubscriberCache.getSubscriberCacheInstance();
       Cache cache=subscriberCache.startCache();
       //log.info(subscriberID + "~Creating the instance of SubscriberInfo");
       SubscriberInfo subInfo = new SubscriberInfo();
       //log.info(subscriberID + "~Getting the Object from cache instance");
       System.out.println("Cache Key value: " + cache.get("100000000"));
       subInfo = (SubscriberInfo)cache.get("100000000");
       System.out.println("SubscriberInfo:::"+subInfo.getMessage());
      
      
        
      
      
      
      

      When I access my 2nd portlet, the line "System.out.println("Cache Key value: " + cache.get("100000000"));" reads "Cache Key value: NULL"

       

      Any idea where am I going wrong or missing something ?

       

      Thanks in advance

      Megha

        • 1. Re: Infinispan caching in portlets within same portal
          pmuir

          Infinispan supports a non-clustered, local-cache mode just like JBoss Cache did.

           

          You don't really show any details of how you use Infinispan in your code snippets, you just refer to your own code. Somewhat difficult to guess what is going wrong then ;-)

          • 2. Re: Infinispan caching in portlets within same portal
            meghaattcs

            Pete,

            Thanks for the reply

            Pete Muir wrote:

             

            You don't really show any details of how you use Infinispan in your code snippets, you just refer to your own code. Somewhat difficult to guess what is going wrong then ;-)

            Well I have just imported the Infinispan related classes in my Cache class and using the infinispan-core.jar under the classpath. Nothing much. Attaching the class files in this thread. Please see and let me know.

             

            P.S. This is my first stint with Infinispan, could be going completely amiss with this

            • 3. Re: Infinispan caching in portlets within same portal
              pmuir

              I'm more interested in how you use Infinispan, so post any code that accesses Infinispan.

              • 4. Re: Infinispan caching in portlets within same portal
                meghaattcs

                Pete,

                I am just creating a cache instance and putting values in the cache object. And through the other portlet trying to retrieve the values from cache, as you saw in the previous 2 classes.

                 

                I am not sure what you mean when you say "I'm more interested in how you use Infinispan, so post any code that accesses Infinispan."

                 

                It would be really helpful in case you could share some samples in this regard.

                 

                Thanks,

                Megha

                • 5. Re: Infinispan caching in portlets within same portal
                  meghaattcs

                  To make things clear, the CustomPhaselistener1 and CustomPhaselistener classes are test classes where I am trying to read/write values from/to cache.

                   

                  My actual application [is intended to] will make use of the cache in a similar way.

                  • 6. Re: Infinispan caching in portlets within same portal
                    pmuir

                    No, I can't see that from any code you have shown so far. You seem to have some kind of abstraction layer being used. Post the code of the abstraction layer. I want to see classes where you import classes from Infinispan itself.

                    • 7. Re: Infinispan caching in portlets within same portal
                      pmuir

                      Sorry, I missed your second set of attachments. I don't really know how portlets work... Do you share the Infinispan classes between portlets? Or are the jars copied to each portlet?

                      • 8. Re: Infinispan caching in portlets within same portal
                        prabhat.jha

                        Portlet is another form of web application and so the way you would use Infinispan will be similar to how you would use it in any other web application. Multiple portlets in same .war should be able to interact with same cache.

                         

                        Looking at your code, I don't think it has anything to do with portlet and more to do with sharing cache between different JSF licecycles. You should verify your assertion first in simple JSF webapp and then move to portlet.

                        • 9. Re: Infinispan caching in portlets within same portal
                          pmuir

                          I think as the cache is stored in a static as long as the same classloader is used for both portlets then the cache should be shared.

                          • 10. Re: Infinispan caching in portlets within same portal
                            meghaattcs

                            Pete Muir wrote:

                             

                            Sorry, I missed your second set of attachments. I don't really know how portlets work... Do you share the Infinispan classes between portlets? Or are the jars copied to each portlet?

                            Pete,

                            Yes, we do include the infinispan related jars in each portlet.

                            Nevertheless, lets ignore the portlets bit for sometime here now.

                            My requirement is: I have a selfcare application where a customer can login to perform various activites like Recharge, Log complaints, Update Profile etc.. Now for this I need to share the customer specific data within the actions he performs until he/she is logged in.

                            For this in my earlier setup we were using JBoss Cache with POJO Cache. Now I need to replace the same with Infinispan.

                             

                            Any idea how to go about this?

                             

                            If you could share some examlpes/samples it would be great.

                             

                            P.S If you can share your email Id, I can mail the entire present code which uses JBoss Cache and POJO Cache

                             

                            Thanks,

                            Megha

                            • 11. Re: Infinispan caching in portlets within same portal
                              pmuir

                              We don't have any samples of using Infinispan in portlets. And no, I don't particularly want to see your entire code base!

                               

                              Anyway, you're answer gives us the clue we need. You share the cache manager using a static. Static's are shared between all instances of a class. However if you bundle Infinispan in each portlet and then each portlet will end up with a different set of classes (the name is the same, but the actual class object is different - anyway, this is basic hierarchical classloading - checkout Jason's excellent intro http://www.vimeo.com/15122570).

                               

                              So, to solve your problem either

                               

                              * move the Infinispanjars  to a shared classloader

                              * find another way (e.g. JNDI) to share the classloader

                               

                              The latter and more is covered in this article from Paul http://community.jboss.org/docs/16180

                              1 of 1 people found this helpful