如何使用 JCache 在客户端-服务器拓扑中使用 Hazelcast?

How to work with Hazelcast in client-server topology with JCache?

我正在使用 Jakarta EE 8、JCache API 1.1.1 和 Hazelcast 客户端 4.2.1 和服务器(尝试过 4.2.1 和 5.0-beta)。我为缓存管理器创建了提供程序:

@ApplicationScoped
public class MyCacheManager {

   private CacheManager manager;

   @Produces
   public CacheManager getCacheManager() {
       if (manager == null) {
           var provider = Caching.getCachingProvider();
           manager = provider.getCacheManager();
       }
       return manager;
   }

   public void close(@Disposes final CacheManager instance) {
       var provider = instance.getCachingProvider();
       provider.close();
   }
}

我这样创建缓存:

@ApplicationScoped
public class SessionCacheBean extends CacheBean{
    @Inject
    private CacheManager manager;
    @Override
    protected Cache getCache() {
        if (manager.getCache("session-cache") == null) {
            var config = new MutableConfiguration<String, Object>();
            config.setTypes(String.class, Object.class);
            config.setStoreByValue(true);
            config.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(
                    new Duration(TimeUnit.MINUTES, 30)));
            return manager.createCache("session-cache", config);
        } else {
            return manager.getCache("session-cache");
        }
    }
}

如果应用程序配置为集群成员,则此代码可以正常工作。但是现在配置为客户端:

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
                  http://www.hazelcast.com/schema/client-config/hazelcast-client-config-5.0.xsd">
    <cluster-name>mimedia-session</cluster-name>
    <network>
        <aws enabled="true">
            <access-key>*****</access-key>
            <secret-key>*****</secret-key>
            <region>****</region>
            <tag-key>****</tag-key>
            <tag-value>****</tag-value>
            <use-public-ip>true</use-public-ip>
        </aws>
    </network>
</hazelcast-client>

服务器使用相同的 hazelcast.xml:

命令 sudo hz start -c /usr/share/hazelcast4/bin/hazelcast.xml -j /lib/cache-api-1.1.1.jar 启动
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
           http://www.hazelcast.com/schema/config/hazelcast-config-4.2.xsd">

    <cluster-name>mimedia-session</cluster-name>

    <network>
        <port auto-increment="true" port-count="100">5701</port>
        <join>
            <auto-detection enabled="true"/>
            <multicast enabled="false"/>
            <aws enabled="true">
                <tag-key>****</tag-key>
                <tag-value>****</tag-value>
            </aws>
        </join>
    </network>
    <cache name="session-cache">
        <key-type class-name="java.lang.String"/>
        <value-type class-name="java.lang.Object"/>
        <management-enabled>true</management-enabled>
        <expiry-policy-factory>
            <timed-expiry-policy-factory expiry-policy-type="TOUCHED"
                                                    duration-amount="30"
                                                    time-unit="MINUTES"/>
        </expiry-policy-factory>
    </cache>
</hazelcast>

现在应用 return 行 if (manager.getCache("session-cache") == null) 出错

"java.io.InvalidClassException: javax.cache.configuration.FactoryBuilder$SingletonFactory; local class incompatible: stream classdesc serialVersionUID = 201305101634, local class serialVersionUID = 201402"

当我在服务器配置应用程序 return 中删除 <cache name="session-cache"> 时,第 return manager.createCache("session-cache", config);

行出现错误
"java.io.InvalidClassException: javax.cache.configuration.FactoryBuilder$SingletonFactory; local class incompatible: stream classdesc serialVersionUID = 201402, local class serialVersionUID = 201305101634"

我需要在哪里修复 class 不匹配?

我认为问题出在我使用的是 TomEE。因为 serialVersionUID = 201402 位于 geronimo-jcache_1.0_spec,但我在我的项目中使用 jsr107spec。找到这个问题的解决方案后,我会更新答案。

更新

解决方法:我为用作服务器的 TomEE 创建了 'cache-server.war'