Dynamic Configuration API

You can use the dynamic configuration API to deploy resources from your client or HazelcastInstance member. This means that you can configure namespaces and their resources, and replace the resources at runtime for use when a user customization is next instantiated or executed.

For example, you can use the dynamic configuration API to add or replace a specific namespace on a running cluster’s configuration.

Before you can use User Code Namespaces, you must enable it as described in the Enable User Code Namespaces topic.
User Code Namespaces can replace resources dynamically, but this does not affect the lifecycle of existing instances using older class versions. This means that the creation of new objects will use new resources (latest bytecode), but existing objects won’t be removed and recreated automatically. In this case, manual intervention is needed, such as destroying and recreating the relevant objects, or restarting Hazelcast members. This is particularly applicable to long-lived objects tied to the lifecycle of Hazelcast data structures, such as a MapLoader: deploying a new version won’t replace an existing MapLoader, and to refresh such an object, a cluster restart is required.

You must create a Client.java file containing the required code and save it to your class configuration folder. The file will be similar to the following:

package com.hazelcast.namespace.dynamic;
import com.hazelcast.config.UserCodeNamespaceConfig;
import com.hazelcast.config.UserCodeNamespacesConfig;
import com.hazelcast.core.HazelcastInstance;
import java.net.MalformedURLException;
import java.nio.file.Paths;
public class Client {
    public static void main(String[] args) throws MalformedURLException {
        // Assumes a HazelcastInstance is already running with a Config that has UCN enabled
        HazelcastInstance instance = getHazelcastInstance();
        UserCodeNamespacesConfig parentConfig = instance.getConfig().getNamespacesConfig(); (1)
        UserCodeNamespaceConfig namespaceConfig = new UserCodeNamespaceConfig("my_namespace"); (2)
            namespaceConfig.addClass(Paths.get("/etc/path/to/MyRequiredClass.class").toUri().toURL(), "MyRequiredClass-class");  (3)
        UserCodeNamespaceConfig ucn = new UserCodeNamespaceConfig("my_namespace")
            namespaceConfig.addClass(Paths.get("url/to/the/class/file").toUri().toURL(), "classId"); (4)
        parentConfig.addNamespaceConfig(namespaceConfig); (5)
        // After some time, you can replace `my_namespace` with new resources...
        UserCodeNamespaceConfig newNamespaceConfig = new UserCodeNamespaceConfig("my_namespace"); (6)
        newNamespaceConfig.addJar(Paths.get("/etc/path/to/my-resource.jar").toUri().toURL(), "my-resource-jar"); (7)
        parentConfig.addNamespaceConfig(namespaceConfig); (8)
    }
}
1 Fetches the dynamic User Code Namespaces configuration instance from the member
2 Creates a new UserCodeNamespaceConfig with the name my_namespace
3 Adds a Class resource to the my_namespace config, where the Class resolved from the client’s local context
4 Adds a Class resource using the URL to the .class and the supplied identifier. In this case, a null identifier is valid; if null is supplied, the class name is used as the ID
5 Adds the my_namespace configuration with its resources to the parent, which is dynamically applied on the member
6 Creates a new namespace config with the same name my_namespace
7 Adds different resources which will replace all previously defined resources
8 Adds the new my_namespace config to the parent, which will replace the previously defined resources