Apache Ignite cache event registration not working

痞子三分冷 提交于 2020-01-07 04:19:48

问题


I am trying to run following sample code. When I am running Apache Ignite server as stand alone service and run code as client then it does not work. I am unable to identify what I have missed. I am referring link https://github.com/apache/ignite/blob/master/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheEventsExample.java and I am interested to get the drop events.

package ignite;

import static org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_PUT;
import static org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ;
import static org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED;
import static org.apache.ignite.events.EventType.EVT_TASK_FAILED;
import static org.apache.ignite.events.EventType.EVT_TASK_FINISHED;
import static org.apache.ignite.events.EventType.EVT_TASK_REDUCED;
import static org.apache.ignite.events.EventType.EVT_TASK_SESSION_ATTR_SET;
import static org.apache.ignite.events.EventType.EVT_TASK_STARTED;
import static org.apache.ignite.events.EventType.EVT_TASK_TIMEDOUT;

import java.util.UUID;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.events.CacheEvent;
import org.apache.ignite.lang.IgniteBiPredicate;
import org.apache.ignite.lang.IgnitePredicate;

public class IgnitePublisher {
    private static final String CACHE_NAME = "myStreamCache";

    public static void main(String[] args) throws Exception {
        System.out.println("Run event example");
        Ignition.setClientMode(true);


        int[] events = {EVT_TASK_STARTED, 
                EVT_TASK_FINISHED, 
                EVT_TASK_FAILED, 
                EVT_TASK_TIMEDOUT, 
                EVT_TASK_SESSION_ATTR_SET, 
                EVT_TASK_REDUCED,
                EVT_CACHE_OBJECT_PUT, 
                EVT_CACHE_OBJECT_READ, 
                EVT_CACHE_OBJECT_REMOVED};

        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setIncludeEventTypes(events);
        Ignite ignite = Ignition.start(cfg);

        CacheConfiguration<Integer, Person> cacheConfiguration = new CacheConfiguration<Integer, Person>(CACHE_NAME);
        cacheConfiguration.setIndexedTypes(Integer.class, Person.class);
        //cacheConfiguration.setCacheMode(CacheMode.PARTITIONED);

        IgniteCache<Integer, Person> cache = ignite.getOrCreateCache(cacheConfiguration);

         IgniteBiPredicate<UUID, CacheEvent> locLsnr = new MyIgniteBiPredicateCacheEvent() ;

         IgnitePredicate<CacheEvent> remoteLsnr = new MyIgnitePredicateCacheEvent();

        System.out.println("EVT_CACHE_OBJECT_PUT : "+ignite.events(ignite.cluster().forCacheNodes(CACHE_NAME)).isEnabled(EVT_CACHE_OBJECT_PUT));
        ignite.events(ignite.cluster().forCacheNodes(CACHE_NAME)).remoteListen(locLsnr, remoteLsnr, events);



        for (int i = 0; i < 100; i++) {
            Person person = new Person(i, i, "name_" + i, (i * 100) % 3000);
            System.out.println("putting--" + person);
            cache.put(i, person);
            Thread.sleep(1*1000);
        }
    }

}

Here is implementation of Local Listener -

package ignite;

import java.util.UUID;

import org.apache.ignite.events.CacheEvent;
import org.apache.ignite.lang.IgniteBiPredicate;

public class MyIgniteBiPredicateCacheEvent implements IgniteBiPredicate<UUID, CacheEvent> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override public boolean apply(UUID uuid, CacheEvent evt) {
        System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() +
            ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());

        return true; // Continue listening.
    }
}

Here is implementation of Remote Listener

package ignite;

import org.apache.ignite.events.CacheEvent;
import org.apache.ignite.lang.IgnitePredicate;

public class MyIgnitePredicateCacheEvent implements IgnitePredicate<CacheEvent> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public boolean apply(CacheEvent evt) {
        System.out.println("Cache event: " + evt);
        return true;
    }
}

来源:https://stackoverflow.com/questions/42729647/apache-ignite-cache-event-registration-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!