Java Binding: add option to not close network thread on shutdown hook

This commit is contained in:
Daniil GItelson 2020-02-02 21:10:23 +03:00
parent d035d4f30c
commit a38827b4e6
1 changed files with 21 additions and 8 deletions

View File

@ -107,10 +107,10 @@ public class FDB {
this(apiVersion, true);
}
private FDB(int apiVersion, boolean controlRuntime) {
private FDB(int apiVersion, boolean stopOnShutdown) {
this.apiVersion = apiVersion;
options = new NetworkOptions(this::Network_setOption);
if (controlRuntime) {
if (stopOnShutdown) {
Runtime.getRuntime().addShutdownHook(new Thread(this::stopNetwork));
}
}
@ -167,23 +167,36 @@ public class FDB {
* object.<br><br>
*
* Warning: When using the multi-version client API, setting an API version that
* is not supported by a particular client library will prevent that client from
* is not supported by a particular client library will prevent that client from
* being used to connect to the cluster. In particular, you should not advance
* the API version of your application after upgrading your client until the
* the API version of your application after upgrading your client until the
* cluster has also been upgraded.
*
* Note: automatically stops network thread upon runtime shutdown, if this
* behavior is undesirable use {@link #selectAPIVersion(int, boolean)}
*
* @param version the API version required
*
* @return the FoundationDB API object
* @see #selectAPIVersion(int, boolean)
*/
public static FDB selectAPIVersion(final int version) throws FDBException {
return selectAPIVersion(version, true);
}
/**
This function is called from C++ if the VM is controlled directly from FDB
* Same as {@link #selectAPIVersion(int)} but lets use choose whether you want to close
* FDB connection automatically on runtime shutdown or you want to do it manually.
*
* Dev Note: This function is called from C++ for tests, method signature must be consistent
* with JNI call.
*
* @param version the API version required
* @param stopOnShutdown whether you need to stop network thread upon Java runtime shutdown (via
* Runtime.getRuntime().addShutdownHook)
* @see #selectAPIVersion(int)
*/
private static synchronized FDB selectAPIVersion(final int version, boolean controlRuntime) throws FDBException {
public static synchronized FDB selectAPIVersion(final int version, boolean stopOnShutdown) throws FDBException {
if(singleton != null) {
if(version != singleton.getAPIVersion()) {
throw new IllegalArgumentException(
@ -197,7 +210,7 @@ public class FDB {
throw new IllegalArgumentException("API version not supported (maximum 620)");
Select_API_version(version);
FDB fdb = new FDB(version, controlRuntime);
FDB fdb = new FDB(version, stopOnShutdown);
return singleton = fdb;
}
@ -497,4 +510,4 @@ public class FDB {
private native boolean Error_predicate(int predicate, int code);
private native long Database_create(String clusterFilePath) throws FDBException;
}
}