8 Replies Latest reply on Nov 23, 2011 11:23 AM by kevinpollet

    jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event

    kristjan273

      Hi,

       

      Looking at infinispan quickstart example, particulary the one for jboss7, I am wondering if it is possible to add a listener for org.infinispan.notifications.cachelistener.event events to injected cache. (Reading quickly some blogs I got an idea this is not quite working at the moment in the 5.0.1 version of infinispan).

       

      If my idea is wrong, than I would be glad to see some example to achieve the goal.

      If unfortunately this is correct, than I would ask for some opinion how to achive the same without CDI but still using jboss7

       

       

      tnx in advance,

      K

        • 1. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
          kevinpollet

          Hi,

           

          IMHO, the listeners should be registered at application startup. For that, you can use an EJB singleton with a @PostConstruct method. In both cases, CDI or AS 7, you will have to inject the cache/cache container and register the listeners in the @PostConstruct method.

           

          • Infinispan CDI

           

          @Startup
          @Singleton
          class ListenerManager {
          
               @Inject
               private Cache<?,?> defaultCache;
          
               @Inject @MyQualifier
               private Cache<?,?> cache;
          
               @PostConstruct
               void registerListeners() {
                    defaultCache.addListener(...);
                    cache.addListener(...);
               }   
          }
          

           

          • AS 7

           

          @Startup
          @Singleton
          class ListenerManager {
          
               @Resource(lookup="java:jboss/infinispan/my-container-name") 
               private CacheContainer container;
          
               @PostConstruct
               void registerListeners() {
                    container.getCache().addListener(...);
                    container.getCache("my-cache-name").addListener(...);
               }   
          }
          
          • 2. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
            kristjan273

            Thank you very much Kevin.

            That was exactly what I was looking for.

            • 3. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
              kristjan273

              Hi,

               

              while testing this solution another question poped out and I won't open new thread as it is on the top of previous.

               

              I have tried to run the solution in domain mode of jboss on two jboss instances started. I expected following event listener

               

              @CacheEntryCreated

                  public void observeAdd(CacheEntryCreatedEvent<?, ?> event) {

                      log.infof("Cache entry with key %s added in cache %s", event.getKey(), event.getCache());

              }

               

              to show the logline on both nodes when something was added to cache.

              But only the node where cache insert was called showed the line. Interesting that I was able to read the value on the other node.

               

              Only problem was that event did not notify other node on created event.

               

               

              Question is - is that meant to work that way or listener should notify whole domain (if started as proposed above)?

              If not, is there another way to achieve such behavior as I see a lot of benefit for my application if domain notification would work.

              (and some example please ...)

               

              thank you again in advance

              • 4. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
                kristjan273

                Ok,  finding that cache was setup as distributed and owners=1.

                 

                But I am still wondering is there a way (or is meant to work like that) for whole domain to be notified on particular event, regardles the number of owners being set?

                • 5. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
                  kevinpollet

                  Hi,

                   

                  It should work!

                   

                  Can you show how your listener is added?

                  Do you see a stacktrace on the second node? (I vaguely remember a classloader issue with listeners and multiple nodes)

                  • 6. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
                    kristjan273

                    Hi,

                     

                    thank you for looking into this. Code is based on infinispan quickstarts, particularly jboss-as7 folder.

                     

                    I have added ListenerManager as you suggested and LoggingListener from cdi example (please see attached file).

                    I am using jboss 7.0.2(everything), cache-manager in domain.xml is altered to owners=2

                     

                     

                    When adding something to cache the following output appears:

                     

                     

                    [Server:server-four] 10:24:23,166 ERROR [stderr] (http--127.0.0.1-8430-1) 43844572 [http--127.0.0.1-8430-1] INFO org.infinispan.quickstart.jbossas7.LoggingListener - Cache entry wi
                    th key 28 added in cache Cache 'jboss-as7-quickstart-cache'@server-four-63862
                    [Server:server-four]
                    [Server:server-four] 10:24:23,166 ERROR [stderr] (http--127.0.0.1-8430-1) 43844574 [http--127.0.0.1-8430-1] INFO org.infinispan.quickstart.jbossas7.LoggingListener - Cache entry wi
                    th key 28 added in cache Cache 'jboss-as7-quickstart-cache'@server-four-63862
                    [Server:server-four]
                    [Server:server-three] 10:24:23,181 ERROR [stderr] (OOB-18,null) 43843993 [OOB-18,null] INFO org.infinispan.quickstart.jbossas7.LoggingListener - Cache entry with key 28 added in ca
                    che Cache 'jboss-as7-quickstart-cache'@server-three-21505
                    [Server:server-three]
                    [Server:server-three] 10:24:23,182 ERROR [stderr] (OOB-18,null) 43843995 [OOB-18,null] INFO org.infinispan.quickstart.jbossas7.LoggingListener - Cache entry with key 28 added in ca
                    che Cache 'jboss-as7-quickstart-cache'@server-three-21505
                    [Server:server-three]
                    

                     

                     

                    I also wonder - listener's output is duplicated and so far I have no idea why (will try looking into it, but i am just so fresh into as7 that I even don't know how to manage loglines formating ...).

                     

                     

                    brg,

                    K

                    • 7. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
                      kristjan273

                      Hi Kevin,

                       

                      sorry to bother but I would be glad if you could confirm the suspicion on classloading problem and possible issue/workaround to avoid such behaviour.

                       

                      tnx

                      • 8. Re: jboss7, CDI and listening to org.infinispan.notifications.cachelistener.event
                        kevinpollet

                        Hi,

                         

                        Sorry for the delay!

                         

                        I think that only the nodes where the cache entry is added are notified (in distributed mode this is not all the domain). Not sure if it's possible to have all the domain notified (you can ask the question in another thread). Btw, it'll work as expected in a replicated mode.

                         

                        I also wonder - listener's output is duplicated and so far I have no idea why (will try looking into it, but i am just so fresh into as7 that I even don't know how to manage loglines formating ...).

                         

                        If you look at the event object you will see that there is an 'isPre()' method. That's because Infinispan send an event before and after the operation is performed.

                         

                        sorry to bother but I would be glad if you could confirm the suspicion on classloading problem and possible issue/workaround to avoid such behaviour.

                         

                        The classloader issue is only here when your listener is a CDI bean with a scope that make it a proxy (@ApplicationScoped).

                        1 of 1 people found this helpful