hazelcasthazelcast-imaphazelcast-jet

Problem trying to configure an interface and connect to the cluster


Hello people and good day, i'm trying to build an interface so, the interface it will be an external client that i need to get access from my linux console and execute the commands, this is the interface

public class HazelcastCacheImpl implements HazelcastCache {

    private HazelcastInstance clientInstance;

    public Boolean initCache() {

        HazelcastInstance status = Hazelcast.newHazelcastInstance();

        if (status.getLifecycleService().isRunning()) {
            return true;
        } else {
            System.out.println("There was a problem trying to run the cluster node");
            return false;
        }

    }

    public Boolean getEntry(String CacheName, String claveReq) {
        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        // Predicate<String,ResponseSerializable> query = Predicates.equal("claveReq", claveReq);

        if (!map.isEmpty()) {
            map.values(query);
        } else {
            return false;
        }
        return false;

    }

    public Boolean putEntry(String CacheName, Object key, Object value) {
        KeySerializable keyAux = null;
        ResponseSerializable responseAux = null;

        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        if (key.getClass().equals(KeySerializable.class) && value.getClass().equals(ResponseSerializable.class)) {
            keyAux = (KeySerializable) key;
            responseAux = (ResponseSerializable) value;

            String keyOR = keyAux.getClaveReq();

            map.put(keyOR, responseAux);

            return true;

        } else {
            System.out.println("the map doesn't exist or wrong data format");
            return false;
        }

    }

    public int removeEntry(String CacheName, String claveReq, int id_interno_pe, String cod_nrbe_en, int num_sec_ac) {
        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        Predicate<String, ResponseSerializable> query = Predicates.equal("claveReq", claveReq);

        if (!map.isEmpty()) {
            map.values(query);
            map.remove(claveReq);

            return 0;
        } else {
            return 0;

        }

    }

    public Boolean removeCache(String CacheName) {

        IMap<String, ResponseSerializable> map = clientInstance.getMap(CacheName);

        if (!map.isEmpty()) {
            map.destroy();
            System.out.println("Cache removed");
            return true;
        } else {
            System.out.println("cannot be deleted because the map doesn't exist, try with the right map name");
            return false;
        }
    }
}

so... what i need the method "initCache" to do is... connect to an external cluster so it will be a HazelcastClient then i need to check somethings that i dont know how to do it, if the client is alive/running, if the client exist well delete the corrupt client and run a new one, if there is no client then run

and the main problem it's about connection lost or connection proble, i dont know how to try a new connection if the connection is lost

¿Any hint friends?

Best, Luis.


Solution

  • I suppose you have a Hazelcast cluster running somewhere and you just want to access it from your "interface". You don't want to manage the cluster, but you are only concerned about the liveness of the client in the interface/cache.

    Use the Hazelcast client configuration options to take care of operation retries and/or connection failures.

    See the Java client documentation (ver 5.1) namely the following sections:

    Then you can use something like the following code to initialize your clientInstance:

        public Boolean initCache() {
            ClientConfig clientConfig = new ClientConfig();
            clientConfig.getNetworkConfig()
                // TODO add address(es) of your cluster member(s)
                .addAddress("10.0.0.1:5701")
                // TODO decide if the client should talk to all cluster members (smart) or just one (unisocket)
                .setSmartRouting(false)
                // Retry operation when it's possible
                .setRedoOperation(true);
    
            ClientConnectionStrategyConfig connectionStrategyConfig = clientConfig.getConnectionStrategyConfig();
            connectionStrategyConfig
                .setAsyncStart(true)
                .setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ASYNC);
            ConnectionRetryConfig connectionRetryConfig = connectionStrategyConfig.getConnectionRetryConfig();
            connectionRetryConfig
                .setInitialBackoffMillis(1000)
                .setMaxBackoffMillis(60000)
                .setMultiplier(2)
                .setClusterConnectTimeoutMillis(50000)
                .setJitter(0.2);
    
            clientInstance = HazelcastClient.newHazelcastClient(clientConfig);
        }