|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.flat502.rox.processing.HttpRpcProcessor com.flat502.rox.client.HttpRpcClient
public abstract class HttpRpcClient
This is the client-side RPC interface.
This class supports both synchronous and asynchronous method calls. An instance is backed by at least two threads: a selecting thread and a worker thread. Both threads are daemon threads.
The selecting thread handles all low-level network I/O. As soon as this thread identifies a complete HTTP response the response is passed to the worker thread (or threads).
The number of worker threads may be adjusted dynamically
using the inherited
HttpRpcProcessor.addWorker()
and HttpRpcProcessor.removeWorker()
methods. Worker threads are responsible for callbacks
when the asynchronous API is used.
Field Summary |
---|
Fields inherited from class com.flat502.rox.processing.HttpRpcProcessor |
---|
ALL_CIPHER_SUITES, ANON_CIPHER_SUITES |
Constructor Summary | |
---|---|
protected |
HttpRpcClient(java.net.URL url,
ClientResourcePool pool)
Initializes a new instance using the given URL. |
protected |
HttpRpcClient(java.net.URL url,
ClientResourcePool pool,
SSLConfiguration sslCfg)
|
Method Summary | |
---|---|
protected void |
connect(java.nio.channels.SelectionKey key)
Called when a requested connect operation on a SocketChannel is eligible for completion. |
protected void |
deregisterChannel(java.nio.channels.SelectableChannel channel)
|
java.lang.Object |
execute(java.lang.String name,
java.lang.Object[] params)
Executes the named RPC method call synchronously, using the parameter list specified. |
void |
execute(java.lang.String name,
java.lang.Object[] params,
AsynchronousResponseHandler handler)
Executes the named RPC method call asynchronously, using the parameter list specified. |
java.lang.Object |
execute(java.lang.String name,
java.lang.Object[] params,
java.lang.Class retClass)
Executes the named RPC method call synchronously, using the parameter list specified. |
void |
execute(java.lang.String name,
java.lang.Object[] params,
java.lang.Class retClass,
AsynchronousResponseHandler handler)
Executes the named RPC method call asynchronously, using the parameter list specified. |
void |
execute(java.lang.String name,
java.lang.Object[] params,
java.lang.Class retClass,
ResponseHandler handler)
Deprecated. Use execute(String, Object[], Class, AsynchronousResponseHandler) instead. |
java.lang.Object |
execute(java.lang.String name,
java.lang.Object[] params,
MethodResponseUnmarshallerAid aid)
Executes the named RPC method call synchronously, using the parameter list specified. |
void |
execute(java.lang.String name,
java.lang.Object[] params,
MethodResponseUnmarshallerAid aid,
AsynchronousResponseHandler handler)
Executes the named RPC method call asynchronously, using the parameter list specified. |
void |
execute(java.lang.String name,
java.lang.Object[] params,
MethodResponseUnmarshallerAid aid,
ResponseHandler handler)
Deprecated. Use execute(String, Object[], MethodResponseUnmarshallerAid, AsynchronousResponseHandler) instead. |
void |
execute(java.lang.String name,
java.lang.Object[] params,
ResponseHandler handler)
Deprecated. Use execute(String, Object[], AsynchronousResponseHandler) instead. |
protected abstract MethodResponseUnmarshaller |
getMethodResponseUnmarshaller()
Retrieves an MethodResponseUnmarshaller instance
for unmarshalling incoming RpcResponse s. |
protected HttpMessageBuffer |
getReadBuffer(java.net.Socket socket)
Called when data is available on a socket. |
protected java.lang.String |
getUserAgent()
|
protected java.nio.ByteBuffer |
getWriteBuffer(java.net.Socket socket)
Called when a socket becomes available for writing. |
protected void |
handleMessageException(HttpMessageBuffer msg,
java.lang.Exception e)
Creates a new instance of HttpMessageException
and enqueues it on the queue shared by worker threads. |
protected void |
handleProcessingException(java.net.Socket socket,
java.lang.Exception e)
An error handler invoked when an error occurs within the main processing loop. |
protected void |
handleSelectionKeyOperation(java.nio.channels.SelectionKey key)
This implementation defers to the connect(SelectionKey) method if a connection
is ready for completion. |
protected void |
handleSSLHandshakeFinished(java.net.Socket socket,
javax.net.ssl.SSLEngine engine)
|
protected void |
handleTimeout(java.net.Socket socket,
java.lang.Exception cause)
|
protected void |
initialize()
This implementation defers to the parent implementation before marking this thread as a daemon thread and starting the thread. |
protected javax.net.ssl.SSLEngine |
initSocketSSLEngine(java.net.Socket socket)
|
protected boolean |
isWriteQueued(java.net.Socket socket)
Called to find out if data is queued to be written to a socket. |
protected HttpRequest |
newHttpRequest(RpcCall call)
|
protected abstract RpcCall |
newRpcCall(java.lang.String name,
java.lang.Object[] params)
|
protected RpcResponseContext |
newRpcResponseContext(java.net.Socket socket,
HttpResponseBuffer rsp)
|
protected ResourcePool |
newWorkerPool()
Factory method for new worker pools. |
java.lang.Object |
proxyObject(java.lang.Class targetClass)
Convenience method for proxying an object without a method prefix. |
java.lang.Object |
proxyObject(java.lang.String methodPrefix,
java.lang.Class targetClass)
Supports dynamic mapping of Java method calls onto RPC method calls. |
protected void |
putWriteBuffer(java.net.Socket socket,
java.nio.ByteBuffer data)
Called when data is available to be written to a socket. |
protected void |
registerChannel(java.nio.channels.SelectableChannel channel)
|
protected void |
removeReadBuffer(java.net.Socket socket)
Called when a complete HTTP message has been identified. |
protected void |
removeReadBuffers(java.net.Socket socket)
Called when a a socket is deregistered and any buffers associated with it should be released. |
protected void |
removeWriteBuffer(java.net.Socket socket)
Called when a all of the data in a pending write buffer has been written to a socket. |
protected void |
removeWriteBuffers(java.net.Socket socket)
Called when a a socket is deregistered and any buffers associated with it should be released. |
void |
setAcceptEncodedResponses(boolean accept)
|
void |
setConnectionPoolLimit(int limit)
Configure a limit on the number of active connections provided at any given time by the underlying connection pool. |
void |
setConnectionPoolTimeout(long timeout)
Configure a timeout value for the underlying connection pool. |
void |
setContentEncoding(Encoding encoding)
Set the content encoding to use for |
void |
setRequestTimeout(long timeout)
Configure a timeout value for RPC method calls. |
protected void |
stopImpl()
|
protected void |
validateHttpResponse(RpcCall call,
HttpResponseBuffer response)
Validates an HTTP response message, optionally converting it into a local exception. |
Methods inherited from class com.flat502.rox.processing.HttpRpcProcessor |
---|
addWorker, addWorkers, deregisterSocket, getQueue, getSocketSelector, getSSLConfiguration, getSSLSession, getTimer, getWorkerCount, initSelector, isSharedWorkerPool, isStarted, newSSLSession, queueCancellation, queueRead, queueRegistration, queueWrite, queueWrite, read, registerProfiler, registerSocket, registerSSLSessionPolicy, removeWorker, setCipherSuitePattern, setSSLHandshakeTimeout, shouldUseHTTPS, start, stop, write |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected HttpRpcClient(java.net.URL url, ClientResourcePool pool) throws java.io.IOException
Currently only HTTP is supported as a transport.
url
- The URL to send method call requests to.
java.lang.IllegalArgumentException
- if the protocol specified by the given URL is
not supported.
java.io.IOException
- if an error occurs while initializing the
underlying Selector
.protected HttpRpcClient(java.net.URL url, ClientResourcePool pool, SSLConfiguration sslCfg) throws java.io.IOException
java.io.IOException
Method Detail |
---|
public java.lang.Object execute(java.lang.String name, java.lang.Object[] params) throws java.lang.Exception
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public java.lang.Object execute(java.lang.String name, java.lang.Object[] params, java.lang.Class retClass) throws java.lang.Exception
The type of the return value is dependent on the value
encoded within the RPC response. The details are
dependent on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).retClass
- The class to use when unmarshalling the response if
the return value is a struct.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public java.lang.Object execute(java.lang.String name, java.lang.Object[] params, MethodResponseUnmarshallerAid aid) throws java.lang.Exception
The type of the return value is dependent on the value
encoded within the RPC response. The details are
dependent on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).aid
- The MethodResponseUnmarshallerAid
to use when unmarshalling the response.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public void execute(java.lang.String name, java.lang.Object[] params, ResponseHandler handler) throws java.lang.Exception
execute(String, Object[], AsynchronousResponseHandler)
instead.
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).handler
- The handler to notify when the response is available.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public void execute(java.lang.String name, java.lang.Object[] params, AsynchronousResponseHandler handler) throws java.lang.Exception
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).handler
- The handler to notify when the response is available.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public void execute(java.lang.String name, java.lang.Object[] params, java.lang.Class retClass, ResponseHandler handler) throws java.lang.Exception
execute(String, Object[], Class, AsynchronousResponseHandler)
instead.
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).retClass
- The class to use when unmarshalling the response if
the return value is a struct.handler
- The handler to notify when the response is available.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public void execute(java.lang.String name, java.lang.Object[] params, java.lang.Class retClass, AsynchronousResponseHandler handler) throws java.lang.Exception
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).retClass
- The class to use when unmarshalling the response if
the return value is a struct.handler
- The handler to notify when the response is available.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public void execute(java.lang.String name, java.lang.Object[] params, MethodResponseUnmarshallerAid aid, ResponseHandler handler) throws java.lang.Exception
execute(String, Object[], MethodResponseUnmarshallerAid, AsynchronousResponseHandler)
instead.
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).aid
- The MethodResponseUnmarshallerAid
to use when unmarshalling the response.handler
- The handler to notify when the response is available.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public void execute(java.lang.String name, java.lang.Object[] params, MethodResponseUnmarshallerAid aid, AsynchronousResponseHandler handler) throws java.lang.Exception
The type of the return value is dependent on the value
encoded within the RPC response. Details are dependent
on the particular implementation of the
RpcMethod
interface
this instance is configured to use.
name
- The name of the method to execute.params
- The list of parameters to pass along with the method call.
This list may be null
or empty (these are
equivalent).aid
- The MethodResponseUnmarshallerAid
to use when unmarshalling the response.handler
- The handler to notify when the response is available.
RpcFaultException
- if an the remote server responds with an RPC fault.
java.lang.Exception
- if any other error occurs while attempting to execute
the method call.public java.lang.Object proxyObject(java.lang.Class targetClass) throws java.lang.Exception
This is equivalent to invoking
proxyObject(String, Class)
with a null
method prefix.
targetClass
- The class to generate a proxy for.
java.lang.Exception
- If an error occurs generating the dynamic proxy.Proxy
public java.lang.Object proxyObject(java.lang.String methodPrefix, java.lang.Class targetClass) throws java.lang.Exception
This method generates a dynamic Proxy
based on targetClass
(which may be an interface)
and associates it with this instance. The returned object
must implement at least one public interface (it may
implement more than one) and is subject to the restrictions
discussed under the description of the
Proxy
class. The returned object
may be cast to any of the public interfaces it implements
(but not to an instance of targetClass
).
Calls made to methods on the returned object are mapped to RPC calls to the remote server this instance is connected to.
How parameters are marshalled is dependent on the
particular implementation of the
RpcMethod
interface
this instance is configured to use.
methodPrefix
- The prefix to prepend when generating the RPC
method name. This may be null, in which case no prefix
is used.targetClass
- The class to generate a proxy for.
java.lang.Exception
- If an error occurs generating the dynamic proxy.Proxy
public void setConnectionPoolLimit(int limit)
If a non-zero limit is any thread requesting a new connection
that would cause this limit to be exceeded will be blocked
until an existing connection is returned or until a timeout
occurs (if a timeout has been set
).
Care should be taken when using the asynchronous execution API without a limit on the connection pool. It's very easy to consume all available local connections like this.
If a non-zero limit is placed on the connection pool then you
must be sure to set a timeout
on the pool. Failure to do so will result in threads blocking
indefinitely when the connection pool is exhausted.
limit
- The maximum number of active connections allowed at any given moment.
A value of 0 indicates no limit should be enforced (this is the default value).
java.lang.IllegalArgumentException
- If the timeout provided is negative.
java.lang.IllegalStateException
- If any of the execute
methods were invoked before
this method was invoked.public void setConnectionPoolTimeout(long timeout)
This timeout only applies if a limit has been set on the number
of active connections (using setConnectionPoolLimit(int)
.
If a limit is configured this timeout controls how long a thread
will be blocked waiting for a new connection before a
ConnectionPoolTimeoutException
is raised.
timeout
- The timeout (in milliseconds). A value of 0 indicates no timeout should be
enforced (this is the default value).
java.lang.IllegalArgumentException
- If the timeout provided is negative.
java.lang.IllegalStateException
- If any of the execute
methods were invoked before
this method was invoked.public void setRequestTimeout(long timeout)
The new timeout affects only calls executed subsequent to the
completion of this method call. Time spent waiting for a connection
from the connection pool is not included in this timeout
(see setConnectionPoolTimeout(long)
.
If a method call times out as a result of the timeout
configured here an instance of RpcCallTimeoutException
will be raised in the case of synchronous calls or, in the case of
asynchronous calls, passed to the appropriate
response handler
.
The connection on which the request that timed out was sent will be closed.
timeout
- The timeout (in milliseconds). A value of 0 indicates no timeout should be
enforced.
java.lang.IllegalArgumentException
- If the timeout provided is negative.public void setContentEncoding(Encoding encoding)
encoding
- public void setAcceptEncodedResponses(boolean accept)
protected void deregisterChannel(java.nio.channels.SelectableChannel channel)
deregisterChannel
in class HttpRpcProcessor
protected void registerChannel(java.nio.channels.SelectableChannel channel)
registerChannel
in class HttpRpcProcessor
protected void validateHttpResponse(RpcCall call, HttpResponseBuffer response) throws java.lang.Exception
This method centralizes (and exposes for replacement by sub-classes) the logic to validate an HTTP response method.
Typically this means converting selected status codes into local exceptions.
This implementation maps all status codes other than
200
onto an exception as follows:
404
results in a NoSuchMethodException
being raised.UnsupportedOperationException
being raised.
call
- The RPC method call the response applies to.response
- The buffer containing the HTTP response.
java.lang.Exception
- If the status code in the HTTP response is not 200
an exception is raised as described above.protected void handleTimeout(java.net.Socket socket, java.lang.Exception cause)
handleTimeout
in class HttpRpcProcessor
protected void initialize() throws java.io.IOException
initialize
in class HttpRpcProcessor
java.io.IOException
- if an error occurs during initialization.protected void stopImpl() throws java.io.IOException
stopImpl
in class HttpRpcProcessor
java.io.IOException
protected javax.net.ssl.SSLEngine initSocketSSLEngine(java.net.Socket socket) throws javax.net.ssl.SSLException
initSocketSSLEngine
in class HttpRpcProcessor
javax.net.ssl.SSLException
protected ResourcePool newWorkerPool()
HttpRpcProcessor
This is invoked from the constructor to create a new worker pool when one is not provided.
newWorkerPool
in class HttpRpcProcessor
ResourcePool
.protected void handleSelectionKeyOperation(java.nio.channels.SelectionKey key) throws java.io.IOException
connect(SelectionKey)
method if a connection
is ready for completion. In all other cases it defers to it's
parent.
handleSelectionKeyOperation
in class HttpRpcProcessor
key
- The SelectionKey
for the socket on which
an I/O operation is pending.
java.io.IOException
- if an error occurs while processing the pending event.protected void connect(java.nio.channels.SelectionKey key) throws java.io.IOException
SocketChannel
is eligible for completion.
key
- The SelectionKey
for the socket on which
a connection is pending.
java.io.IOException
- if an error occurs while completing the
connection.protected void handleMessageException(HttpMessageBuffer msg, java.lang.Exception e)
HttpMessageException
and enqueues it on the queue shared by worker threads.
handleMessageException
in class HttpRpcProcessor
msg
- The HTTP response being processed when the exception
occurred.e
- The exception that was raised.protected void handleProcessingException(java.net.Socket socket, java.lang.Exception e)
HttpRpcProcessor
handleProcessingException
in class HttpRpcProcessor
socket
- The socket the exception is associated with. This
may be null
if the exception is not
specific to a particular socket.e
- The exception that was raised.protected void handleSSLHandshakeFinished(java.net.Socket socket, javax.net.ssl.SSLEngine engine)
handleSSLHandshakeFinished
in class HttpRpcProcessor
protected HttpMessageBuffer getReadBuffer(java.net.Socket socket)
HttpRpcProcessor
Implementations must return a buffer, even if this
means creating a new instance. The same buffer should
be returned for a given socket until the
HttpRpcProcessor.removeReadBuffer(Socket)
method is invoked,
ensuring that message fragmentation is correctly handled.
getReadBuffer
in class HttpRpcProcessor
socket
- The socket on which data has arrived.
protected void removeReadBuffer(java.net.Socket socket)
HttpRpcProcessor
removeReadBuffer
in class HttpRpcProcessor
socket
- The socket on which a complete message has been
received.protected void removeReadBuffers(java.net.Socket socket)
HttpRpcProcessor
removeReadBuffers
in class HttpRpcProcessor
socket
- The socket on which a complete message has been
received.protected void putWriteBuffer(java.net.Socket socket, java.nio.ByteBuffer data)
HttpRpcProcessor
putWriteBuffer
in class HttpRpcProcessor
socket
- The socket on which the data should be sent.data
- The data to be written.protected boolean isWriteQueued(java.net.Socket socket)
HttpRpcProcessor
isWriteQueued
in class HttpRpcProcessor
socket
- The socket on which the data should be sent.
true
if there is a buffer waiting to be written on
the given socketprotected java.nio.ByteBuffer getWriteBuffer(java.net.Socket socket)
HttpRpcProcessor
Implementations should return a buffer only if one
has been added using HttpRpcProcessor.putWriteBuffer(Socket, ByteBuffer)
.
The same buffer should be returned for a given socket until the
HttpRpcProcessor.removeWriteBuffer(Socket)
method is invoked,
ensuring that large messages that must be written in
multiple fragments are correctly handled.
getWriteBuffer
in class HttpRpcProcessor
socket
- The socket that is available for writing.
protected void removeWriteBuffer(java.net.Socket socket)
HttpRpcProcessor
removeWriteBuffer
in class HttpRpcProcessor
socket
- The socket the buffer was associated with.protected void removeWriteBuffers(java.net.Socket socket)
HttpRpcProcessor
removeWriteBuffers
in class HttpRpcProcessor
socket
- The socket the buffer was associated with.protected HttpRequest newHttpRequest(RpcCall call) throws java.lang.Exception
java.lang.Exception
protected java.lang.String getUserAgent()
protected RpcResponseContext newRpcResponseContext(java.net.Socket socket, HttpResponseBuffer rsp)
protected abstract RpcCall newRpcCall(java.lang.String name, java.lang.Object[] params)
protected abstract MethodResponseUnmarshaller getMethodResponseUnmarshaller()
MethodResponseUnmarshaller
instance
for unmarshalling incoming RpcResponse
s.
Implementations may return the same unmarshaller instance for each call to this method as long as that unmarshaller is thread-safe.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |