Infinispan @CacheEntryExpired监听器 - 事件触发两次

时间:2016-03-15 15:00:32

标签: java infinispan

我通过infinispan cache .addListener()方法添加了一个CacheEntryExpired侦听器实现。

在条目到期时触发侦听器事件。 问题是,每次事件被触发两次。

我验证了(使用debugger和cache.getListeners())缓存不包含我的同一个Listener的两个实例。 getListeners的结果是:

     (java.util.Collections$UnmodifiableSet<E>) [org.infinispan.iteration.impl.LocalEntryRetriever$PartitionListener@666SAT, my.supercool.package.MyExpirationEventListenerImpl@666TAN]

所以只有一个听众。监听器实现接口:

@Listener
public interface TokenExpirationEventListener<T> {

@CacheEntryExpired
   public void entryExpired(CacheEntryExpiredEvent<String, T> event);       
}

实现如下:

@Override
@CacheEntryExpired
public void entryExpired(CacheEntryExpiredEvent<String, T> event) {
    CODE
}

但是从界面中删除@CacheEntryExpired和@Listener注释(只是尝试)并没有导致事件只发生一次。

我有什么问题,我在每个条目到期事件中都会收到两个事件吗?

infinispan版本是8.2.0.final。

EDIT2: 我能够找到导致问题的所有配置。允许重现错误的代码是:

import java.util.concurrent.TimeUnit;

import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ClusteringConfigurationBuilder;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.configuration.global.TransportConfigurationBuilder;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryExpired;
import org.infinispan.notifications.cachelistener.event.CacheEntryExpiredEvent;

@Listener
public class TestMain { 

private Cache<String, String> cache;

public static void main(String[] args) throws Exception {
    new TestMain().test();
}

private void test() throws Exception{
    configureInfinispan();

    cache.put("test", "test", 10, TimeUnit.SECONDS);
    cache.put("test2", "test", 10, TimeUnit.SECONDS);       
    while(true){
        Thread.sleep(100);          
    }
}



public void configureInfinispan(){
    DefaultCacheManager defaultCacheManager = configureCachaManager();//globalConfigurationBuilder.build(), defaultConfigurationBuilder.build(), /*startNow*/ true)

    Configuration conf = defaultCacheManager.getDefaultCacheConfiguration();
    ConfigurationBuilder cacheConfigBuilder = new ConfigurationBuilder().read(conf);
    cacheConfigBuilder.expiration().enableReaper().wakeUpInterval(5, TimeUnit.SECONDS); //to speed test results


    duplicationReason2(cacheConfigBuilder);
    defaultCacheManager.defineConfiguration("testCache", cacheConfigBuilder.build());

    cache = defaultCacheManager.getCache("testCache");
    cache.addListener(new TestMain());
    System.out.println("conf");
}

public DefaultCacheManager configureCachaManager(){
    GlobalConfigurationBuilder globalConfigurationBuilder = new GlobalConfigurationBuilder();
    ConfigurationBuilder defaultConfigurationBuilder = new ConfigurationBuilder();
    duplicationReason1(defaultConfigurationBuilder, globalConfigurationBuilder);

    return new DefaultCacheManager(globalConfigurationBuilder.build(), defaultConfigurationBuilder.build(), true);
}

@CacheEntryExpired
public void entryExpired(CacheEntryExpiredEvent<String, String> event) {
    System.out.println("Expired:" + event.getKey());
}

private void duplicationReason2(ConfigurationBuilder configurationBuilder) {
    configurationBuilder.persistence() //enable persistence
            .passivation(false)
            .addSingleFileStore()
            .location("C:/test/infinispan")
            // Disable writing anything to the file except when we do shutdown
            .maxEntries(0)
            .shared(false)
            .fetchPersistentState(true)
            .async() //write-behind
            .enable() //write-behind
            .threadPoolSize(Runtime.getRuntime().availableProcessors()) //writing threads
            .preload(true); //load data from file on startup
}

private void duplicationReason1(ConfigurationBuilder configurationBuilder, GlobalConfigurationBuilder globalConfigurationBuilder){
    configureCacheOperationalMode(configurationBuilder);
    configureTransport(globalConfigurationBuilder);
}

private void configureCacheOperationalMode(ConfigurationBuilder configurationBuilder) {
    ClusteringConfigurationBuilder clusteringConfigurationBuilder = configurationBuilder.clustering();
    clusteringConfigurationBuilder.cacheMode(CacheMode.REPL_ASYNC);
}

private void configureTransport(GlobalConfigurationBuilder globalConfigurationBuilder) {
    TransportConfigurationBuilder transportConfigurationBuilder = globalConfigurationBuilder.transport().defaultTransport();
    transportConfigurationBuilder.addProperty("configurationFile", "default-configs/default-jgroups-udp.xml");
}

}

运行此将(不幸的是,您之后需要自己杀死主线程)导致事件被打印两次。

有两种方法可以共同导致这种情况。评论其中一个并留下第二个导致事件发生一次。

这些方法名为duplicationReason1和duplicationReason2。

也许在配置中我有些不明白的地方?

1 个答案:

答案 0 :(得分:2)

好的,我能够看到你添加的测试用例发生了什么,谢谢!

这是群集缓存atm中的一个错误。我记录了[1]来查看它,如果你想要更新,你应该关注。

虽然我们可以修复重复的问题,但您仍然可能会出现其他可能的重复项,因此您仍然可以处理它们。生成副本的示例可以在“并发过期访问”下的[2]处找到。因此,您通常应该忽略具有空值的到期事件。

[1] https://issues.jboss.org/browse/ISPN-6405 [2] http://blog.infinispan.org/2015/10/expiration-enhancements.html