A newer version of Hazelcast Platform is available.

View latest

Non-associated Namespaces

You can configure a default namespace with resources that can be used by any data structure that does not have an associated User Code Namespaces defined, or, for example, a partition loss listener. A default namespace is also useful when you are using an API that is not namespace-aware.

You can use static or dynamic configuration to define your default namespace.

Static Configuration

You can define your default namespace either programmatically or in the hazelcast configuration file.

For further information on static configuration, see the Static Configuration section.

Programmatically

To configure a default namespace programmatically, add the following lines to your code:

UserCodeNamespaceConfig userCodeNamespaceConfig = new UserCodeNamespaceConfig();
        userCodeNamespaceConfig.setName("default");
        userCodeNamespaceConfig.addClass(loadClass("org.example.RequiredClass"));
        config.getNamespacesConfig().addNamespaceConfig(userCodeNamespaceConfig);

Configuration File

To configure a default namespace using static configuration in the hazelcast configuration file, complete the following steps:

  1. If not already open, open the hazelcast configuration file in your preferred editor and scroll to the User Code Namespaces section

  2. Define the name of the namespace as default, as follows:

    • XML

    • YAML

    <user-code-namespaces enabled="true">
        ...
        <namespace name="default">
        </namespace>
    </user-code-namespaces>
    user-code-namespaces:
       enabled: true
       ...
       name-spaces:
          default:
  3. Define the resources

    To define a resource, you must provide, the resource type, and the location (URL) in which the resource can be found.You can also provide an identifier; this is recommended if needed for debugging purposes.

    The resource type can be JAR (jar), JARS IN ZIP (jars-in-zip), or CLASS (class); the following extract provides examples for both of these resource types:

    • XML

    • YAML

    <user-code-namespaces enabled="true">
        ...
        <namespace name="default">
            <jar id="jarID">
                <url>file:///yoursrc/class/ucnDefaults/MyJarFile.jar</url>
            </jar>
            <jars-in-zip id="MyJARsInZIPFileID">
                <url>file:///yoursrc/class/ucnDefaults/MyJarZIP.zip</url>
            </jars-in-zip>
            <class id="classId">
              <url>file:///yoursrc/class/ucnDefaults/MyClass.class</url>
            </class>
        </namespace>
    </user-code-namespaces>
    user-code-namespaces:
       enabled: true
       ...
       name-spaces:
          default:
           - id: jarID
             url: "file:///yoursrc/class/ucnDefaults/MyJarFile.jar"
           - id: MyJARsInZIPFileID
             url: "file:///yoursrc/class/ucnDefaults/MyJarZIP.zip"
           - id: classId
             resource-type: class
             url: "file:///yoursrc/class/ucnDefaults/MyClass.class"
  4. Save the file

Dynamic Configuration

To configure a default namespace using dynamic configuration, add the following to your member.java file:

UserCodeNamespaceConfig userCodeNamespaceConfig = new UserCodeNamespaceConfig("default");
        UserCodeNamespaceConfig userCodeNamespaceConfig = new UserCodeNamespaceConfig("default");
        // Add your resources to the namespace
        config.getNamespacesConfig().addNamespaceConfig(userCodeNamespaceConfig);

For further information on dynamic configuration, see the Dynamic Configuration API topic.