首页 > 解决方案 > Apache Ignte:如何在 XML 缓存配置中定义 CacheInterceptors

问题描述

我在 XML 中定义了一个缓存配置,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:util="http://www.springframework.org/schema/util" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">
    <!--
        Alter configuration below as needed.
    -->
    <bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">

        <!-- test configuration -->
        <property name="cacheConfiguration">
            <bean class="org.apache.ignite.configuration.CacheConfiguration">
                <property name="name" value="TestCache" />
                <property name="cacheMode" value="LOCAL" />
            </bean>
        </property>

    </bean>
</beans>

此外,我实现了一个 CacheInterceptor:

package com.avl.drive.dataservice.setup.test;

import javax.cache.Cache.Entry;

import org.apache.ignite.cache.CacheInterceptor;
import org.apache.ignite.lang.IgniteBiTuple;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

public class TestCacheInterceptor<K, V> implements CacheInterceptor<K, V> {

    private static final long serialVersionUID = 8568641462030568760L;
    private static Logger logger = LogManager.getLogger("ConsoleLogger");

    @Override
    public V onGet(K key, V val) {
        logger.debug("TestCacheInterceptor onGet");
        return null;
    }

    @Override
    public V onBeforePut(Entry<K, V> entry, V newVal) {
        logger.debug("TestCacheInterceptor onBeforePut");
        return null;
    }

    @Override
    public void onAfterPut(Entry<K, V> entry) {
        logger.debug("TestCacheInterceptor onAfterPut");
    }

    @Override
    public @Nullable IgniteBiTuple<Boolean, V> onBeforeRemove(Entry<K, V> entry) {
        logger.debug("TestCacheInterceptor onBeforeRemove");
        return null;
    }

    @Override
    public void onAfterRemove(Entry<K, V> entry) {
        logger.debug("TestCacheInterceptor onAfterRemove");
    }
}

在 Ignite 文档中,我只找到了使用 Java 配置添加拦截器的示例。

如何在 XML 缓存配置中添加此 CacheInterceptor?

标签: javaxmlconfigurationignite

解决方案


找到了解决方案...

CacheConfiguration 必须如下所示:

    <!-- test configuration -->
    <property name="cacheConfiguration">
        <bean class="org.apache.ignite.configuration.CacheConfiguration">
            <property name="name" value="TestCache" />
            <property name="cacheMode" value="LOCAL" />
            <property name="interceptor">
                <bean class="com.avl.drive.dataservice.setup.test.TestCacheInterceptor" />
            </property>
        </bean>
    </property>

推荐阅读