Class RequestHandler
java.lang.Object
no.mnemonic.messaging.requestsink.RequestHandler
- All Implemented Interfaces:
RequestContext
Common implementation for asynchronous response handling.
After making an asynchronous call, this requesthandler may be used for tracking responses from the requestsink.
-
Constructor Summary
ConstructorDescriptionRequestHandler
(boolean allowKeepAlive, String callID, long maxWait) Initialize a RequestHandler -
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Abort this handler, notifying all listening resources.void
addListener
(RequestListener listener) Add aRequestListener
to context.boolean
addResponse
(Message msg) Add a signal response to give back to requesting clientvoid
close()
Close this handler, closing all listening resources.void
Signal end of stream (the current context holder will not provide any more data).<T extends Message>
TWait for the next response to show up (since the last time this method was called).<T extends Message>
TgetNextResponse
(long maxWait) Wait for the next response to show up (since the last time this method was called).<T extends Message>
Collection<T>getResponses
(long maxWait, int maxResults) Same as getResponses(long), but will return immediately when maxResults results are returned, without waiting for more results.<T extends Message>
Collection<T>Fetch the responses received so far, without delay.boolean
boolean
isClosed()
boolean
keepAlive
(long until) Request that this request should be kept alive a bit longervoid
Notify signal context that server has closed this request.void
Notify signal context that an error has occurred while waiting for datavoid
removeListener
(RequestListener listener) Remove a RequestListener from contextstatic RequestHandler
signal
(RequestSink sink, Message msg, boolean allowKeepAlive, long maxWait) Convenience method to create a RequestHandler and submit a signal bound to this handler as a RequestContextvoid
timeout()
Signal unexpected timeout to request listenersboolean
waitForEndOfStream
(long maxWait) Wait for end-of-stream.
-
Constructor Details
-
RequestHandler
Initialize a RequestHandler- Parameters:
allowKeepAlive
- if allowKeepalive, received keepalive messages and responses will cause call timeout to be extendedcallID
- the ID of the call this handler representsmaxWait
- the maximum time in milliseconds before this call times out (unless keepAlive is received)
-
-
Method Details
-
signal
public static RequestHandler signal(RequestSink sink, Message msg, boolean allowKeepAlive, long maxWait) Convenience method to create a RequestHandler and submit a signal bound to this handler as a RequestContext- Parameters:
sink
- the sink to send tomsg
- the message to sendallowKeepAlive
- if keepalive should be allowedmaxWait
- max initial milliseconds before request times out (if no keepalive)- Returns:
- the RequestHandler which is used as RequestContext
-
addListener
Description copied from interface:RequestContext
Add aRequestListener
to context. A context implementation should notify any registered listeners about relevant events.- Specified by:
addListener
in interfaceRequestContext
- Parameters:
listener
- listener to add
-
removeListener
Description copied from interface:RequestContext
Remove a RequestListener from context- Specified by:
removeListener
in interfaceRequestContext
- Parameters:
listener
- listener to remove
-
keepAlive
public boolean keepAlive(long until) Description copied from interface:RequestContext
Request that this request should be kept alive a bit longer- Specified by:
keepAlive
in interfaceRequestContext
- Parameters:
until
- Ask this request to keep open at least until this timestamp- Returns:
- true if the request is granted (will not guarantee that endpoint clients keep this up)
-
endOfStream
public void endOfStream()Description copied from interface:RequestContext
Signal end of stream (the current context holder will not provide any more data).- Specified by:
endOfStream
in interfaceRequestContext
-
notifyError
Description copied from interface:RequestContext
Notify signal context that an error has occurred while waiting for data- Specified by:
notifyError
in interfaceRequestContext
- Parameters:
e
- exception that was caught
-
notifyClose
public void notifyClose()Description copied from interface:RequestContext
Notify signal context that server has closed this request. The request may already be closed on the client side.- Specified by:
notifyClose
in interfaceRequestContext
-
addResponse
Description copied from interface:RequestContext
Add a signal response to give back to requesting client- Specified by:
addResponse
in interfaceRequestContext
- Parameters:
msg
- the response message to add- Returns:
- true if the response message was accepted, false if it was discarded
-
isClosed
public boolean isClosed()- Specified by:
isClosed
in interfaceRequestContext
- Returns:
- true if this request is closed down (will not accept any more responses)
-
getCallID
- Returns:
- the callID this handler is bound to
-
waitForEndOfStream
public boolean waitForEndOfStream(long maxWait) Wait for end-of-stream. Method will return true when end-of-stream is received, or timeout has occurred. If handler accepts keepalives, this means that waitForEndOfStream may return false when waiting for the configured maxwait millis, because timeout is extended. However, method will return (true or false) no later than maxWait.- Parameters:
maxWait
- do not wait longer than maxWait ms- Returns:
- true if handler is closed (end of stream or timeout), false otherwise
-
hasReceivedError
public boolean hasReceivedError()- Returns:
- true if the response stream has received an error
-
close
public void close()Close this handler, closing all listening resources. Further responses to this signal will be ignored -
abort
public void abort()Abort this handler, notifying all listening resources. This will also close the handler,so further signals are ignored. -
timeout
public void timeout()Signal unexpected timeout to request listeners -
getResponsesNoWait
Fetch the responses received so far, without delay. Don't wait for additional responses.- Returns:
- all responses currently received
- Throws:
InvocationTargetException
-
getNextResponse
Wait for the next response to show up (since the last time this method was called). Similar togetNextResponse(long)
, but will continue waiting until a response is received, or until end of stream/stream timeout. If stream is kept alive, this may wait indefinetely- Returns:
- the first response to show up, or null if the stream was closed before any responses show up
- Throws:
InvocationTargetException
-
getNextResponse
Wait for the next response to show up (since the last time this method was called).- Parameters:
maxWait
- max millis to wait before returning- Returns:
- the first response to show up, or null if no responses were recorded within maxWait millis or end of stream.
- Throws:
InvocationTargetException
-
getResponses
public <T extends Message> Collection<T> getResponses(long maxWait, int maxResults) throws InvocationTargetException Same as getResponses(long), but will return immediately when maxResults results are returned, without waiting for more results.- Parameters:
maxWait
- max millis to wait before returning.maxResults
- max results to receive before returning.- Returns:
- a collection with the results received up until maxWait millis, or maxResults results, whatever happens first.
- Throws:
InvocationTargetException
-