All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tarantool.driver.core.TarantoolConnectionImpl Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.core;

import io.netty.channel.Channel;
import io.tarantool.driver.TarantoolVersion;
import io.tarantool.driver.TarantoolVersionHolder;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.mappers.MessagePackValueMapper;
import io.tarantool.driver.protocol.TarantoolRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

public class TarantoolConnectionImpl implements TarantoolConnection {

    private final TarantoolVersionHolder versionHolder;
    private final RequestFutureManager requestManager;
    private final Channel channel;
    private final AtomicBoolean connected = new AtomicBoolean(true);
    private final List failureListeners = new ArrayList<>();

    public TarantoolConnectionImpl(RequestFutureManager requestManager,
                                   TarantoolVersionHolder versionHolder,
                                   Channel channel) {
        this.requestManager = requestManager;
        this.versionHolder = versionHolder;
        this.channel = channel;
        channel.closeFuture().addListener(f -> {
           if (connected.compareAndSet(true, false)) {
               for (TarantoolConnectionFailureListener listener : failureListeners) {
                   listener.onConnectionFailure(f.cause());
               }
           }
        });
    }

    @Override
    public TarantoolVersion getVersion() throws TarantoolClientException {
        if (!isConnected()) {
            throw new TarantoolClientException("Not connected to Tarantool server");
        }
        return versionHolder.getVersion();
    }

    @Override
    public boolean isConnected() {
        return connected.get();
    }

    @Override
    public  CompletableFuture sendRequest(TarantoolRequest request, MessagePackValueMapper resultMapper) {
        if (!isConnected()) {
            throw new TarantoolClientException("Not connected to Tarantool server");
        }

        CompletableFuture requestFuture = requestManager.submitRequest(request, resultMapper);
        channel.writeAndFlush(request).addListener(f -> {
            if (!f.isSuccess()) {
                requestFuture.completeExceptionally(
                        new RuntimeException("Failed to send the request to Tarantool server", f.cause()));
            }
        });

        return requestFuture;
    }

    @Override
    public void addConnectionFailureListener(TarantoolConnectionFailureListener listener) {
        failureListeners.add(listener);
    }

    @Override
    public void close() {
        connected.set(false);
        requestManager.close();
        channel.pipeline().close();
        channel.closeFuture().syncUninterruptibly();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy