Add SSL client and move logger out from abs. class

This commit is contained in:
Mark Vainomaa 2016-09-01 15:32:57 +03:00
parent fa29bb1970
commit 8c42f0dcb9
3 changed files with 46 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package eu.mikroskeem.utils.etcdconnector;
import mousio.client.retry.RetryOnce; import mousio.client.retry.RetryOnce;
import mousio.etcd4j.EtcdClient; import mousio.etcd4j.EtcdClient;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -16,6 +17,9 @@ public class EtcdConnector extends EtcdConnectorBase {
public EtcdConnector(URI... etcdUrls) throws IOException { public EtcdConnector(URI... etcdUrls) throws IOException {
super(); super();
/* Set up logger */
logger = LoggerFactory.getLogger("EtcdConnector");
/* Initialize client */ /* Initialize client */
initClient(etcdUrls); initClient(etcdUrls);

View File

@ -17,13 +17,10 @@ import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
abstract class EtcdConnectorBase { abstract class EtcdConnectorBase {
final Logger logger; Logger logger;
EtcdClient etcdClient; EtcdClient etcdClient;
EtcdConnectorBase() throws IOException { EtcdConnectorBase() throws IOException {}
/* Set up logger */
logger = LoggerFactory.getLogger("EtcdConnector");
}
/** /**
@ -171,7 +168,7 @@ abstract class EtcdConnectorBase {
* @param ttl key ttl (seconds) * @param ttl key ttl (seconds)
* @return whether write succeeded or not * @return whether write succeeded or not
*/ */
public boolean putKey(@NotNull String path, @NotNull String value, @NotNull int ttl){ public boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl){
logger.debug("putKey: {}, {}, {}", path, value, ttl); logger.debug("putKey: {}, {}, {}", path, value, ttl);
try { try {
EtcdKeysResponse response = etcdClient.put(path, new Gson().toJson(value)).ttl(ttl).send().get(); EtcdKeysResponse response = etcdClient.put(path, new Gson().toJson(value)).ttl(ttl).send().get();

View File

@ -0,0 +1,39 @@
package eu.mikroskeem.utils.etcdconnector;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import mousio.client.retry.RetryOnce;
import mousio.etcd4j.EtcdClient;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.net.URI;
public class EtcdSSLConnector extends EtcdConnectorBase {
public EtcdSSLConnector(URI... etcdUrls) throws IOException {
super();
/* Set up logger */
logger = LoggerFactory.getLogger("EtcdConnector");
/* Initialize client */
initClient(etcdUrls);
/* Test client */
testEtcd();
}
@Override void initClient(URI... urls){
SslContext sslContext;
try {
//sslContext = SslContext.newClientContext();
sslContext = SslContextBuilder.forClient().build();
} catch (SSLException e){
logger.error("Encountered error while creating new SSL context");
throw new RuntimeException(e);
}
etcdClient = new EtcdClient(sslContext, urls);
etcdClient.setRetryHandler(new RetryOnce(5000));
}
}