Lord M-Cube
Lord M-Cube

Reputation: 361

Hazelcast and JCache: Custom serializers without using XML

Here is what I have:

Here is my problem:

Solutions that I am looking for / that I have tried so far:

Question(s):

Is there any way to get it work? Am I missing something completely obvious? Am I using the frameworks wrong?

Upvotes: 0

Views: 282

Answers (1)

Vassilis Bekiaris
Vassilis Bekiaris

Reputation: 823

Assuming you build your Hazelcast Config with custom Serializers programmatically, here is one way to start a named Hazelcast embedded member and reference it by name:

    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.

As a sidenote, in general Hazelcast expects that the Config it is started with is final on startup. 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.

Upvotes: 1

Related Questions