简体   繁体   English

Hazelcast:如何刷新Hazelcast实例?

[英]Hazelcast: How to refresh the Hazelcast Instance?

I am creating a service (ReST) which would be up and running all the time. 我正在创建将一直运行的服务(ReST)。 So in this service I am calling the init method from my spring context which hits the database and loads the required data into the hazelcast instance. 因此,在此服务中,我从我的spring上下文调用init方法,该方法会访问数据库并将所需数据加载到hazelcast实例中。

Now I have to ensure that I refresh the hazelcast instance by calling the init method. 现在,我必须确保通过调用init方法刷新hazelcast实例。 Let's say that the refresh period is every 6 hours. 假设刷新周期为每6小时一次。

Is there a good clean way to achieve this ? 有没有一个好的清洁方法来实现这一目标?

You can use Hazelcast eviction policy for this problem. 您可以使用Hazelcast逐出策略来解决此问题。

<time-to-live-seconds>3600</time-to-live-seconds>

which clears map content every 1 hour and when any request comes it reloads that map content from a loader. 它将每1小时清除一次地图内容,并且在收到任何请求时会从加载程序重新加载该地图内容。

Below is one of the Hazelcast map config 以下是Hazelcast地图配置之一

...
    <!--
        Maximum number of seconds for each entry to stay in the map. Entries that are
        older than <time-to-live-seconds> and not updated for <time-to-live-seconds>
        will get automatically evicted from the map.
        Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.
    -->
    <time-to-live-seconds>0</time-to-live-seconds>
...

Or You can add the time while adding data to cache map, after specified time, specific cache map can also be clean. 或者,您可以在将数据添加到高速缓存映射时添加时间,在指定的时间之后,特定的高速缓存映射也可以清除。

This can be easily achieved by implementing EntryListener interface provided by Hazelcast. 通过实现Hazelcast提供的EntryListener接口,可以轻松实现这一点。
Implement the below methods and call your init() method. 实现以下方法并调用您的init()方法。

IMap<> map = hzInstance.getMap("mapName");
map.addEntryListener(new MyMapEventListener());

public class MyMapEventListener implements EntryListener{
    @Override
    public void mapCleared(MapEvent mapEvent) {
        //Make your init call inside this
    }

    @Override
    public void mapEvicted(MapEvent mapEvent) {
        //Make your init call inside this
    }
}

Let me know if this makes sense. 让我知道这是否有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM