[英]Hazelcast and JCache: Custom serializers without using XML
Here is what I have:这是我所拥有的:
Serializer
s that I want to use.我有几个要使用的自定义Serializer
。Here is my problem:这是我的问题:
Serializer
s have constructor dependencies so it's not possible to declare them via the.xml.我的Serializer
具有构造函数依赖项,因此无法通过.xml 声明它们。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:我正在寻找/到目前为止我尝试过的解决方案:
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(不知道如何实现这一点,甚至不知道是否可能)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.