This is a prerelease version.

View latest

Configure Client

Clients use the dynamic configuration API to create and update User Code Namespaces on the cluster.

Dynamically Creating User Code Namespaces

You must create a Client.java file containing the required code and save it to your configuration folder.

User Code Namespaces must be enabled using static configuration on the member side before clients are allowed to dynamically create User Code Namespaces. For further information on enabling User Code Namespaces, see Enable User Code Namespaces.

The created file will be similar to the following:

package com.hazelcast.namespaces.dyamicconfig;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.config.UserCodeNamespaceConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.EntryProcessor;

public class Client {

    public static void main(String[] args) {
        HazelcastInstance client = HazelcastClient.newHazelcastClient();

        // prepare our user code that we want to execute
        EntryProcessor entryProcessor = new IncrementingEntryProcessor();

        // prepare a new namespace config
        UserCodeNamespaceConfig namespaceConfig = new UserCodeNamespaceConfig("namespace_name"); (1)
        namespaceConfig.addClass(IncrementingEntryProcessor.class); (2)

        // prepare our data structure config
        MapConfig mapConfig = new MapConfig("map_name"); (3)
        mapConfig.setUserCodeNamespace("namespace_name"); (4)

        // apply our new configs to the cluster
        client.getConfig().getNamespacesConfig().addNamespaceConfig(namespaceConfig); (5)
        client.getConfig().addMapConfig(mapConfig); (6)

        // execute the entry processor on the IMap associated with our new configs
        client.getMap("map_name").executeOnKey("key", entryProcessor); (7)
    }
}
1 Creates a new UserCodeNamespaceConfig with a name of namespace_name
2 Adds the defined class from the classpath to the UserCodeNamespaceConfig
3 Creates a new MapConfig for the map named map_name
4 Associated the User Code Namespace namespace_name with this MapConfig
5 Applies our new UserCodeNamespaceConfig to the cluster - this must be done before applying any data structure configuration that references this Namespace
6 Applies our new MapConfig to the cluster
7 Executes IncrementingEntryProcessor from the classpath on the key in our map

Dynamically Updating Existing User Code Namespaces

You must create a Client.java file containing the required code and save it to your configuration folder.

Ensure that you have configured your data structure (an IMap in this example) on the member side with the same User Code Namespaces referenced in your client configuration.

The file will be similar to the following:

package com.hazelcast.namespaces.dyamicconfig;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.config.UserCodeNamespaceConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.EntryProcessor;

public class Client {

    public static void main(String[] args) {
        HazelcastInstance client = HazelcastClient.newHazelcastClient();

        EntryProcessor entryProcessor = new IncrementingEntryProcessor();
        UserCodeNamespaceConfig namespaceConfig = new UserCodeNamespaceConfig("namespace_name"); (1)
        namespaceConfig.addClass(IncrementingEntryProcessor.class); (2)

        // dynamically update the namespace config
        client.getConfig().getNamespacesConfig().addNamespaceConfig(namespaceConfig); (3)
        // execute the entry processor - this map should already be configured to use our namespace
        client.getMap("map_name").executeOnKey("key", entryProcessor); (4)
    }
}
1 Creates a UserCodeNamespaceConfig with a name of namespace_name
2 Adds the defined class from the classpath to the UserCodeNamespaceConfig
3 Gets the Hazelcast configuration from the client instance and adds the UserCodeNamespaceConfig to the Config, overwriting the existing config
4 Executes IncrementingEntryProcessor from the classpath on the key in an IMap that has been configured to use the same Namespace