Over a million developers have joined DZone.

Hazelcast IMDG Tutorial: Implement Variable Replacer

DZone 's Guide to

Hazelcast IMDG Tutorial: Implement Variable Replacer

With Hazelcast, you can use Variables within XML configuration files. Here, we implement and use a custom Variable Replacer for a Hazelcast XML configuration file.

· Big Data Zone ·
Free Resource

Hazelcast IMDG 3.10 comes with a new feature: support for Variable Replacers. The Replacers allow for using placeholders (Variables) in configuration XML files. You can use Replacers to mask sensitive strings (such as passwords) or to provide values that depend on the current environment.

There was already a partial support for placeholders before version 3.10; it allows for referencing system properties:


Hazelcast version 3.10 extends the feature and adds the possibility to introduce your own Replacer implementations. Variable Replacers are classes that implement the ConfigReplacer interface:

public interface ConfigReplacer {
    void init(Properties properties);
    String getPrefix();
    String getReplacement(String maskedValue);

A Variable to be replaced within the configuration file has the following form:


...where the PREFIX is the value returned by the getPrefix() method and STRING-TO-BE-REPLACED is a value provided to the getReplacement(String) method. The result of the getReplacement(String) method call replaces the whole Variable String.

Hazelcast contains the example implementation EncryptionReplacer in the codebase — it allows for masking sensitive strings in the configuration file. Another example implementation called ExecReplacer is placed in the Hazelcast code samples — it allows for executing external commands and using its output as the replacement.

Custom Implementation

Now, let's show how simple is to implement and use your own custom replacer. The replacer will get a class name and create a new object from it. Then, it will call the toString() method on it to get the replacement value. Let's use a "TOSTRING" prefix for our new replacer.

package com.hazelcast.tutorial;

import com.hazelcast.config.replacer.spi.ConfigReplacer;

public class ToStringReplacer implements ConfigReplacer {

    public void init(java.util.Properties properties) {
        // we don't need init properties

    public String getPrefix() {
        return "TOSTRING";

    public String getReplacement(String value) {
        try {
            return Class.forName(value).newInstance().toString();
        } catch (Exception e) {
        // Return null when we are not able to find proper replacement.
        return null;

Once the new implementation is enabled within the configuration file's element, we can use it to provide values for variables:

<hazelcast xmlns="http://www.hazelcast.com/schema/config"

        <replacer class-name="com.hazelcast.config.replacer.ToStringReplacer"/>


    <instance-name>Instance $TOSTRING{java.util.Date}</instance-name>

When you start such a server instance and the GroupNameProvider.toString() call returns TestGroup constant, then you will see it as the group name in the server console output:

INFO: [LOCAL] [TestGroup] [3.10] Prefer IPv4 stack is true.
Apr 30, 2018 11:00:05 AM com.hazelcast.instance.AddressPicker
INFO: [LOCAL] [TestGroup] [3.10] Picked []:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
Apr 30, 2018 11:00:05 AM com.hazelcast.system

Find the full source code in this GitHub repository.


With Hazelcast, you can use Variables within XML configuration files. Moreover, you can provide own replacer implementations that will evaluate the Variables and provide replacements. We have implemented and used a custom Variable Replacer for a Hazelcast XML configuration file.

Is there any replacer you would like to have implemented? Stop by here and give us a shout!


imdg ,big data ,custom variables ,variable replacer ,tutorial ,hazelcast

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}