public class GrizzlyThriftClient<T extends org.apache.thrift.TServiceClient> extends Object implements ThriftClient<T>, ZooKeeperSupportThriftClient
ThriftClient based on Grizzly
Basically, this class use BaseObjectPool for pooling connections of the thrift server
and RoundRobinStore for selecting the thrift server.
When a thrift operation is called, 1. finding the correct server by round-robin 2. borrowing the connection from the connection pool 3. returning the connection to the pool
For the failback of the thrift server, HealthMonitorTask will be scheduled by healthMonitorIntervalInSecs.
If connecting and writing are failed, this thrift client retries failure operations by retryCount.
The retrial doesn't request failed server but another thrift server.
And this client provides failover flag which can turn off the failover/failback.
Example of use:
// creates a ThriftClientManager
final GrizzlyThriftClientManager manager = new GrizzlyThriftClientManager.Builder().build();
// creates a ThriftClientBuilder
final GrizzlyThriftClient.Builder<Calculator.Client> builder = manager.createThriftClientBuilder("Calculator", new Calculator.Client.Factory());
// sets initial servers
builder.servers(initServerSet);
// creates the specific thrift client
final ThriftClient<Calculator.Client> calculatorThriftClient = builder.build();
// if you need to add another server
calculatorThriftClient.addServer(anotherServerAddress);
// custom thrift operations
Integer result = calculatorThriftClient.execute(new ThriftClientCallback<Calculator.Client, Integer>() {
public Integer call(Calculator.Client client) throws TException {
return client.add(1, 2);
}
});
// ...
// shuts down
manager.shutdown();
| Modifier and Type | Class and Description |
|---|---|
static class |
GrizzlyThriftClient.Builder<T extends org.apache.thrift.TServiceClient> |
| Modifier and Type | Field and Description |
|---|---|
static String |
CLIENT_ATTRIBUTE_NAME |
static String |
CONNECTION_POOL_ATTRIBUTE_NAME |
static String |
INPUT_BUFFERS_QUEUE_ATTRIBUTE_NAME |
| Modifier and Type | Method and Description |
|---|---|
boolean |
addServer(SocketAddress serverAddress)
Add a specific server in this thrift client
|
void |
addZooKeeperListener(BarrierListener listener)
Add the custom
BarrierListener
The given listener will be called after thrift client's default listener will be completed. |
<U> U |
execute(ThriftClientCallback<T,U> callback) |
String |
getCurrentServerListFromZooKeeper()
Return the current thrift server list string from the ZooKeeper server
|
String |
getName()
Return the name of the thrift client.
|
String |
getZooKeeperServerListPath()
Return the path of the thrift server list which has been registered in the ZooKeeper server
|
boolean |
isInServerList(SocketAddress serverAddress)
Check if this thrift client contains the given server
|
boolean |
isZooKeeperSupported()
Check if this thrift client supports the ZooKeeper for synchronizing the thrift server list
|
void |
removeServer(SocketAddress serverAddress)
Remove the given server in this thrift client
|
void |
removeZooKeeperListener(BarrierListener listener)
Remove the custom
BarrierListener
The given listener will be called after thrift client's default listener will be completed. |
boolean |
setCurrentServerListOfZooKeeper(String thriftServerList)
Set the current thrift server list string with the given
thriftServerList |
void |
start()
Notifies providers to start themselves.
|
void |
stop()
Providers may be doing all sorts of exotic things and need to be able to clean up on
stop.
|
String |
toString() |
public static final String CONNECTION_POOL_ATTRIBUTE_NAME
public static final String CLIENT_ATTRIBUTE_NAME
public static final String INPUT_BUFFERS_QUEUE_ATTRIBUTE_NAME
public void start()
start in interface ThriftClientLifecyclepublic void stop()
stop in interface ThriftClientLifecyclepublic boolean addServer(SocketAddress serverAddress)
addServer in interface ThriftClient<T extends org.apache.thrift.TServiceClient>serverAddress - a specific server's SocketAddress to be addedserverAddress is added successfullypublic void removeServer(SocketAddress serverAddress)
removeServer in interface ThriftClient<T extends org.apache.thrift.TServiceClient>serverAddress - the specific server's SocketAddress to be removed in this thrift clientpublic boolean isInServerList(SocketAddress serverAddress)
isInServerList in interface ThriftClient<T extends org.apache.thrift.TServiceClient>serverAddress - the specific server's SocketAddress to be checkedserverAddresspublic boolean isZooKeeperSupported()
isZooKeeperSupported in interface ZooKeeperSupportThriftClientpublic String getZooKeeperServerListPath()
getZooKeeperServerListPath in interface ZooKeeperSupportThriftClientpublic String getCurrentServerListFromZooKeeper()
getCurrentServerListFromZooKeeper in interface ZooKeeperSupportThriftClientpublic boolean setCurrentServerListOfZooKeeper(String thriftServerList)
thriftServerList
thriftServerList could be comma separated host:port pairs, each corresponding to a thrift server.
e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
Be careful that this operation will propagate thriftServerList to thrift clients which has joinned the same thrift client name(scope)
because the thrift server list of ZooKeeper server will be changed.
setCurrentServerListOfZooKeeper in interface ZooKeeperSupportThriftClientthriftServerList - the thrift server list stringpublic void addZooKeeperListener(BarrierListener listener)
BarrierListener
The given listener will be called after thrift client's default listener will be completed.
BarrierListener.onInit(java.lang.String, java.lang.String, byte[]) will be called when this thrift client will be registered in the ZooKeeper.
BarrierListener.onCommit(java.lang.String, java.lang.String, byte[]) will be called when this thrift client's server list will be changed in the ZooKeeper.
BarrierListener.onDestroy(java.lang.String) will be called when this thrift client will be unregistered in the ZooKeeper.addZooKeeperListener in interface ZooKeeperSupportThriftClientlistener - the custom listenerpublic void removeZooKeeperListener(BarrierListener listener)
BarrierListener
The given listener will be called after thrift client's default listener will be completed.
BarrierListener.onInit(java.lang.String, java.lang.String, byte[]) will be called when this thrift client will be registered in the ZooKeeper.
BarrierListener.onCommit(java.lang.String, java.lang.String, byte[]) will be called when this thrift client's server list will be changed in the ZooKeeper.
BarrierListener.onDestroy(java.lang.String) will be called when this thrift client will be unregistered in the ZooKeeper.removeZooKeeperListener in interface ZooKeeperSupportThriftClientlistener - the custom listener which was given by ZooKeeperSupportThriftClient.addZooKeeperListener(org.glassfish.grizzly.thrift.client.zookeeper.BarrierListener)public String getName()
getName in interface ThriftClient<T extends org.apache.thrift.TServiceClient>public <U> U execute(ThriftClientCallback<T,U> callback) throws Exception
execute in interface ThriftClient<T extends org.apache.thrift.TServiceClient>ExceptionCopyright © 2017-2019 Oracle Corporation. All Rights Reserved.