请参阅以下事件监听器代码。
尝试成为听众的客户端节点:
Ignition.setClientMode(true);
IgniteConfiguration cfg = new IgniteConfiguration();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(Arrays.asList("127.0.0.1", "127.0.0.1:47500..47509"));
TcpDiscoverySpi spi = new TcpDiscoverySpi();
spi.setIpFinder(ipFinder);
cfg.setDiscoverySpi(spi);
cfg.setIncludeEventTypes(EventType.EVTS_CACHE);
IgnitePredicate<CacheEvent> rmtLsnr = (CacheEvent cacheEvent) -> {
if(cacheEvent.type() == EventType.EVT_CACHE_OBJECT_PUT) {
System.out.println(cacheEvent.name() + "," + cacheEvent.key());
}
return true;
};
Ignite ignite = Ignition.start(cfg);
ignite.events(ignite.cluster()
.forCacheNodes(TestConstants.IgniteEventsTest.ORDER_CACHE))
.remoteListen(null,rmtLsnr,EventType.EVTS_CACHE);
托管缓存的服务器节点:
IgniteConfiguration cfg = new IgniteConfiguration();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(Arrays.asList("127.0.0.1", "127.0.0.1:47500..47509"));
TcpDiscoverySpi spi = new TcpDiscoverySpi();
spi.setIpFinder(ipFinder);
cfg.setDiscoverySpi(spi);
Ignite ignite = Ignition.start(cfg);
CacheConfiguration cacheCfg = new CacheConfiguration(TestConstants.IgniteEventsTest.ORDER_CACHE);
cacheCfg.setCacheMode(CacheMode.PARTITIONED);
cacheCfg.setWriteSynchronizationMode(FULL_ASYNC);
cacheCfg.setBackups(1);
远程客户端更新缓存:
Ignition.setClientMode(true);
IgniteConfiguration cfg = new IgniteConfiguration();
TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
ipFinder.setAddresses(Arrays.asList("127.0.0.1", "127.0.0.1:47500..47509"));
TcpDiscoverySpi spi = new TcpDiscoverySpi();
spi.setIpFinder(ipFinder);
cfg.setDiscoverySpi(spi);
Ignite ignite = Ignition.start(cfg);
CacheConfiguration cacheCfg = new CacheConfiguration(TestConstants.IgniteEventsTest.ORDER_CACHE);
cacheCfg.setCacheMode(CacheMode.PARTITIONED);
cacheCfg.setWriteSynchronizationMode(FULL_SYNC);
IgniteCache<Integer, Order>
clientOrderCache = ignite.getOrCreateCache(cacheCfg);
in a loop...
clientOrderCache.put(key , order);
我的观察是,当事件监听器上的ClientMode = true时,我们不接收事件。 如果事件监听器必须处于服务器模式以监听事件,请告诉我吗?或者我做错了什么?
答案 0 :(得分:1)
您没有定义本地侦听器(代码中的null
)。始终在触发事件的地方执行远程过滤器,在高速缓存更新的情况下,它是其中一个服务器节点。如果您也创建了本地侦听器,则远程过滤器返回true
的所有事件都将传播到客户端。
请参阅CacheEventsExample [1]以获取代码示例。