2017-11-25

Integration with Hazelcast (Extension Module)

Tamaya Hazelcast is an extension module. Refer to the extensions documentation for further details.

What functionality this module provides ?

Tamaya Hazelcast provides a property source which uses Hazelcast as configuration backend. Hereby the module supports read-only integration (as a HazelcastPropertySource as well as a writing configuration changes back (based on Tamaya’s MutableConfiguration API defined by the tamaya-mutable-config extension module.

Compatibility

The module is based on Java 7, so it will not run on Java 7 and beyond.

Installation

To use tamaya-hazelcast you only must add the corresponding dependency to your module:

<dependency>
  <groupId>org.apache.tamaya.ext</groupId>
  <artifactId>tamaya-hazelcast</artifactId>
  <version>{tamaya_version}</version>
</dependency>

The Extensions Provided

Hazelcast integration comes basically with 2 artifacts:

  • The org.apache.tamaya.hazelcast.HazelcastPropertySource is a PropertySource. The property source is not automatically registered. Either register it using the ServiceLoader yourself or implement and register a corresponding PropertySourceProvider.

  • If the tamaya-mutable-config module is loaded it is possible to write property values back into the consul cluster, by accessing a MutableConfiguration using the URI config:hazelcast.

Access of consul key/value pairs is through the normal Tamaya API.

The HazelcastPropertySource

The HazelcastPropertySource is not automatically registered and provides different options how to integrate Tamaya with Hazelcast.

/**
 * Creates a new instance, hereby using {@code "Hazelcast"} as property source name and
 * a default hazelcast backend created by calling {@link Hazelcast#newHazelcastInstance()}.
 */
public HazelcastPropertySource();

/**
 * Creates a new instance, hereby using {@code "Hazelcast"} as property source name and the
 * given hazelcast instance.
 * @param hazelcastInstance the hazelcast instance, not null.
 */
public HazelcastPropertySource(HazelcastInstance hazelcastInstance);

/**
 * Creates a new instance, hereby using the given property source name and
 * a default hazelcast backend created by calling {@link Hazelcast#newHazelcastInstance()}.
 * @param name the property source name, not null.
 */
public HazelcastPropertySource(String name);

/**
 * Creates a new instance, hereby using the given property source name and
 * a creating a new hazelcast backend using the given Hazelcast {@link Config}.
 * @param config the hazelcast config, not null.
 * @param name the property source name, not null.
 */
public HazelcastPropertySource(String name, Config config);

/**
 * Creates a new instance, hereby using the given property source name and the
 * hazelcast instance.
 * @param name
 * @param hazelcastInstance
 */
public HazelcastPropertySource(String name, HazelcastInstance hazelcastInstance);

To use hazelcast as a configuration backend, you simply create the corresponding Hazelcast instance and use it to initialize the Tamaya property source. Given that a hazelcast backedn configuration can be easily created asillustrated below:

Config hazelcastConfig = new Config();
// define config settings
HazelcastInstance hazelcastInstance = Hazelcast.newInstance(hazelcastConfig);
HazelcastPropertySource ps = new HazelcastPropertySource(hazelcastInstance);
ps.setName("myHazelcast-config");
ps.setOrdinal(2000);
// Build your own configuration context
ConfigurationContextBuilder b = ConfigurationProvider.createNewConfigurationContextBuilder();
b.addDefaultPropertyConverters().addDefaultPropertyFilter().addDefaultPropertySources();
// Add the hazelcast property source (as most significant)
b.addPropertySource(ps);
// build and use the configuration
Configuration config = b.build();