简体   繁体   English

Hazelcast 和 JCache:不使用 XML 的自定义序列化程序

[英]Hazelcast and JCache: Custom serializers without using XML

Here is what I have:这是我所拥有的:

  • I am using Hazelcast v4.0.1我正在使用 Hazelcast v4.0.1
  • I am using JCache v1.1.1我正在使用 JCache v1.1.1
  • I have several custom Serializer s that I want to use.我有几个要使用的自定义Serializer

Here is my problem:这是我的问题:

  • My Serializer s have constructor dependencies so it's not possible to declare them via the.xml.我的Serializer具有构造函数依赖项,因此无法通过.xml 声明它们。
  • JCache "automagically" binds itself to Hazelcast on startup using the.xml. JCache 在启动时使用.xml“自动”将自身绑定到 Hazelcast。
  • Configuring the Config afterwards (ie adding the Serializer s to the config) does not result in the Serializer s being registred.之后配置Config (即将Serializer添加到配置中)不会导致Serializer被注册。

Solutions that I am looking for / that I have tried so far:我正在寻找/到目前为止我尝试过的解决方案:

  • Configuring the constructor dependencies into the.xml (can't be possible as these are specific runtime objects)将构造函数依赖项配置到 .xml 中(不可能,因为这些是特定的运行时对象)
  • Preventing the "automagical" bind from JCache, setting up the HazelcastInstance manually and the bind it to JCache somehow (don't know how to achieve this, don't even know if it's possible)防止JCache的“自动”绑定,手动设置HazelcastInstance并将其绑定到JCache(不知道如何实现这一点,甚至不知道是否可能)
  • Adding the Serializer s to the existing HazelcastInstance / Config (as said before, seems not to work)Serializer添加到现有的HazelcastInstance / Config (如前所述,似乎不起作用)

Question(s):问题):

Is there any way to get it work?有什么办法让它工作吗? Am I missing something completely obvious?我错过了一些完全明显的东西吗? Am I using the frameworks wrong?我是否使用错误的框架?

Assuming you build your Hazelcast Config with custom Serializer s programmatically, here is one way to start a named Hazelcast embedded member and reference it by name:假设您以编程方式使用自定义Serializer构建 Hazelcast Config ,这是启动命名 Hazelcast 嵌入式成员并按名称引用它的一种方法:

    Config config = new Config();
    // apart from your config, setup the instance name
    config.setInstanceName("jcache-test");

    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

    // request the server-side caching provider
    // same method to bind by instance name also applies to client-side caching provider
    CachingProvider cachingProvider = Caching.getCachingProvider(HazelcastCachingProvider.SERVER_CACHING_PROVIDER);

    // request the default CacheManager(url = null & ClassLoader = null)
    // indicating it should be bound to the named HazelcastInstance
    CacheManager cacheManager = cachingProvider.getCacheManager(null, null,
            HazelcastCachingProvider.propertiesByInstanceName("jcache-test"));

    // use the CacheManager as usual
    Cache<String, String> cache = cacheManager.createCache("cache", new MutableConfiguration<>());
    cache.put("1", "a");
    System.out.println(cache.get("1"));

There are more ways to achieve binding a CacheManager to an explicitly configured HazelcastInstance , you can have a look at the examples in this reference manual section .还有更多方法可以实现将CacheManager绑定到显式配置的HazelcastInstance ,您可以查看此参考手册部分中的示例。

As a sidenote, in general Hazelcast expects that the Config it is started with is final on startup.作为旁注,通常 Hazelcast 期望它启动的Config在启动时是最终的。 Mutations to the Config object after Hazelcast is started are not taken into account, except for specific data structure configuration that can be added after startup as discussed here .不考虑 Hazelcast 启动后对Config object 的突变,除了可以在此处讨论的启动后添加的特定数据结构配置外。

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

相关问题 在将ArrayList的派生值用作Hazelcast中的值时需要自定义序列化程序 - Need for custom serializers while using derivatives of ArrayList as value in Hazelcast 使用带有hazelcast的jcache时获取IllegalArgumentException - Getting IllegalArgumentException while using jcache with hazelcast 如何以编程方式使用JCache并在后面使用writw配置Hazelcast? - How to configure Hazelcast using JCache programmatically and using writw behind? 如何在Hazelcast中使用kryo实现多个串行器? - How to implement multiple serializers using kryo in Hazelcast? Spring Boot中的Hazelcast和JCache创建两个实例 - Hazelcast and JCache in Spring Boot creates two instances Hazelcast JCache CacheEntryListener触发过多 - Hazelcast JCache CacheEntryListener triggered too often Jackson ObjectMapper使用自定义序列化器和反序列化器 - Jackson ObjectMapper using custom Serializers and Deserializers 不使用 XML 的自定义吐司 - Custom toast without using XML 请求hazelcast jcache提供程序时,找不到HazelcastClientCachingProvider类异常 - HazelcastClientCachingProvider class not found exception when requesting hazelcast jcache provider 尝试使Hazelcast群集与兼容JCache的客户端一起使用时发生异常 - Exception while trying to make Hazelcast cluster work with JCache compliant client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM