1 Reply Latest reply on Apr 22, 2013 2:50 PM by JoeHolder

    MapListener / Entry Processors

    user12988247
      Hi,

      Just starting to use Coherance api.

      1. Need to implement a MapListener.

      When an entry is inserted, the logic needs to invoke ejb method to retrieve some data.... is it posible to have a reference to an ejb and invoke an ejb operation from the entryInserted block?

      public void entryInserted( MapEvent mapEvent )
      {
      // System.out.println( mapEvent);

      }
      Mostly all the examples I have found they only do System.out to illustrate the concept.

      2. Same question regarding Entry Processors...can I have ejb method calls from it?

      3. Can a stateless session bean implement MapListener? If yes, how does this behave?

      Thanks any help,guidance greatly appreciated.
        • 1. Re: MapListener / Entry Processors
          JoeHolder
          Hi,
          I came across this old post because I was wondering a similar thing.

          It seems the way it is done is to instantiate the derived class and cast that to a MapListener when you add it to the cache. I had been trying to cast my derived class back to a ::coherence::util::MapListener::Handle or to use the create() function previously - with no success. Here is some example code.

          Instead of a ejb I am passing in my own ID3ContextRepositoryInterfacePtr

               class DLL_EXPORT ID3ContextRepositoryListener
                         {
                         public:
                                   virtual void onEntryInserted(ID3ContextPtr cPtr) = 0;
                                   virtual void onEntryUpdated(ID3ContextPtr cPtr) = 0;
                                   virtual void onEntryDeleted(ID3ContextPtr cPtr) = 0;
                         };
                         typedef std::tr1::shared_ptr<ID3ContextRepositoryListener> ID3ContextRepositoryListenerPtr;

          #include <coherence/util/MapListener.hpp>

                              //fwd declaration
                              class CacheD3ContextRepository;

                              class CacheD3ContextMapListener : public ::coherence::lang::class_spec<CacheD3ContextMapListener,
                                                                 ::coherence::lang::extends<::coherence::lang::Object>,
                                                                 ::coherence::lang::implements<::coherence::util::MapListener>>
                              {

                                   friend class ::coherence::lang::factory<CacheD3ContextMapListener>;
                                   friend class CacheD3ContextRepository;

                              protected:
                                        CacheD3ContextMapListener() {}

                                   public:
                                        virtual void entryInserted(::coherence::util::MapEvent::View vEvent);
                                        virtual void entryUpdated(::coherence::util::MapEvent::View vEvent);
                                        virtual void entryDeleted(::coherence::util::MapEvent::View vEvent);
                                        virtual void setRepositoryListener(ID3ContextRepositoryListenerPtr iListener);
                                   private:
                                        ID3ContextRepositoryListenerPtr _iListener;
                              };

          #include <coherence/util/MapListener.hpp>


          void CacheD3ContextMapListener::entryInserted(::coherence::util::MapEvent::View vEvent)
          {
               if (_iListener == NULL)
               {
                    return ;
               }
               ::coherence::lang::Managed<CacheD3Context>::View vD3Context =
               ::coherence::lang::cast
                    <::coherence::lang::Managed<CacheD3Context>::View>(vEvent->getNewValue());
               ID3ContextPtr icPtr = ID3ContextPtr(new CacheD3Context(*vD3Context));
               _iListener->onEntryInserted(icPtr);
          }

          void CacheD3ContextMapListener::entryUpdated(::coherence::util::MapEvent::View vEvent)
          {
               if (_iListener == NULL)
               {
                    return ;
               }
               ::coherence::lang::Managed<CacheD3Context>::View vD3Context =
               ::coherence::lang::cast
                    <::coherence::lang::Managed<CacheD3Context>::View>(vEvent->getNewValue());
               ID3ContextPtr icPtr = ID3ContextPtr(new CacheD3Context(*vD3Context));
               _iListener->onEntryUpdated(icPtr);
          }
          void CacheD3ContextMapListener::entryDeleted(::coherence::util::MapEvent::View vEvent)
          {
               if (_iListener == NULL)
               {
                    return ;
               }
               ::coherence::lang::Managed<CacheD3Context>::View vD3Context =
               ::coherence::lang::cast
                    <::coherence::lang::Managed<CacheD3Context>::View>(vEvent->getOldValue());
               ID3ContextPtr icPtr = ID3ContextPtr(new CacheD3Context(*vD3Context));
               _iListener->onEntryDeleted(icPtr);
          }
          void CacheD3ContextMapListener::setRepositoryListener(ID3ContextRepositoryListenerPtr iListener)
          {
               _iListener = iListener;
          }


          void CacheD3ContextRepository::setRepositoryListener(ID3ContextRepositoryListenerPtr iListener)
          *{*
          *     if (_cache != NULL)*
          *     {*
          *          _cListenerPtr = new CacheD3ContextMapListener();*
          *          _cListenerPtr->setRepositoryListener(iListener);*
          *          cache->addFilterListener(cListenerPtr);*
          *     }*
          *}*