java ignite listner 示例未获取事件
java ignite listner example doesn't get events
我试图在 Ignite 缓存上接收一些事件。我的 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>
部分代码取自 ignite 示例:
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();
无法打印收到的事件。可能我的配置有问题。我在代码中尝试配置:
cfg.setIncludeEventTypes(EVTS_CACHE);
没有效果。
无法从客户端节点本地捕获与缓存相关的事件。例如,在实际存储具有特定键的主分区的服务器节点上触发 CACHE_PUT 事件,而不是从调用 #put 方法的节点触发。
您可以从服务器监听本地事件:
IgniteEvents events = server.events();
events.localListen(locLsnr, EVTS_CACHE);
或者监听来自客户端的远程事件:
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);
请注意,需要明确为您的服务器启用事件:
Ignite server = Ignition.start(new IgniteConfiguration()
.setIgniteInstanceName("server")
.setIncludeEventTypes(EVTS_CACHE)
请查看文档以获取更多详细信息:
https://www.gridgain.com/docs/latest/developers-guide/events/listening-to-events
我试图在 Ignite 缓存上接收一些事件。我的 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>
部分代码取自 ignite 示例:
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();
无法打印收到的事件。可能我的配置有问题。我在代码中尝试配置:
cfg.setIncludeEventTypes(EVTS_CACHE);
没有效果。
无法从客户端节点本地捕获与缓存相关的事件。例如,在实际存储具有特定键的主分区的服务器节点上触发 CACHE_PUT 事件,而不是从调用 #put 方法的节点触发。
您可以从服务器监听本地事件:
IgniteEvents events = server.events();
events.localListen(locLsnr, EVTS_CACHE);
或者监听来自客户端的远程事件:
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);
请注意,需要明确为您的服务器启用事件:
Ignite server = Ignition.start(new IgniteConfiguration()
.setIgniteInstanceName("server")
.setIncludeEventTypes(EVTS_CACHE)
请查看文档以获取更多详细信息: https://www.gridgain.com/docs/latest/developers-guide/events/listening-to-events