Home > Net >  What are new API for old thing HazelcastInstance hzClient = HazelcastClient.newHazelcastClient();?
What are new API for old thing HazelcastInstance hzClient = HazelcastClient.newHazelcastClient();?

Time:03-11

I follow tutorial at enter image description here

What are new API for old thing HazelcastInstance hzClient = HazelcastClient.newHazelcastClient(); ?

CodePudding user response:

You are using the following dependency

    <dependency>
        <groupId>com.hazelcast</groupId>
        <artifactId>hazelcast</artifactId>
        <version>3.11</version>
    </dependency>

This does not contain the client. The tutorial changes that dependency to hazelcast-all at 3:45, which has the client included. You can also include the client separately using hazelcast-client artifactId.

However 3.11 is a really old version, not supported anymore, you should use the latest release - 5.1 currently. In this release, there is no hazelcast-all jar anymore and the client is included in the main jar. Note that there are some breaking changes between 3.x and 4.x/5.x, but it's mostly just package names.

CodePudding user response:

Base on answer from František Hartman, I add more detail

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.anil.hz</groupId>
    <artifactId>H</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
            <version>5.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

File Client.java

package com.anil.server;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;

public class Client {

    public static void main(String[] args) {
        HazelcastInstance hzClient = null;
        try {
            hzClient = HazelcastClient.newHazelcastClient();
            IMap<Long, String> testMap = hzClient.getMap("testMap");
            System.out.println(testMap.get(1L));
        } finally {
            hzClient.shutdown();
        }
    }

}

File Server.java

package com.anil.server;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;

public class Server {

    public static void main(String[] args) {
        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance();
        IMap<Long, String> map = hzInstance.getMap("testMap");
        map.put(1L, "one");
        map.put(2L, "two");
    }
}

File HZConfig.java

package com.anil.server;

import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;

public class HZConfig {

    public static Config getConfig(){
        Config config = new Config();
        NetworkConfig network = config.getNetworkConfig();
        network.setPort(5710).setPortCount(20);
        network.setPortAutoIncrement(true);
        JoinConfig join = network.getJoin();
        join.getMulticastConfig().setEnabled(false);
        join.getTcpIpConfig().addMember("localhost").setEnabled(true);
        config.getManagementCenterConfig().setConsoleEnabled(true);
        config.getManagementCenterConfig().setDataAccessEnabled(true);
        config.getManagementCenterConfig().setScriptingEnabled(true);
                       
        // config.getManagementCenterConfig().setUrl("http://localhost:8081/mancenter"); // ?
        return config;
    }

}
  • Related