Class CallbackWrapper<T>

java.lang.Object
org.nustaq.kontraktor.impl.CallbackWrapper<T>
All Implemented Interfaces:
Serializable, Callback<T>, IPromise<T>

public class CallbackWrapper<T> extends Object implements IPromise<T>, Serializable
If a promise or callback is wrapped by this, it will be treated correctly when remoted. If callback/promises are part of an actor's async method signature (parameter), kontraktor will automatically use this class to wrap such that remote calls work correctly. However if a promise or callback is embedded inside some Pojo, and this pojo is sent over the network, Promises and Callbacks do not work (performance issues, deep scan with many instanceof's required). Note that identity of callbacks / promises gets lost when sent over network, e.g. when passing the same callback twice to a remote actor, on the remote actor these callback objects will not be equal (=> cannot hash on calbacks on remote side, e.g. for subscription schemes). Callbacks/Promises are lightweigt remote objects while remote actor references are hashed and managed by the framework in order to be able to detect identiy (remoteId).
See Also:
  • Constructor Details

    • CallbackWrapper

      public CallbackWrapper(Actor targetQ, Callback<T> realFuture)
  • Method Details

    • isRouted

      public boolean isRouted()
    • complete

      public void complete(T result, Object error)
      Description copied from interface: Callback
      set result or error. error might also contain flow indicators to signal end/continue of stream when remoting. (Actor.FIN Actor.CONT)
      Specified by:
      complete in interface Callback<T>
    • getRealCallback

      public Callback<T> getRealCallback()
    • then

      public IPromise<T> then(Runnable result)
      Warning: this will not be called on error or timeout
      Specified by:
      then in interface IPromise<T>
      Parameters:
      result -
      Returns:
    • thenAnd

      public IPromise<T> thenAnd(Supplier<IPromise<T>> result)
      Description copied from interface: IPromise
      called once any result of a future becomes available Can be used in case a sender is not interested in the actual result but when a remote method has finished processing. e.g. actor.asyncMehod().then( () -> { furtherProcessing(); return new Promise("result"); } );
      Specified by:
      thenAnd in interface IPromise<T>
      Returns:
      a future ressolved with the Supüplier result after this
    • then

      public IPromise then(Callback<T> result)
      Warning: this will not be called on error or timeout
      Specified by:
      then in interface IPromise<T>
      Parameters:
      result -
      Returns:
    • onResult

      public IPromise<T> onResult(Consumer<T> resultHandler)
      Warning: this will not be called on error or timeout
      Specified by:
      onResult in interface IPromise<T>
      Parameters:
      resultHandler -
      Returns:
    • onError

      public IPromise<T> onError(Consumer errorHandler)
      Description copied from interface: IPromise
      called when an error is set as the result forwards to (new) "catchError" variant.
      Specified by:
      onError in interface IPromise<T>
      Returns:
    • onTimeout

      public IPromise<T> onTimeout(Consumer timeoutHandler)
      Description copied from interface: IPromise
      called when the async call times out. see 'timeOutIn'
      Specified by:
      onTimeout in interface IPromise<T>
      Returns:
    • thenAnd

      public <OUT> IPromise<OUT> thenAnd(Function<T,IPromise<OUT>> function)
      Description copied from interface: IPromise
      called once any result of a future becomes available Can be used in case a sender is not interested in the actual result but when a remote method has finished processing. e.g. actor.asyncMehod().then( () -> { furtherProcessing(); return new Promise("result"); } );
      Specified by:
      thenAnd in interface IPromise<T>
      Returns:
      a future ressolved with the Function result after this
    • then

      public <OUT> IPromise<OUT> then(Consumer<T> function)
      Description copied from interface: IPromise
      called once any result of a future becomes available Can be used in case a sender is not interested in the actual result but when a remote method has finished processing. e.g. actor.asyncMethod().then( () -> { furtherProcessing(); return new Promise("result"); } );
      Specified by:
      then in interface IPromise<T>
      Returns:
      a future ressolved empty after this
    • catchError

      public <OUT> IPromise<OUT> catchError(Function<Object,IPromise<OUT>> function)
      Description copied from interface: IPromise
      called if an error has been signaled by one of the futures in the previous future chain. e.e. actor.async().then( ).then( ).then( ).catchError( error -> .. );
      Specified by:
      catchError in interface IPromise<T>
    • catchError

      public <OUT> IPromise<OUT> catchError(Consumer<Object> function)
      Description copied from interface: IPromise
      called if an error has been signaled by one of the futures in the previous future chain. e.e. actor.async().then( ).then( ).then( ).catchError( () -> .. );
      Specified by:
      catchError in interface IPromise<T>
    • get

      public T get()
      Description copied from interface: IPromise
      Warning: this is different to JDK's BLOCKING future
      Specified by:
      get in interface IPromise<T>
      Returns:
      result if avaiable (no blocking no awaiting).
    • await

      public T await(long timeout)
      Description copied from interface: IPromise
      schedule other events/messages until future is resolved/settled (Nonblocking delay). In case this is called from a non-actor thread, the current thread is blocked until the result is avaiable. If the future is rejected (resolves to an error) an excpetion is raised. if timeout is 0l - wait forever.
      Specified by:
      await in interface IPromise<T>
      Returns:
      the futures result or throw exception in case of error
    • awaitPromise

      public IPromise<T> awaitPromise(long timeout)
      Description copied from interface: IPromise
      schedule other events/messages until future is resolved/settled (Nonblocking delay). In case this is called from a non-actor thread, the current thread is blocked until the result is avaiable.
      Specified by:
      awaitPromise in interface IPromise<T>
      Returns:
      the settled promise. No Exception is thrown, but the exception can be obtained by IPromise.getError()
    • getError

      public Object getError()
      Specified by:
      getError in interface IPromise<T>
      Returns:
      error if avaiable
    • timeoutIn

      public IPromise timeoutIn(long millis)
      Description copied from interface: IPromise
      tellMsg the future to call the onTimeout callback in N milliseconds if future is not settled until then
      Specified by:
      timeoutIn in interface IPromise<T>
      Returns:
      this for chaining
    • isSettled

      public boolean isSettled()
      Specified by:
      isSettled in interface IPromise<T>
      Returns:
      wether an error or a result has been set to this future
    • isRemote

      public boolean isRemote()
    • isTerminated

      public boolean isTerminated()
      Description copied from interface: Callback
      relevant for remoted callback's
      Specified by:
      isTerminated in interface Callback<T>
      Returns:
      if the corresponding remote connection is closed if any