简体   繁体   中英

Hazelcast- Right place to register a Map Listener with a IMap

I have the following use-case

I have two Spring boot applications on two separate machines. One application is running with an embedded hazelcast and another application is connecting to the hazelcast running embedded.

I have two maps- one IMap and one MultiMap. I want to add an EntryEvictionListener to the IMAP. WhaI I want to do is on the eviction of an entry from the IMap , go to the MuliMap and remove the corresponding entry from it.

I am using Spring java configuration. I wanted advice on where should I register the listener with the IMap . The class which implements EntryEvictionListener interface( which will be registered with IMap ), it is a spring managed bean and also has other spring managed beans autowired inside of it.

I was planning to register the mapListener in the spring boot application which connects to the embedded hazelcast running in other spring boot application. I was planning to do it inside a postconstruct method, so it runs only once.

Is this a good approach?

Thank you in advance.

EDIT-

           Class CustomListener implements HazelcastInstanceAware,EntryEvictedListener{

   private HazelcastInstance hazelcastInstance;

   @Override
   public void setHazelcastInstance(HazelcastInstance hazelcastInstance){

   this.hazelcastInstance=hazelcastInstance;

   }

   @Override
   public void entryEvicted(EntryEvent<String,String> event){

   // get multi map from hazelcast instance

   //remove value

  }


}

The above works!

@indraneel-bende, please check this: http://docs.hazelcast.org/docs/latest-development/manual/html/Distributed_Events/Distributed_Object_Events/Listening_for_Map_Events.html#page_Registering+Map+Listeners

If you use Hazelcast-Spring config, you can add the listener in the Hazelcast Config, could be XML config like in the doc or Java config, and that's it. Make sure that your MapListener is a Spring bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM