com.axibase.tsd.client.TcpClientManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atsd-api-java Show documentation
Show all versions of atsd-api-java Show documentation
The ATSD Client for Java enables Java developers to easily read
and write statistics and metadata from Axibase Time-Series Database. Build reporting,
analytics, and alerting solutions with minimal effort.
package com.axibase.tsd.client;
import com.axibase.tsd.model.system.TcpClientConfiguration;
import com.axibase.tsd.network.PlainCommand;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;
public class TcpClientManager {
private static final int DEFAULT_BORROW_MAX_TIME_MS = 3000;
private static final int DEFAULT_MAX_TOTAL = 8;
private static final int DEFAULT_MAX_IDLE = 8;
private GenericObjectPoolConfig objectPoolConfig;
private AtomicReference> objectPoolAtomicReference = new AtomicReference<>();
private int borrowMaxWaitMillis = DEFAULT_BORROW_MAX_TIME_MS;
private TcpClientConfiguration clientConfiguration;
public TcpClientManager() {
objectPoolConfig = new GenericObjectPoolConfig();
objectPoolConfig.setMaxTotal(DEFAULT_MAX_TOTAL);
objectPoolConfig.setMaxIdle(DEFAULT_MAX_IDLE);
}
public TcpClientManager(TcpClientConfiguration clientConfiguration) {
this();
this.clientConfiguration = clientConfiguration;
}
public void setClientConfiguration(TcpClientConfiguration clientConfiguration) {
this.clientConfiguration = clientConfiguration;
}
public void setObjectPoolConfig(GenericObjectPoolConfig objectPoolConfig) {
this.objectPoolConfig = objectPoolConfig;
}
public void setBorrowMaxWaitMillis(int borrowMaxWaitMillis) {
this.borrowMaxWaitMillis = borrowMaxWaitMillis;
}
public void send(PlainCommand plainCommand) {
TcpClient tcpClient = borrowClient();
try {
tcpClient.send(plainCommand);
} finally {
returnClient(tcpClient);
}
}
public void send(Collection plainCommand) {
TcpClient tcpClient = borrowClient();
try {
tcpClient.send(plainCommand);
} finally {
returnClient(tcpClient);
}
}
private TcpClient borrowClient() {
GenericObjectPool objectPool = createObjectPool();
TcpClient tcpClient;
try {
tcpClient = objectPool.borrowObject(borrowMaxWaitMillis);
} catch (Exception e) {
throw new AtsdClientException("Could not borrow tcp client from pool", e);
}
return tcpClient;
}
private void returnClient(TcpClient tcpClient) {
objectPoolAtomicReference.get().returnObject(tcpClient);
}
private GenericObjectPool createObjectPool() {
GenericObjectPool tcpClientGenericObjectPool = objectPoolAtomicReference.get();
if (tcpClientGenericObjectPool == null) {
tcpClientGenericObjectPool = new GenericObjectPool<>(new TcpClientBasePooledObjectFactory(), objectPoolConfig);
objectPoolAtomicReference.compareAndSet(null, tcpClientGenericObjectPool);
}
return objectPoolAtomicReference.get();
}
public void close() {
GenericObjectPool pool = objectPoolAtomicReference.get();
if (pool != null) {
pool.close();
}
}
private class TcpClientBasePooledObjectFactory extends BasePooledObjectFactory {
@Override
public TcpClient create() throws Exception {
return new TcpClient(clientConfiguration);
}
@Override
public PooledObject wrap(TcpClient tcpClient) {
return new DefaultPooledObject<>(tcpClient);
}
@Override
public void destroyObject(PooledObject p) throws Exception {
p.getObject().close();
}
}
}