Package com.clickhouse.client
Class AbstractClient<T>
- java.lang.Object
-
- com.clickhouse.client.AbstractClient<T>
-
- All Implemented Interfaces:
ClickHouseClient
,AutoCloseable
public abstract class AbstractClient<T> extends Object implements ClickHouseClient
Base class for implementing a thread-safe ClickHouse client. It usesReadWriteLock
to manage access to underlying connection.
-
-
Field Summary
Fields Modifier and Type Field Description protected ReadWriteLock
lock
-
Constructor Summary
Constructors Constructor Description AbstractClient()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected boolean
checkConnection(T connection, ClickHouseNode requestServer, ClickHouseNode currentServer, ClickHouseRequest<?> request)
Checks if the underlying connection can be reused.void
close()
protected abstract void
closeConnection(T connection, boolean force)
Closes a connection.protected CompletableFuture<ClickHouseResponse>
failedResponse(Throwable ex)
ClickHouseConfig
getConfig()
Gets the immutable configuration associated with this client.protected T
getConnection(ClickHouseRequest<?> request)
Gets a connection according to the given request.protected ExecutorService
getExecutor()
Gets executor service for this client.protected ClickHouseNode
getServer()
Gets current server.void
init(ClickHouseConfig config)
Initializes the client using immutable configuration extracted from the builder usingClickHouseClientBuilder.getConfig()
.protected abstract T
newConnection(T connection, ClickHouseNode server, ClickHouseRequest<?> request)
Creates a new connection and optionally close existing connection.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.clickhouse.client.ClickHouseClient
accept, connect, execute, getOptionClass, ping
-
-
-
-
Field Detail
-
lock
protected final ReadWriteLock lock
-
-
Method Detail
-
failedResponse
protected CompletableFuture<ClickHouseResponse> failedResponse(Throwable ex)
-
getExecutor
protected final ExecutorService getExecutor()
Gets executor service for this client.- Returns:
- executor service
- Throws:
IllegalStateException
- when the client is either closed or not initialized
-
getServer
protected final ClickHouseNode getServer()
Gets current server.- Returns:
- current server
- Throws:
IllegalStateException
- when the client is either closed or not initialized
-
checkConnection
protected boolean checkConnection(T connection, ClickHouseNode requestServer, ClickHouseNode currentServer, ClickHouseRequest<?> request)
Checks if the underlying connection can be reused. In general, new connection will be created whenconnection
is null orrequestServer
is different fromcurrentServer
- the existing connection will be closed in the later case.- Parameters:
connection
- existing connection which may or may not be nullrequestServer
- non-null requested server, returned from previous call ofrequest.getServer()
currentServer
- current server, same asgetServer()
request
- non-null request- Returns:
- true if the connection should NOT be changed(e.g. requestServer is same as currentServer); false otherwise
-
newConnection
protected abstract T newConnection(T connection, ClickHouseNode server, ClickHouseRequest<?> request)
Creates a new connection and optionally close existing connection. This method will be called fromgetConnection(ClickHouseRequest)
as needed.- Parameters:
connection
- existing connection which may or may not be nullserver
- non-null requested server, returned from previous call ofrequest.getServer()
request
- non-null request- Returns:
- new connection
- Throws:
CompletionException
- when error occured
-
closeConnection
protected abstract void closeConnection(T connection, boolean force)
Closes a connection. This method will be called fromclose()
.- Parameters:
connection
- connection to closeforce
- whether force to close the connection or not
-
getConnection
protected final T getConnection(ClickHouseRequest<?> request)
Gets a connection according to the given request.- Parameters:
request
- non-null request- Returns:
- non-null connection
- Throws:
CompletionException
- when error occured
-
getConfig
public final ClickHouseConfig getConfig()
Description copied from interface:ClickHouseClient
Gets the immutable configuration associated with this client. In most cases it's the exact same one passed toClickHouseClient.init(ClickHouseConfig)
method for initialization.- Specified by:
getConfig
in interfaceClickHouseClient
- Returns:
- configuration associated with this client
-
init
public void init(ClickHouseConfig config)
Description copied from interface:ClickHouseClient
Initializes the client using immutable configuration extracted from the builder usingClickHouseClientBuilder.getConfig()
. In general, it'sClickHouseClientBuilder
's responsiblity to call this method to initialize the client at the end ofClickHouseClientBuilder.build()
. However, sometimes, you may want to call this method explicitly in order to (re)initialize the client based on certain needs. If that's the case, please consider the environment when calling this method to avoid concurrent modification, and keep in mind that 1) ClickHouseConfig is immutable but ClickHouseClient is NOT; and 2) no guarantee that this method is thread-safe.- Specified by:
init
in interfaceClickHouseClient
- Parameters:
config
- immutable configuration extracted from the builder
-
close
public final void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceClickHouseClient
-
-