add helper classes to share same Elasticsearch low level client
parent
794446534c
commit
66df115579
@ -1,64 +1,26 @@
|
|||||||
package org.xbib.elx.node;
|
package org.xbib.elx.node;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.elasticsearch.Version;
|
|
||||||
import org.elasticsearch.client.ElasticsearchClient;
|
import org.elasticsearch.client.ElasticsearchClient;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.env.Environment;
|
|
||||||
import org.elasticsearch.node.Node;
|
|
||||||
import org.elasticsearch.plugins.Plugin;
|
|
||||||
import org.xbib.elx.common.AbstractAdminClient;
|
import org.xbib.elx.common.AbstractAdminClient;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class NodeAdminClient extends AbstractAdminClient {
|
public class NodeAdminClient extends AbstractAdminClient {
|
||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger(NodeAdminClient.class.getName());
|
private final NodeClientHelper helper;
|
||||||
|
|
||||||
private Node node;
|
public NodeAdminClient() {
|
||||||
|
this.helper = new NodeClientHelper();
|
||||||
@Override
|
|
||||||
protected ElasticsearchClient createClient(Settings settings) throws IOException {
|
|
||||||
if (settings != null) {
|
|
||||||
String version = System.getProperty("os.name")
|
|
||||||
+ " " + System.getProperty("java.vm.name")
|
|
||||||
+ " " + System.getProperty("java.vm.vendor")
|
|
||||||
+ " " + System.getProperty("java.runtime.version")
|
|
||||||
+ " " + System.getProperty("java.vm.version");
|
|
||||||
Settings effectiveSettings = Settings.builder().put(settings)
|
|
||||||
.put("node.client", true)
|
|
||||||
.put("node.master", false)
|
|
||||||
.put("node.data", false)
|
|
||||||
.build();
|
|
||||||
logger.info("creating node client on {} with effective settings {}",
|
|
||||||
version, effectiveSettings.getAsMap());
|
|
||||||
Collection<Class<? extends Plugin>> plugins = Collections.emptyList();
|
|
||||||
this.node = new BulkNode(new Environment(effectiveSettings), plugins);
|
|
||||||
try {
|
|
||||||
node.start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(e);
|
|
||||||
}
|
|
||||||
return node.client();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeClient() {
|
public ElasticsearchClient createClient(Settings settings) throws IOException {
|
||||||
if (node != null) {
|
return helper.createClient(settings, null);
|
||||||
logger.debug("closing node...");
|
|
||||||
node.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static class BulkNode extends Node {
|
|
||||||
|
|
||||||
BulkNode(Environment env, Collection<Class<? extends Plugin>> classpathPlugins) {
|
@Override
|
||||||
super(env, Version.CURRENT, classpathPlugins);
|
public void closeClient() {
|
||||||
}
|
helper.closeClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,68 +1,25 @@
|
|||||||
package org.xbib.elx.node;
|
package org.xbib.elx.node;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.elasticsearch.Version;
|
|
||||||
import org.elasticsearch.client.ElasticsearchClient;
|
import org.elasticsearch.client.ElasticsearchClient;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
||||||
import org.elasticsearch.env.Environment;
|
|
||||||
import org.elasticsearch.node.Node;
|
|
||||||
import org.elasticsearch.plugins.Plugin;
|
|
||||||
import org.xbib.elx.common.AbstractBulkClient;
|
import org.xbib.elx.common.AbstractBulkClient;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class NodeBulkClient extends AbstractBulkClient {
|
public class NodeBulkClient extends AbstractBulkClient {
|
||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger(NodeBulkClient.class.getName());
|
private final NodeClientHelper helper;
|
||||||
|
|
||||||
private Node node;
|
public NodeBulkClient() {
|
||||||
|
this.helper = new NodeClientHelper();
|
||||||
@Override
|
|
||||||
protected ElasticsearchClient createClient(Settings settings) throws IOException {
|
|
||||||
if (settings == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String version = System.getProperty("os.name")
|
|
||||||
+ " " + System.getProperty("java.vm.name")
|
|
||||||
+ " " + System.getProperty("java.vm.vendor")
|
|
||||||
+ " " + System.getProperty("java.runtime.version")
|
|
||||||
+ " " + System.getProperty("java.vm.version");
|
|
||||||
Settings effectiveSettings = Settings.builder().put(settings)
|
|
||||||
.put("node.client", true)
|
|
||||||
.put("node.master", false)
|
|
||||||
.put("node.data", false)
|
|
||||||
.build();
|
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
||||||
effectiveSettings.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true")));
|
|
||||||
logger.info("creating node client on {} with effective settings {}",
|
|
||||||
version, builder.string());
|
|
||||||
Collection<Class<? extends Plugin>> plugins = Collections.emptyList();
|
|
||||||
this.node = new BulkNode(new Environment(effectiveSettings), plugins);
|
|
||||||
try {
|
|
||||||
node.start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(e);
|
|
||||||
}
|
|
||||||
return node.client();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeClient() throws IOException {
|
public ElasticsearchClient createClient(Settings settings) throws IOException {
|
||||||
if (node != null) {
|
return helper.createClient(settings, null);
|
||||||
logger.debug("closing node client");
|
|
||||||
node.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static class BulkNode extends Node {
|
|
||||||
|
|
||||||
BulkNode(Environment env, Collection<Class<? extends Plugin>> classpathPlugins) {
|
@Override
|
||||||
super(env, Version.CURRENT, classpathPlugins);
|
public void closeClient() {
|
||||||
}
|
helper.closeClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
package org.xbib.elx.node;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.elasticsearch.Version;
|
||||||
|
import org.elasticsearch.client.ElasticsearchClient;
|
||||||
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.env.Environment;
|
||||||
|
import org.elasticsearch.node.Node;
|
||||||
|
import org.elasticsearch.plugins.Plugin;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class NodeClientHelper {
|
||||||
|
|
||||||
|
private static final Logger logger = LogManager.getLogger(NodeClientHelper.class.getName());
|
||||||
|
|
||||||
|
private static Node node;
|
||||||
|
|
||||||
|
private static ElasticsearchClient client;
|
||||||
|
|
||||||
|
private static Object configurationObject;
|
||||||
|
|
||||||
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
public ElasticsearchClient createClient(Settings settings, Object object) {
|
||||||
|
if (configurationObject == null) {
|
||||||
|
configurationObject = object;
|
||||||
|
}
|
||||||
|
if (configurationObject instanceof ElasticsearchClient) {
|
||||||
|
return (ElasticsearchClient) configurationObject;
|
||||||
|
}
|
||||||
|
if (client == null) {
|
||||||
|
synchronized (lock) {
|
||||||
|
String version = System.getProperty("os.name")
|
||||||
|
+ " " + System.getProperty("java.vm.name")
|
||||||
|
+ " " + System.getProperty("java.vm.vendor")
|
||||||
|
+ " " + System.getProperty("java.runtime.version")
|
||||||
|
+ " " + System.getProperty("java.vm.version");
|
||||||
|
Settings effectiveSettings = Settings.builder().put(settings)
|
||||||
|
.put("node.client", true)
|
||||||
|
.put("node.master", false)
|
||||||
|
.put("node.data", false)
|
||||||
|
.build();
|
||||||
|
logger.info("creating node client on {} with effective settings {}",
|
||||||
|
version, effectiveSettings.getAsMap());
|
||||||
|
Collection<Class<? extends Plugin>> plugins = Collections.emptyList();
|
||||||
|
node = new BulkNode(new Environment(effectiveSettings), plugins);
|
||||||
|
node.start();
|
||||||
|
client = node.client();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void closeClient() {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (client != null) {
|
||||||
|
logger.debug("closing node...");
|
||||||
|
node.close();
|
||||||
|
node = null;
|
||||||
|
client = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class BulkNode extends Node {
|
||||||
|
|
||||||
|
BulkNode(Environment env, Collection<Class<? extends Plugin>> classpathPlugins) {
|
||||||
|
super(env, Version.CURRENT, classpathPlugins);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,68 +1,25 @@
|
|||||||
package org.xbib.elx.node;
|
package org.xbib.elx.node;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.elasticsearch.Version;
|
|
||||||
import org.elasticsearch.client.ElasticsearchClient;
|
import org.elasticsearch.client.ElasticsearchClient;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
||||||
import org.elasticsearch.env.Environment;
|
|
||||||
import org.elasticsearch.node.Node;
|
|
||||||
import org.elasticsearch.plugins.Plugin;
|
|
||||||
import org.xbib.elx.common.AbstractSearchClient;
|
import org.xbib.elx.common.AbstractSearchClient;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class NodeSearchClient extends AbstractSearchClient {
|
public class NodeSearchClient extends AbstractSearchClient {
|
||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger(NodeSearchClient.class.getName());
|
private final NodeClientHelper helper;
|
||||||
|
|
||||||
private Node node;
|
public NodeSearchClient() {
|
||||||
|
this.helper = new NodeClientHelper();
|
||||||
@Override
|
|
||||||
protected ElasticsearchClient createClient(Settings settings) throws IOException {
|
|
||||||
if (settings == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String version = System.getProperty("os.name")
|
|
||||||
+ " " + System.getProperty("java.vm.name")
|
|
||||||
+ " " + System.getProperty("java.vm.vendor")
|
|
||||||
+ " " + System.getProperty("java.runtime.version")
|
|
||||||
+ " " + System.getProperty("java.vm.version");
|
|
||||||
Settings effectiveSettings = Settings.builder().put(settings)
|
|
||||||
.put("node.client", true)
|
|
||||||
.put("node.master", false)
|
|
||||||
.put("node.data", false)
|
|
||||||
.build();
|
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
||||||
effectiveSettings.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true")));
|
|
||||||
logger.info("creating node client on {} with effective settings {}",
|
|
||||||
version, builder.string());
|
|
||||||
Collection<Class<? extends Plugin>> plugins = Collections.emptyList();
|
|
||||||
this.node = new BulkNode(new Environment(effectiveSettings), plugins);
|
|
||||||
try {
|
|
||||||
node.start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(e);
|
|
||||||
}
|
|
||||||
return node.client();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void closeClient() throws IOException {
|
public ElasticsearchClient createClient(Settings settings) throws IOException {
|
||||||
if (node != null) {
|
return helper.createClient(settings, null);
|
||||||
logger.debug("closing node client");
|
|
||||||
node.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static class BulkNode extends Node {
|
|
||||||
|
|
||||||
BulkNode(Environment env, Collection<Class<? extends Plugin>> classpathPlugins) {
|
@Override
|
||||||
super(env, Version.CURRENT, classpathPlugins);
|
public void closeClient() {
|
||||||
}
|
helper.closeClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue