You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hazelcast:IMap注册EntryEvictionListener的合理位置咨询

Hey there! Let's walk through how to implement your requirement step by step using Spring's Java configuration. I'll cover both the embedded Hazelcast server setup, the client app, and the eviction listener logic that keeps your IMap and MultiMap in sync.

1. Configure the Embedded Hazelcast Instance (First Spring Boot App)

First, we'll set up the embedded Hazelcast instance with your IMap, MultiMap, and register the eviction listener directly in the configuration.

@Configuration
public class HazelcastEmbeddedConfig {

    @Bean
    public HazelcastInstance hazelcastInstance() {
        Config config = new Config();
        
        // Configure IMap with eviction policy (required to trigger eviction events)
        MapConfig mapConfig = new MapConfig("your-imap-name")
                .setEvictionConfig(new EvictionConfig()
                        .setEvictionPolicy(EvictionPolicy.LRU)
                        .setMaxSizePolicy(MaxSizePolicy.PER_NODE)
                        .setSize(1000)); // Adjust size based on your memory needs

        // Configure MultiMap
        MultiMapConfig multiMapConfig = new MultiMapConfig("your-multimap-name")
                .setValueCollectionType(ValueCollectionType.SET); // Use LIST if you need duplicate values

        // Register the custom eviction listener to the IMap
        mapConfig.addEntryListenerConfig(new EntryListenerConfig(
                new EntryEvictionListenerImpl(),
                null,
                false,
                false));

        config.addMapConfig(mapConfig);
        config.addMultiMapConfig(multiMapConfig);

        // Configure network for cluster discovery (adjust IP for your environment)
        config.getNetworkConfig()
                .getJoin()
                .getMulticastConfig().setEnabled(false)
                .getTcpIpConfig().setEnabled(true)
                .addMember("your-embedded-server-ip:5701");

        return Hazelcast.newHazelcastInstance(config);
    }

    // Expose IMap as a Spring bean for easy injection
    @Bean
    public IMap<String, Object> yourIMap(HazelcastInstance hazelcastInstance) {
        return hazelcastInstance.getMap("your-imap-name");
    }

    // Expose MultiMap as a Spring bean
    @Bean
    public MultiMap<String, Object> yourMultiMap(HazelcastInstance hazelcastInstance) {
        return hazelcastInstance.getMultiMap("your-multimap-name");
    }
}

2. Implement the EntryEvictionListener

Create the listener class that deletes corresponding entries from the MultiMap when an IMap entry is evicted. We'll inject the MultiMap directly (Spring handles the cluster-aware reference for us).

@Component
public class EntryEvictionListenerImpl implements EntryEvictionListener<String, Object> {

    private final MultiMap<String, Object> yourMultiMap;

    // Constructor injection (preferred over field injection for testability)
    public EntryEvictionListenerImpl(MultiMap<String, Object> yourMultiMap) {
        this.yourMultiMap = yourMultiMap;
    }

    @Override
    public void evicted(Iterable<EntryEvent<String, Object>> events) {
        // Iterate over all evicted entries and clean up the MultiMap
        for (EntryEvent<String, Object> event : events) {
            String evictedKey = event.getKey();
            // Remove all values linked to this key from MultiMap
            yourMultiMap.remove(evictedKey);
            // If you need to remove a specific value instead of all, use:
            // yourMultiMap.remove(evictedKey, event.getValue());
        }
    }
}

3. Configure the Hazelcast Client (Second Spring Boot App)

For the second app connecting to the embedded Hazelcast instance, set up a client configuration to access the same distributed IMap and MultiMap.

@Configuration
public class HazelcastClientConfig {

    @Bean
    public HazelcastInstance hazelcastClient() {
        ClientConfig clientConfig = new ClientConfig();
        // Point to the embedded server's IP and port
        clientConfig.getNetworkConfig()
                .addAddress("your-embedded-server-ip:5701");

        return HazelcastClient.newHazelcastClient(clientConfig);
    }

    // Expose IMap bean for client app usage
    @Bean
    public IMap<String, Object> yourIMap(HazelcastInstance hazelcastClient) {
        return hazelcastClient.getMap("your-imap-name");
    }

    // Expose MultiMap bean for client app usage
    @Bean
    public MultiMap<String, Object> yourMultiMap(HazelcastInstance hazelcastClient) {
        return hazelcastClient.getMultiMap("your-multimap-name");
    }
}

Key Notes to Keep in Mind

  • Eviction Policy Requirement: The IMap must have an eviction policy configured (like LRU in the example) — without this, no eviction events will fire. Tweak setMaxSizePolicy and setSize to match your application's memory constraints.
  • Cluster Connectivity: Ensure both apps can communicate over the network. Use TCP/IP configuration (as shown) for production environments instead of multicast, and double-check IP addresses/ports.
  • Listener Execution Context: The evicted method runs on the Hazelcast node where the entry was evicted. Since MultiMap is a distributed structure, the remove operation will automatically sync across the entire cluster.
  • Error Handling: Consider adding try-catch blocks in the listener to log failures gracefully — unhandled exceptions here could disrupt Hazelcast's internal operations.

内容的提问来源于stack exchange,提问作者Indraneel Bende

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:19:50