Package org.apache.vinci.transport
Class PooledVinciClient
- java.lang.Object
-
- org.apache.vinci.transport.PooledVinciClient
-
public class PooledVinciClient extends java.lang.ObjectMaintains a pool of connections to a given service and allows thread-safe querying of that service. This provides a set of sendAndReceive methods with signatures equivalent to those in VinciClient, but unlike VinciClient, the methods can be invoked concurrently by multiple threads.
-
-
Field Summary
Fields Modifier and Type Field Description private VinciClient[]availableClientsprivate intavailableClientsStartIndex(package private) booleanclosed(package private) intconnectTimeoutprivate VinciContextcontextprivate TransportableFactoryfactoryprivate intmaxPoolSizeprivate java.lang.StringserviceName(package private) intsocketTimeout
-
Constructor Summary
Constructors Constructor Description PooledVinciClient(java.lang.String serviceName, int maxPoolSize)Create a PooledVinciClient that will establish at most maxPoolSize connections to the designated service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose(boolean wait)Close this pooled client.private VinciClientgetClientFromPool()Retrieve a client from the pool, blocking for at most "socketTimeout" seconds.java.lang.StringgetServiceName()Get the service name to which this client connects.private voidreleaseClient(VinciClient c)Release a client that has been obtained from getClientFromPool.TransportablesendAndReceive(Transportable in)Send a request to the service and receive the response.TransportablesendAndReceive(Transportable in, int socketTimeout)Send a request to the service and receive the response, using the provided socketTimeout in place of the client-provided one.TransportablesendAndReceive(Transportable in, TransportableFactory f)Send a request to the service and receive the response, using the provided transportable factory in place of the client-provided one.TransportablesendAndReceive(Transportable in, TransportableFactory f, int socketTimeout)Send a request to the service and receive the response, using the provided transportable factory and socketTimeout in place of the client-provided ones.voidsetConnectTimeout(int connectTimeoutMillis)Set a connect timeout that will be used in place of BaseClient.DEFAULT_CONNECT_TIMEOUTvoidsetContext(VinciContext context)Set a VinciContext that will be used by this PooledVinciClient instead of the default global context.voidsetSocketTimeout(int socketTimeoutMillis)Set a socket timeout that will be used in place of BaseClient.DEFAULT_SOCKET_TIMEOUTvoidsetTransportableFactory(TransportableFactory factory)Set a transportable factory that will be used in place of the VinciFrame factory.
-
-
-
Field Detail
-
context
private VinciContext context
-
factory
private TransportableFactory factory
-
connectTimeout
int connectTimeout
-
socketTimeout
int socketTimeout
-
serviceName
private java.lang.String serviceName
-
maxPoolSize
private int maxPoolSize
-
availableClients
private VinciClient[] availableClients
-
availableClientsStartIndex
private int availableClientsStartIndex
-
closed
boolean closed
-
-
Method Detail
-
setContext
public void setContext(VinciContext context)
Set a VinciContext that will be used by this PooledVinciClient instead of the default global context.- Parameters:
context- -
-
setConnectTimeout
public void setConnectTimeout(int connectTimeoutMillis)
Set a connect timeout that will be used in place of BaseClient.DEFAULT_CONNECT_TIMEOUT- Parameters:
connectTimeoutMillis- -
-
setSocketTimeout
public void setSocketTimeout(int socketTimeoutMillis)
Set a socket timeout that will be used in place of BaseClient.DEFAULT_SOCKET_TIMEOUT- Parameters:
socketTimeoutMillis- -
-
setTransportableFactory
public void setTransportableFactory(TransportableFactory factory)
Set a transportable factory that will be used in place of the VinciFrame factory.- Parameters:
factory- -
-
getServiceName
public java.lang.String getServiceName()
Get the service name to which this client connects.- Returns:
- -
-
sendAndReceive
public Transportable sendAndReceive(Transportable in) throws java.io.IOException, ServiceException
Send a request to the service and receive the response. This method is tread safe.- Parameters:
in- -- Returns:
- -
- Throws:
java.io.IOException- -ServiceException- -
-
sendAndReceive
public Transportable sendAndReceive(Transportable in, TransportableFactory f) throws java.io.IOException, ServiceException
Send a request to the service and receive the response, using the provided transportable factory in place of the client-provided one. This method is tread safe.- Parameters:
in- -f- -- Returns:
- -
- Throws:
java.io.IOException- -ServiceException- -
-
sendAndReceive
public Transportable sendAndReceive(Transportable in, TransportableFactory f, int socketTimeout) throws java.io.IOException, ServiceException
Send a request to the service and receive the response, using the provided transportable factory and socketTimeout in place of the client-provided ones. This method is tread safe.- Parameters:
in- -f- -socketTimeout- -- Returns:
- -
- Throws:
java.io.IOException- -ServiceException- -
-
sendAndReceive
public Transportable sendAndReceive(Transportable in, int socketTimeout) throws java.io.IOException, ServiceException
Send a request to the service and receive the response, using the provided socketTimeout in place of the client-provided one. This method is tread safe.- Parameters:
in- -socketTimeout- -- Returns:
- -
- Throws:
java.io.IOException- -ServiceException- -
-
close
public void close(boolean wait)
Close this pooled client. Blocked requests will return IOException, as will any requests following the invocation of this method. Once a pooled client is closed it cannot be reused.- Parameters:
wait- If true, this method will block until all in-progress requests have completed, otherwise this method will return immediately (though in progress requests will still be allowed to complete)
-
getClientFromPool
private VinciClient getClientFromPool() throws java.io.IOException
Retrieve a client from the pool, blocking for at most "socketTimeout" seconds. If the wait block time is exceeded, an IOException will be thrown. Connections obtained by this method *must* be released by calling releaseClient().- Throws:
java.io.IOException- if the wait() time before a connectoin becomes available exceeds the socketTimeout value, or if thrown by a failed service connection attempt.
-
releaseClient
private void releaseClient(VinciClient c)
Release a client that has been obtained from getClientFromPool.
-
-