Skip to content

Commit

Permalink
feat: support create client with Proprities object (apache#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverneverer authored Aug 24, 2021
1 parent 4222817 commit 034c114
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.xiaomi.infra.pegasus.security.Credential;
import java.time.Duration;
import java.util.Properties;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationConverter;

Expand Down Expand Up @@ -131,9 +132,17 @@ public static ClientOptions create() {
return builder().build();
}

public static ClientOptions create(Properties properties) throws PException {
Configuration config = ConfigurationConverter.getConfiguration(properties);
return create(config);
}

public static ClientOptions create(String configPath) throws PException {
Configuration config = ConfigurationConverter.getConfiguration(loadConfiguration(configPath));
return create(config);
}

private static ClientOptions create(Configuration config) throws PException {
String metaList = config.getString(PEGASUS_META_SERVERS_KEY);
if (metaList == null) {
throw new IllegalArgumentException("no property set: " + PEGASUS_META_SERVERS_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -67,6 +68,10 @@ private PegasusTable getTable(String tableName, InternalTableOptions internalTab
return table;
}

public PegasusClient(Properties properties) throws PException {
this(ClientOptions.create(properties));
}

// configPath could be:
// - zk path: zk://host1:port1,host2:port2,host3:port3/path/to/config
// - local file path: file:///path/to/config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.client;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -35,6 +36,17 @@ public static PegasusClientInterface createClient(String configPath) throws PExc
return new PegasusClient(configPath);
}

/**
* Create a client instance. After used, should call client.close() to release resource.
*
* @param properties properties
* @return PegasusClientInterface {@link PegasusClientInterface}
* @throws PException throws exception if any error occurs.
*/
public static PegasusClientInterface createClient(Properties properties) throws PException {
return new PegasusClient(properties);
}

/**
* Create a client instance instance with {@link ClientOptions}. After used, should call
* client.close() to release resource.
Expand Down Expand Up @@ -94,6 +106,18 @@ public static PegasusClientInterface getSingletonClient(String configPath) throw
}
}

/**
* Get the singleton client instance with properties. After used, should call
* PegasusClientFactory.closeSingletonClient() to release resource.
*
* @param properties properties
* @return PegasusClientInterface {@link PegasusClientInterface}
* @throws PException throws exception if any error occurs.
*/
public static PegasusClientInterface getSingletonClient(Properties properties) throws PException {
return getSingletonClient(ClientOptions.create(properties));
}

/**
* Get the singleton client instance instance with {@link ClientOptions}. After used, should call
* PegasusClientFactory.closeSingletonClient() to release resource.
Expand Down

0 comments on commit 034c114

Please sign in to comment.