Search code examples
oracle-coherence

Can I listen to multiple caches using same com.tangosol.util.MapListener instance?


Can I listen to multiple Coherence Caches using a single com.tangosol.util.MapListener instance? As can be seen in the below code snippet, I have created an instance of MapListener and using the same instance to listen to multiple coherence caches. My concern is that would this piece of code work in case of multiple Events being generated from both the caches?

protected class MapListenerImpl implements MapListener {

    @Override
    public void entryDeleted(MapEvent event) {
        System.out.println("Delete Event: " + event.getOldEntry());
    }

    @Override
    public void entryInserted(MapEvent event) {
        System.out.println("INsert Event: " + event.getNewEntry());

    }

    @Override
    public void entryUpdated(MapEvent event) {
        System.out.println("Update Event: " + event.getNewEntry());

    }       
}

//Main Method
public static void main(String[] args) {
    //Initializing the caches here
    NamedCache mapTrades1;
    NamedCache mapTrades2;

    MapListener listener = new MapListenerImpl();

    mapTrades1.addMapListener(listener);
    mapTrades2.addMapListener(listener);
}

Solution

  • You can, but it might be challenging to determine to which map an event corresponds. If you need to keep the events separate but you need to keep shared state, I would suggest creating separate listener objects but have them both hold a reference to a single shared state object.