JEE - 每天重置ejb单身人士

问题描述 投票:0回答:1

我正在使用JEE,我创建了EJB-Singleton。它具有字段LRUMap,表示用于在应用程序中存储数据的缓存。我想在单例内清除数据,或者只是在运行时杀死并重新启动整个单例。有没有选择定期这样做?例如,为了清除数据,每天重置一次单独的bean?

这是代码:

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class BpNotificationCacheBean {

    private static final int CACHE_SIZE = 1000;

    private Map<String, Boolean> cacheMap;

    @PostConstruct
    public void init() {
        cacheMap = new LRUMap(CACHE_SIZE);
    }

    @Lock(LockType.WRITE)
    public Boolean get(String businessPartnerId) {
        return cacheMap.get(businessPartnerId);
    }

    @Lock(LockType.WRITE)
    public void put(String businessPartnerId, Boolean isVIP) {
        this.cacheMap.put(businessPartnerId, isVIP);
    }
}
java-ee singleton ejb runtime
1个回答
2
投票

如果你在一个完整的JEE环境中,我会创建一个Schedule注释方法:

import javax.ejb.Schedule;

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class BpNotificationCacheBean {

    private static final int CACHE_SIZE = 1000;

    private Map<String, Boolean> cacheMap;

    ...

    @Lock(LockType.WRITE)
    @Schedule(hour = "1", persistent = false)
    private void resetCache() {
        cacheMap = new LRUMap(CACHE_SIZE);
    }
}

关键是Schedule annotation,它是Java EE Timer Service的一部分。我显示的注释每天运行01:00(凌晨1点),但请参阅文档以显示如何更改它。

© www.soinside.com 2019 - 2024. All rights reserved.