Package org.apache.vinci.transport
Class BaseClient
java.lang.Object
org.apache.vinci.transport.BaseClient
- Direct Known Subclasses:
VinciClient
Class for conjuring a Vinci service by host/port (that is, without interaction with the naming
service). Usually you want to use VinciClient, which extends this class to invoke a service by
(qualified) name.
Provides generic "send/recieve/sendAndReceive" for communicating arbitrary (transportable)
document models, and also specific "rpc" methods for more convenient support of the VinciFrame
document model.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
-
Constructor Summary
ConstructorDescriptionCreate a base client without establishing a connection.BaseClient
(String h, int p) Open up the service at the specified host and port, using a VinciFrame factory.BaseClient
(String h, int p, int connect_timeout) Open up the service at the specified host and port, using a VinciFrame factory.BaseClient
(String h, int p, TransportableFactory f) Open up the service at the specified host and port.BaseClient
(String h, int p, TransportableFactory f, int timeout) Open up the service at the specified host and port, using the specified connect timeout.Create a base client without establishing a connection.BaseClient
(TransportableFactory f, int timeout) Create a base client without establishing a connection. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the connection.Fetch the header of the last Transportable received.getHost()
Get the hostname/ip address to which this client is connected.int
getPort()
protected Socket
Get the socket used for the connection.int
Get the default timeout value for the socket (0 indicates never timeout, which is the default, but generally NOT a good setting).boolean
isOpen()
protected boolean
Gets whether keepAlive should be turned on for client sockets.final void
open()
(Re)connects the client to a previously specified host and port.protected final void
Connects the client to the specified host and port.void
Make this client use an already established socket connection.receive()
The other 1/2 of the split RPC.protected void
rpc
(Transportable query) Same as sendAndReceive(Transportable) except for return type.rpc
(Transportable query, int timeout) Same as sendAndReceive(Transportable, timeout) except for return type.static VinciFrame
rpc
(Transportable in, String host_name, int p) Convenience method for "one-shot" or "single-query" connections.static VinciFrame
rpc
(Transportable in, String host_name, int p, int socket_timeout) Convenience method for "one-shot" or "single-query" connections.static VinciFrame
rpc
(Transportable in, String host_name, int p, int socket_timeout, int connect_timeout) Convenience method for "one-shot" or "single-query" connections.void
send
(Transportable in) Support for 1/2 transaction RPC.Takes a transportable object, sends it across the connection, then retrieves the response and returns it.sendAndReceive
(Transportable in, int timeout) Takes a transportable object, sends it across the connection, then retrieves the response and returns it.static Transportable
sendAndReceive
(Transportable in, String host_name, int p, TransportableFactory f) Convenience method for "one-shot" or "single-query" connections.static Transportable
sendAndReceive
(Transportable in, String host_name, int p, TransportableFactory f, int socket_timeout) Convenience method for "one-shot" or "single-query" connections with socket timeout support.static Transportable
sendAndReceive
(Transportable in, String host_name, int p, TransportableFactory f, int socket_timeout, int connect_timeout) Convenience method for "one-shot" or "single-query" connections with socket timeout support & connect timeout support.Same as sendAndReceive(Transportable) except the provided factory is used to create the return document in place of the default factory.sendAndReceive
(Transportable in, TransportableFactory f, int timeout) Same as sendAndReceive(Transportable, timeout) except the provided factory is used to create the return document in place of the default factory.protected Transportable
protected Transportable
sendAndReceiveWork
(Transportable in, TransportableFactory f, int timeout) void
setConnectTimeout
(int timeout) Set the timeout value used for connect timeouts.void
setRetry
(boolean to) Set connection restablishment on IOException to on/off, default is ON.void
setSocketTimeout
(int millis) Set the timeout value used by the underlying socket.void
Set the transportable factory used by this client.
-
Field Details
-
DEFAULT_SOCKET_TIMEOUT
public static final int DEFAULT_SOCKET_TIMEOUT- See Also:
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT- See Also:
-
-
Constructor Details
-
BaseClient
Open up the service at the specified host and port, using a VinciFrame factory.- Parameters:
h
- The hostname/ip address of the machine running the service.p
- The port on which the service runs.- Throws:
IOException
- if the underlying socket fails to connect
-
BaseClient
Open up the service at the specified host and port, using a VinciFrame factory.- Parameters:
h
- The hostname/ip address of the machine running the service.p
- The port on which the service runs.connect_timeout
- The number of milliseconds that will elapse before a connect attempt fails.- Throws:
IOException
- if the underlying socket fails to connect
-
BaseClient
Open up the service at the specified host and port.- Parameters:
h
- The hostname/ip address of the machine running the service.p
- The port on which the service runs.f
- A factory for creating documents of the desired type.- Throws:
IOException
- if the underlying socket fails to connect.
-
BaseClient
Open up the service at the specified host and port, using the specified connect timeout.- Parameters:
h
- The hostname/ip address of the machine running the service.p
- The port on which the service runs.f
- A factory for creating documents of the desired type.timeout
- The number of milliseconds that will elapse before a connect attempt fails.- Throws:
IOException
- if the underlying socket fails to connect.
-
BaseClient
public BaseClient()Create a base client without establishing a connection. This is useful for client classes which extend this class and which to perform their own connection establishment. Uses a VinciFrame factory. -
BaseClient
Create a base client without establishing a connection. This is useful for client classes which extend this class and which to perform their own connection establishment.- Parameters:
f
- A factory for creating documents of the desired type.
-
BaseClient
Create a base client without establishing a connection. This is useful for client classes which extend this class and which to perform their own connection establishment.- Parameters:
f
- A factory for creating documents of the desired type.timeout
- The number of milliseconds that will elapse before a connect attempt fails.
-
-
Method Details
-
getHost
Get the hostname/ip address to which this client is connected.- Returns:
- -
-
getPort
public int getPort() -
getSocket
Get the socket used for the connection.- Returns:
- -
-
getSocketTimeout
public int getSocketTimeout()Get the default timeout value for the socket (0 indicates never timeout, which is the default, but generally NOT a good setting).- Returns:
- -
-
setConnectTimeout
public void setConnectTimeout(int timeout) Set the timeout value used for connect timeouts. Note that if you use one of the connection-inducing constructors, then this method has no effect unless a subsequent connection attempt is made.- Parameters:
timeout
- The number of milliseconds that will elapse before a connect attempt fails.
-
setTransportableFactory
Set the transportable factory used by this client.- Parameters:
f
- -
-
sendAndReceive
Takes a transportable object, sends it across the connection, then retrieves the response and returns it.- Parameters:
in
- The query frame.- Returns:
- A transportable representing the result, its specific type determined by the factory provided through BaseClient's constructor.
- Throws:
IOException
- thrown by the underlying socket IO (e.g. due to connection timeout).ServiceException
- thrown if the server threw ServiceException or returned an ErrorFrame.
-
sendAndReceive
public Transportable sendAndReceive(Transportable in, int timeout) throws IOException, ServiceException Takes a transportable object, sends it across the connection, then retrieves the response and returns it.- Parameters:
in
- The query frame.timeout
- The timeout value to use in place of this client's default timeout setting.- Returns:
- A transportable representing the result, its specific type determined by the factory provided through BaseClient's constructor.
- Throws:
IOException
- thrown by the underlying socket IO (e.g. due to connection timeout).ServiceException
- thrown if the server threw ServiceException or returned an ErrorFrame.
-
sendAndReceive
public Transportable sendAndReceive(Transportable in, TransportableFactory f) throws IOException, ServiceException Same as sendAndReceive(Transportable) except the provided factory is used to create the return document in place of the default factory.- Parameters:
in
- -f
- The factory to used to create the return document.- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
sendAndReceive
public Transportable sendAndReceive(Transportable in, TransportableFactory f, int timeout) throws IOException, ServiceException Same as sendAndReceive(Transportable, timeout) except the provided factory is used to create the return document in place of the default factory.- Parameters:
in
- -f
- The factory to used to create the return document.timeout
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
rpc
Same as sendAndReceive(Transportable) except for return type. Syntactic sugar method for the case where return result is known to be VinciFrame (eliminates the need for casting in the typical usage case).- Parameters:
query
- -- Returns:
- A VinciFrame representing the service result.
- Throws:
IOException
- -ServiceException
- -
-
rpc
Same as sendAndReceive(Transportable, timeout) except for return type. Syntactic sugar method for the case where return result is known to be VinciFrame (eliminates the need for casting in the typical usage case).- Parameters:
query
- -timeout
- -- Returns:
- A VinciFrame representing the service result.
- Throws:
IOException
- -ServiceException
- -
-
send
Support for 1/2 transaction RPC. This allows interaction with an asynchronous ("receive only") service, or for the sender to simply do something else before coming back and receiving the result (though at the risk of timeouts!).- Parameters:
in
- The Transportable to send.- Throws:
IOException
- Thrown by the underlying transport layer.
-
receive
The other 1/2 of the split RPC. This allows for interaction with an asynchronous "publish only" service, or simply picks up a result queried for earlier via send().- Returns:
- The Transportable requested.
- Throws:
IOException
- Thrown by the underlying transport layer, or the socket is closed.ServiceException
- Thrown if the remote server responded with an error frame.
-
close
public void close()Close the connection. Using the Client object after this will throw an exception. -
isOpen
public boolean isOpen() -
setRetry
public void setRetry(boolean to) Set connection restablishment on IOException to on/off, default is ON. This way, by default, BaseClient attempts to reopen a connection at most once if it receives an IOException which can happen, for example, from the connection timing out.- Parameters:
to
- -
-
getHeader
Fetch the header of the last Transportable received.- Returns:
- -
-
sendAndReceive
public static Transportable sendAndReceive(Transportable in, String host_name, int p, TransportableFactory f) throws IOException, ServiceException Convenience method for "one-shot" or "single-query" connections.- Parameters:
in
- -host_name
- -p
- -f
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
sendAndReceive
public static Transportable sendAndReceive(Transportable in, String host_name, int p, TransportableFactory f, int socket_timeout) throws IOException, ServiceException Convenience method for "one-shot" or "single-query" connections with socket timeout support.- Parameters:
in
- -host_name
- -p
- -f
- -socket_timeout
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
sendAndReceive
public static Transportable sendAndReceive(Transportable in, String host_name, int p, TransportableFactory f, int socket_timeout, int connect_timeout) throws IOException, ServiceException Convenience method for "one-shot" or "single-query" connections with socket timeout support & connect timeout support.- Parameters:
in
- -host_name
- -p
- -f
- -socket_timeout
- -connect_timeout
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
rpc
public static VinciFrame rpc(Transportable in, String host_name, int p) throws IOException, ServiceException Convenience method for "one-shot" or "single-query" connections. Same as sendAndReceive except uses VinciFrame factory so return type is known to be VinciFrame.- Parameters:
in
- -host_name
- -p
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
rpc
public static VinciFrame rpc(Transportable in, String host_name, int p, int socket_timeout) throws IOException, ServiceException Convenience method for "one-shot" or "single-query" connections. Same as sendAndReceive except uses VinciFrame factory so return type is known to be VinciFrame.- Parameters:
in
- -host_name
- -p
- -socket_timeout
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
rpc
public static VinciFrame rpc(Transportable in, String host_name, int p, int socket_timeout, int connect_timeout) throws IOException, ServiceException Convenience method for "one-shot" or "single-query" connections. Same as sendAndReceive except uses VinciFrame factory so return type is known to be VinciFrame.- Parameters:
in
- -host_name
- -p
- -socket_timeout
- -connect_timeout
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
sendAndReceiveWork
protected Transportable sendAndReceiveWork(Transportable in, TransportableFactory f) throws IOException, ServiceException - Parameters:
in
- -f
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
sendAndReceiveWork
protected Transportable sendAndReceiveWork(Transportable in, TransportableFactory f, int timeout) throws IOException, ServiceException - Parameters:
in
- -f
- -timeout
- -- Returns:
- -
- Throws:
IOException
- -ServiceException
- -
-
reopen
- Parameters:
e
- -- Throws:
IOException
- -
-
open
Connects the client to the specified host and port.- Parameters:
h
- The hostname/ip address of the server to connect to.p
- The port to connect to.- Throws:
IOException
- Thrown by underlying Socket open() call.
-
open
(Re)connects the client to a previously specified host and port. Should only be called if this client has been previously closed via a call to "close".- Throws:
IOException
- Thrown by underlying Socket open() call.
-
open
Make this client use an already established socket connection. If you use this open method, then setRetry is set to false. Resetting it to true will cause problems since the client does not know how to reopen the connection.- Parameters:
use_me
- The socket to use.- Throws:
IOException
- Thrown by underlying Socket open() call.
-
setSocketTimeout
Set the timeout value used by the underlying socket. Default is 2 minutes.- Parameters:
millis
- -- Throws:
IOException
- -
-
isSocketKeepAliveEnabled
protected boolean isSocketKeepAliveEnabled()Gets whether keepAlive should be turned on for client sockets. Always returns true for BaseClient. Can be overridden in subclasses.- Returns:
- whether socket keepAlive should be turned on
-