Search code examples
javaeventslistenerconfigignite

java ignite listner example doesn't get events


I trying to receive some events on Ignite cache. My config.xml:

<bean abstract="true" id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
    <property name="workDirectory" value="/opt/apache-ignite-2.8.1-bin"/>

    <!-- Set to true to enable distributed class loading for examples, default is false. -->
    <property name="peerClassLoadingEnabled" value="true"/>

    <!-- Enable task execution events for examples. -->
    <property name="includeEventTypes">
        <util:constant static-field="org.apache.ignite.events.EventType.EVTS_CACHE"/>
    </property>

Code partialy taken from ignite examples:

    IgniteConfiguration cfg = new IgniteConfiguration();


    cfg.setClientMode(true);
    cfg.setPeerClassLoadingEnabled(true);

    TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
    ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
    cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));


    Ignite ignite = Ignition.start(cfg);

    IgnitePredicate<CacheEvent> locLsnr = evt -> {
        System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() +
                ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());

        return true; // Continue listening.
    };

    ignite.events().localListen(locLsnr,
            EventType.EVT_CACHE_OBJECT_PUT,
            EventType.EVT_CACHE_OBJECT_READ,
            EventType.EVT_CACHE_OBJECT_REMOVED);

    final IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myCache");

    for (int i = 0; i < 20; i++)
        cache.put(i, Integer.toString(i));

    System.out.println(">> Created the cache and add the values.");

    ignite.close();

Cant print received events. Probably have issues in my config. I have trying config in code:

cfg.setIncludeEventTypes(EVTS_CACHE);

without effect.


Solution

  • The cache-related events can't be locally captured from a client node. For a sample, a CACHE_PUT event is being fired on the server node that actually stores the primary partition with the particular key, not from a node, from which the #put method is called.

    You can either listen for local events from a server:

        IgniteEvents events = server.events();
        events.localListen(locLsnr, EVTS_CACHE);
    

    Or to listen for remote events from a client:

        IgniteEvents events = ignite.events();        
        events.remoteListen(new IgniteBiPredicate<UUID, CacheEvent>() {
            @Override
            public boolean apply(UUID uuid, CacheEvent e) {
                // process the event
                return true; //continue listening
            }
        }, evt -> {
            System.out.println("remote event: " + evt.name());
            return true;
        }, EVTS_CACHE);
    

    Note, that it's required to explicitly enable events for your server:

    Ignite server = Ignition.start(new IgniteConfiguration()
        .setIgniteInstanceName("server")
        .setIncludeEventTypes(EVTS_CACHE)
    

    Please, check the documentation for more details: https://www.gridgain.com/docs/latest/developers-guide/events/listening-to-events