我编写了以下代码来创建嵌入式Elasticsearch引擎:
public class EmbeddedElasticsearchServer {
private static final String DEFAULT_DATA_DIRECTORY = "/Users/kevin/elasticsearch-5.2.2/data";
private static final String DEFAULT_PATH_DIRECTORY = "/Users/kevin/elasticsearch-5.2.2/home/dir";
private final Node node;
private final String dataDirectory;
public EmbeddedElasticsearchServer() {
this(DEFAULT_DATA_DIRECTORY);
}
public EmbeddedElasticsearchServer(String dataDirectory) {
this.dataDirectory = dataDirectory;
MudrodEngine mudrodEngine = new MudrodEngine();
Settings.Builder elasticsearchSettings = Settings.builder().put("http.enabled", "false").put("path.data", dataDirectory).put("path.home", DEFAULT_PATH_DIRECTORY);
node = new Node(elasticsearchSettings.build());
}
public Client getClient() {
return node.client();
}
public void shutdown() {
try {
node.close();
} catch (IOException e) {
System.out.println(e);
}
deleteDataDirectory();
}
private void deleteDataDirectory() {
try {
FileUtils.deleteDirectory(new File(dataDirectory));
} catch (IOException e) {
throw new RuntimeException("Could not delete data directory of embedded elasticsearch server", e);
}
}
}
但是,当使用此功能进行单元测试时,它无法通过,错误如下:
java.lang.IllegalStateException:无法获取节点锁,尝试使用lock id [0] [[/ Users/kevin/elasticsearch-5.2.2/data/elasticsearch]];也许这些位置不可写或多个节点在没有增加[node.max_local_storage_nodes]的情况下启动(是[1])?
有任何帮助吗?谢谢大家。