Apache Ignite 在触发事件后无法从侦听器访问缓存"EVT_CACHE_STARTED"



我希望能够监视Apache Ignite中的缓存创建事件。

每当这样的事件发生时,我希望能够在创建缓存之后,但在其他人插入之前,对这些缓存做些什么。

所以我用了当地的听众。以下是所有代码:

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.events.CacheEvent;
import org.apache.ignite.events.EventType;
import org.apache.ignite.lang.IgnitePredicate;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class ServerConfig {
public ServerConfig(Environment e) throws Exception {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIncludeEventTypes(EventType.EVT_CACHE_STARTED);
Ignite ignite = Ignition.start(cfg);
String cacheName = "test";
registerCacheCreationListener(ignite);
IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cacheName);
}
private void registerCacheCreationListener(Ignite ignite){
IgnitePredicate<CacheEvent> locLsnr = new IgnitePredicate<CacheEvent>(){
@IgniteInstanceResource
private Ignite ignite;
@Override
public boolean apply(CacheEvent evt) {
System.out.println("Received event [evt=" + evt.name() + " cacheName=" + evt.cacheName());
IgniteCache<Integer, String > cache = ignite.cache(evt.cacheName());      // CANNOT ACCESS evt.cacheName() - STUCKS HERE
System.out.println("finish listener");
return true;
}
};
ignite.events().localListen(locLsnr, EventType.EVT_CACHE_STARTED);
}
}

所以当我这样做的时候:

ignite.cache(evt.cacheName())

内部IgnitePredcate-据我所知,它还不可用。

请帮我找出我哪里错了。谢谢

通常,您不应该执行缓存操作,也不应该执行阻止或访问Ignite内部的大多数其他操作。事件应该非常快速和轻量级,这意味着它们是从Ignite线程和Ignite内部锁内部执行的。

只需在事件到达时在另一个线程中安排一个操作。

最新更新