Class RequestHandler

java.lang.Object
no.mnemonic.messaging.requestsink.RequestHandler
All Implemented Interfaces:
RequestContext

public class RequestHandler extends Object implements RequestContext
Common implementation for asynchronous response handling.

After making an asynchronous call, this requesthandler may be used for tracking responses from the requestsink.

  • Constructor Details

    • RequestHandler

      @Deprecated public RequestHandler(boolean allowKeepAlive, String callID, long maxWait)
      Deprecated.
      Use builder instead
      Initialize a RequestHandler with an unbounded internal buffering queue. If using JMSRequestSink, clients should not switch to use bounded response buffer queue unless they have enabled the V4 JMSRequestSink protocol.
      Parameters:
      allowKeepAlive - if allowKeepalive, received keepalive messages and responses will cause call timeout to be extended
      callID - the ID of the call this handler represents
      maxWait - the maximum time in milliseconds before this call times out (unless keepAlive is received)
  • Method Details

    • signal

      @Deprecated public static RequestHandler signal(RequestSink sink, Message msg, boolean allowKeepAlive, long maxWait)
      Deprecated.
      Use the builder() to create the RequestHandler, and invoke RequestSink.signal(Message, RequestContext, long) directly.
      Convenience method to create a RequestHandler and submit a signal bound to this handler as a RequestContext
      Parameters:
      sink - the sink to send to
      msg - the message to send
      allowKeepAlive - if keepalive should be allowed
      maxWait - max initial milliseconds before request times out (if no keepalive)
      Returns:
      the RequestHandler which is used as RequestContext
    • addListener

      public void addListener(RequestListener listener)
      Description copied from interface: RequestContext
      Add a RequestListener to context. A context implementation should notify any registered listeners about relevant events.
      Specified by:
      addListener in interface RequestContext
      Parameters:
      listener - listener to add
    • removeListener

      public void removeListener(RequestListener listener)
      Description copied from interface: RequestContext
      Remove a RequestListener from context
      Specified by:
      removeListener in interface RequestContext
      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 interface RequestContext
      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 interface RequestContext
    • notifyError

      public void notifyError(Throwable e)
      Description copied from interface: RequestContext
      Notify signal context that an error has occurred while waiting for data
      Specified by:
      notifyError in interface RequestContext
      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 interface RequestContext
    • addResponse

      public boolean addResponse(Message msg)
      Description copied from interface: RequestContext
      Add a signal response to give back to requesting client
      Specified by:
      addResponse in interface RequestContext
      Parameters:
      msg - the response message to add
      Returns:
      true if the response message was accepted, false if it was discarded
    • addResponse

      public boolean addResponse(Message msg, ResponseListener responseListener)
      Description copied from interface: RequestContext
      Add a signal response to give back to requesting client, with a acknoeldgement listener to notify when the response has been accepted. When using this method, clients MUST invoke ResponseListener.responseAccepted() when the response has been handled, else the sender may starve the client once the sender has sent responses enough to fill the agreed client buffer.
      Specified by:
      addResponse in interface RequestContext
      Parameters:
      msg - the response message to add
      responseListener - listener to acknowledge to when the response has been accepted.
      Returns:
      true if the response message was accepted, false if it was discarded
    • isClosed

      public boolean isClosed()
      Specified by:
      isClosed in interface RequestContext
      Returns:
      true if this request is closed down (will not accept any more responses)
    • getCallID

      public String 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

      public <T extends Message> Collection<T> getResponsesNoWait() throws InvocationTargetException
      Fetch the responses received so far, without delay. Don't wait for additional responses.
      Returns:
      all responses currently received
      Throws:
      InvocationTargetException
    • getNextResponse

      public <T extends Message> T getNextResponse() throws InvocationTargetException
      Wait for the next response to show up (since the last time this method was called). Similar to getNextResponse(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

      public <T extends Message> T getNextResponse(long maxWait) throws InvocationTargetException
      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
    • builder

      public static RequestHandler.Builder builder()