我正在尝试运行以下示例代码。当我将Apache Ignite服务器作为独立服务运行并将代码作为客户端运行时,它不起作用。我无法确定我错过了什么。我指的是链接https://github.com/apache/ignite/blob/master/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheEventsExample.java,我有兴趣获得放弃事件。
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);
}
}
}
这是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.
}
}
这是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;
}
}