Class Actors

java.lang.Object
org.nustaq.kontraktor.Actors
Direct Known Subclasses:
Actor

public class Actors extends Object
Copyright (c) 2012, Ruediger Moeller. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Date: 04.01.14 Time: 19:50 A set of static async helper methods. Note Actor inherits from this class.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    use value as error to indicate more messages are to come (else remoting will close channel).
    static int
     
     
    static Timer
     
     
    static ActorsImpl
     
    static int
     
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
     
    static <T> IPromise<IPromise<T>[]>
    all(int count, Function<Integer,IPromise<T>> loop)
     
    static <T> IPromise<List<IPromise<T>>>
    all(List<IPromise<T>> futures)
    similar to es6 Promise.all method, however non-IPromise objects are not allowed returns a future which is settled once all promises provided are settled
    static <T> IPromise<IPromise<T>[]>
    all(IPromise<T>... futures)
    similar to es6 Promise.all method, however non-IPromise objects are not allowed returns a future which is settled once all promises provided are settled
    static <T> IPromise<List<T>>
    allMapped(List<IPromise<T>> futures)
    similar all but map promises to their content returns a future which is settled once all promises provided are settled
    static <T extends Actor>
    T
    AsActor(Class<T> actorClazz)
    create an new actor.
    static <T extends Actor>
    T
    AsActor(Class<T> actorClazz, int qSize)
    create an new actor.
    static <T extends Actor>
    T
    AsActor(Class<T> actorClazz, Scheduler scheduler)
    create an new actor dispatched in the given DispatcherThread
    static <T extends Actor>
    T
    AsActor(Class<T> actorClazz, Scheduler scheduler, int qsize)
    create an new actor dispatched in the given DispatcherThread
    static Actor
     
    static <T> Stream<T>
    awaitAll(long timeoutMS, List<IPromise<T>> futures)
     
    static <T> Stream<T>
    awaitAll(long timeoutMS, IPromise<T>... futures)
    await until all futures are settled and stream their results
    static <T> Stream<T>
    awaitAll(List<IPromise<T>> futures)
     
    static <T> Stream<T>
    awaitAll(IPromise<T>... futures)
    await until all futures are settled and stream their results.
    static IPromise
    abbreviation for Promise creation to make code more concise
    static <T> IPromise<T>
    complete(T res, Object err)
    abbreviation for Promise creation to make code more concise
    static Queue<String>
    messages that have been dropped or have been sent to stopped actors
    static <T> KFlow<T>
     
    static <T> T
    InThread(T anInterface)
    in case called from an actor, wraps the given interface instance into a proxy such that all calls on the interface get scheduled on the calling actors thread (avoids accidental multithreading when handing out callback/listener interfaces from an actor) if called from outside an actor thread, NOP
    static boolean
     
    static boolean
    helper to check for "special" error object "CONT". cont signals further callback results might be sent (important for remoting as channels need to get cleaned up)
    static boolean
    helper to check for "special" error objects.
    static boolean
    return if given error Object signals an error or a 'complete' signal
    static boolean
     
    static boolean
     
    static void
    processes messages from mailbox / callbackqueue until no messages are left NOP if called from non actor thread.
    static void
    kYield(long timeout)
    process messages on the mailbox/callback queue until timeout is reached.
    static <T> Promise<T>
    shorthand constructor
    static <T> IPromise<T>
    race(Collection<IPromise<T>> futures)
    similar to es6 Promise.race method, however non-IPromise objects are not allowed returns a future which is settled once one of the futures provided gets settled
    static <T> IPromise<T>
    race(IPromise<T>... futures)
    similar to es6 Promise.race method, however non-IPromise objects are not allowed returns a future which is settled once one of the futures provided gets settled
    static <T> IPromise<T>
    abbreviation for Promise creation to make code more concise
    static <T> IPromise<T>
    abbreviation for Promise creation to make code more concise
    static <T> IPromise<T>
    resolve(T res)
    abbreviation for Promise creation to make code more concise
    static <T> Stream<T>
    stream(T... t)
    utility addition to java 8 streams
    static void
    SubmitDelayed(long millis, Runnable task)
    utility function.
    static void
    yieldCallbacks(long timeout)
    only process callbacks until timeout or cbQ is empty.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_EXTERNAL_THREADS_POOL_SIZE

      public static int MAX_EXTERNAL_THREADS_POOL_SIZE
    • DEFAULT_TIMOUT

      public static int DEFAULT_TIMOUT
    • exec

      public static ThreadPoolExecutor exec
    • version

      public static final String version
      See Also:
    • instance

      public static ActorsImpl instance
    • delayedCalls

      public static Timer delayedCalls
    • defaultScheduler

      public static Supplier<Scheduler> defaultScheduler
    • CONT

      public static final String CONT
      use value as error to indicate more messages are to come (else remoting will close channel).
      See Also:
  • Constructor Details

    • Actors

      public Actors()
  • Method Details

    • isErrorOrComplete

      public static boolean isErrorOrComplete(Object error)
      return if given error Object signals an error or a 'complete' signal
      Parameters:
      error -
      Returns:
    • isTimeout

      public static boolean isTimeout(Object error)
    • isComplete

      public static boolean isComplete(Object error)
    • isCont

      public static boolean isCont(Object o)
      helper to check for "special" error object "CONT". cont signals further callback results might be sent (important for remoting as channels need to get cleaned up)
      Parameters:
      o -
      Returns:
    • isResult

      public static boolean isResult(Object error)
    • isError

      public static boolean isError(Object err)
      helper to check for "special" error objects.
      Parameters:
      err -
      Returns:
    • SubmitDelayed

      public static void SubmitDelayed(long millis, Runnable task)
      utility function. Executed in foreign thread. Use Actor::delayed() to have the runnable executed inside actor thread
    • AddDeadLetter

      public static void AddDeadLetter(String s)
    • InThread

      public static <T> T InThread(T anInterface)
      in case called from an actor, wraps the given interface instance into a proxy such that all calls on the interface get scheduled on the calling actors thread (avoids accidental multithreading when handing out callback/listener interfaces from an actor) if called from outside an actor thread, NOP
      Type Parameters:
      T -
      Parameters:
      anInterface -
      Returns:
    • DeadLetters

      public static Queue<String> DeadLetters()
      messages that have been dropped or have been sent to stopped actors
      Returns:
      queue of dead letters. Note: only strings are recorded to avoid accidental references.
    • AsActor

      public static <T extends Actor> T AsActor(Class<T> actorClazz)
      create an new actor. If this is called outside an actor, a new DispatcherThread will be scheduled. If called from inside actor code, the new actor will share the thread+queue with the caller.
      Type Parameters:
      T -
      Parameters:
      actorClazz -
      Returns:
    • AsUntypedActor

      public static Actor AsUntypedActor(Actor instance)
    • AsActor

      public static <T extends Actor> T AsActor(Class<T> actorClazz, int qSize)
      create an new actor. If this is called outside an actor, a new DispatcherThread will be scheduled. If called from inside actor code, the new actor will share the thread+queue with the caller.
      Type Parameters:
      T -
      Parameters:
      actorClazz -
      Returns:
    • AsActor

      public static <T extends Actor> T AsActor(Class<T> actorClazz, Scheduler scheduler)
      create an new actor dispatched in the given DispatcherThread
      Type Parameters:
      T -
      Parameters:
      actorClazz -
      Returns:
    • AsActor

      public static <T extends Actor> T AsActor(Class<T> actorClazz, Scheduler scheduler, int qsize)
      create an new actor dispatched in the given DispatcherThread
      Type Parameters:
      T -
      Parameters:
      actorClazz -
      Returns:
    • all

      public static <T> IPromise<IPromise<T>[]> all(int count, Function<Integer,IPromise<T>> loop)
    • all

      public static <T> IPromise<IPromise<T>[]> all(IPromise<T>... futures)
      similar to es6 Promise.all method, however non-IPromise objects are not allowed returns a future which is settled once all promises provided are settled
    • all

      public static <T> IPromise<List<IPromise<T>>> all(List<IPromise<T>> futures)
      similar to es6 Promise.all method, however non-IPromise objects are not allowed returns a future which is settled once all promises provided are settled
    • allMapped

      public static <T> IPromise<List<T>> allMapped(List<IPromise<T>> futures)
      similar all but map promises to their content returns a future which is settled once all promises provided are settled
    • awaitAll

      public static <T> Stream<T> awaitAll(long timeoutMS, IPromise<T>... futures)
      await until all futures are settled and stream their results
    • awaitAll

      public static <T> Stream<T> awaitAll(IPromise<T>... futures)
      await until all futures are settled and stream their results. Uses Actors.DEFAULT_TIMEOUT
    • awaitAll

      public static <T> Stream<T> awaitAll(List<IPromise<T>> futures)
    • awaitAll

      public static <T> Stream<T> awaitAll(long timeoutMS, List<IPromise<T>> futures)
    • race

      public static <T> IPromise<T> race(IPromise<T>... futures)
      similar to es6 Promise.race method, however non-IPromise objects are not allowed returns a future which is settled once one of the futures provided gets settled
    • race

      public static <T> IPromise<T> race(Collection<IPromise<T>> futures)
      similar to es6 Promise.race method, however non-IPromise objects are not allowed returns a future which is settled once one of the futures provided gets settled
    • stream

      public static <T> Stream<T> stream(T... t)
      utility addition to java 8 streams
      Type Parameters:
      T -
      Parameters:
      t -
      Returns:
    • flow

      public static <T> KFlow<T> flow()
    • resolve

      public static <T> IPromise<T> resolve(T res)
      abbreviation for Promise creation to make code more concise
      Type Parameters:
      T -
      Parameters:
      res -
      Returns:
    • reject

      public static <T> IPromise<T> reject(Object err)
      abbreviation for Promise creation to make code more concise
    • promise

      public static <T> Promise<T> promise()
      shorthand constructor
      Type Parameters:
      T -
      Returns:
    • complete

      public static <T> IPromise<T> complete(T res, Object err)
      abbreviation for Promise creation to make code more concise
    • complete

      public static IPromise complete()
      abbreviation for Promise creation to make code more concise
    • resolve

      public static <T> IPromise<T> resolve()
      abbreviation for Promise creation to make code more concise
    • kYield

      public static void kYield()
      processes messages from mailbox / callbackqueue until no messages are left NOP if called from non actor thread.
    • kYield

      public static void kYield(long timeout)
      process messages on the mailbox/callback queue until timeout is reached. In case timeout is 0, process until mailbox+callback queue is empty. If called from a non-actor thread, either sleep until timeout or (if timeout == 0) its a NOP.
      Parameters:
      timeout -
    • yieldCallbacks

      public static void yieldCallbacks(long timeout)
      only process callbacks until timeout or cbQ is empty. Messages are not polled and processed. Useful in order to stall a producer, but preserve message processing order.
      Parameters:
      timeout -