Class Actors

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

public class Actors
extends java.lang.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 java.lang.String CONT
    use value as error to indicate more messages are to come (else remoting will close channel).
    static int DEFAULT_TIMOUT  
    static java.util.function.Supplier<Scheduler> defaultScheduler  
    static java.util.Timer delayedCalls  
    static java.util.concurrent.ThreadPoolExecutor exec  
    static ActorsImpl instance  
    static int MAX_EXTERNAL_THREADS_POOL_SIZE  
    static java.lang.String version  
  • Constructor Summary

    Constructors 
    Constructor Description
    Actors()  
  • Method Summary

    Modifier and Type Method Description
    static void AddDeadLetter​(java.lang.String s)  
    static <T> IPromise<IPromise<T>[]> all​(int count, java.util.function.Function<java.lang.Integer,​IPromise<T>> loop)  
    static <T> IPromise<java.util.List<IPromise<T>>> all​(java.util.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<java.util.List<T>> allMapped​(java.util.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​(java.lang.Class<T> actorClazz)
    create an new actor.
    static <T extends Actor>
    T
    AsActor​(java.lang.Class<T> actorClazz, int qSize)
    create an new actor.
    static <T extends Actor>
    T
    AsActor​(java.lang.Class<T> actorClazz, Scheduler scheduler)
    create an new actor dispatched in the given DispatcherThread
    static <T extends Actor>
    T
    AsActor​(java.lang.Class<T> actorClazz, Scheduler scheduler, int qsize)
    create an new actor dispatched in the given DispatcherThread
    static Actor AsUntypedActor​(Actor instance)  
    static <T> java.util.stream.Stream<T> awaitAll​(long timeoutMS, java.util.List<IPromise<T>> futures)  
    static <T> java.util.stream.Stream<T> awaitAll​(long timeoutMS, IPromise<T>... futures)
    await until all futures are settled and stream their results
    static <T> java.util.stream.Stream<T> awaitAll​(java.util.List<IPromise<T>> futures)  
    static <T> java.util.stream.Stream<T> awaitAll​(IPromise<T>... futures)
    await until all futures are settled and stream their results.
    static IPromise complete()
    abbreviation for Promise creation to make code more concise
    static <T> IPromise<T> complete​(T res, java.lang.Object err)
    abbreviation for Promise creation to make code more concise
    static java.util.Queue<java.lang.String> DeadLetters()
    messages that have been dropped or have been sent to stopped actors
    static <T> KFlow<T> flow()  
    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 isComplete​(java.lang.Object error)  
    static boolean isCont​(java.lang.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)
    static boolean isError​(java.lang.Object err)
    helper to check for "special" error objects.
    static boolean isErrorOrComplete​(java.lang.Object error)
    return if given error Object signals an error or a 'complete' signal
    static boolean isResult​(java.lang.Object error)  
    static boolean isTimeout​(java.lang.Object error)  
    static void kYield()
    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> promise()
    shorthand constructor
    static <T> IPromise<T> race​(java.util.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> reject​(java.lang.Object err)
    abbreviation for Promise creation to make code more concise
    static IPromise resolve()
    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> java.util.stream.Stream<T> stream​(T... t)
    utility addition to java 8 streams
    static void SubmitDelayed​(long millis, java.lang.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

  • Constructor Details

  • Method Details

    • isErrorOrComplete

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

      public static boolean isTimeout​(java.lang.Object error)
    • isComplete

      public static boolean isComplete​(java.lang.Object error)
    • isCont

      public static boolean isCont​(java.lang.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​(java.lang.Object error)
    • isError

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

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

      public static void AddDeadLetter​(java.lang.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 java.util.Queue<java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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, java.util.function.Function<java.lang.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<java.util.List<IPromise<T>>> all​(java.util.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<java.util.List<T>> allMapped​(java.util.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> java.util.stream.Stream<T> awaitAll​(long timeoutMS, IPromise<T>... futures)
      await until all futures are settled and stream their results
    • awaitAll

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

      public static <T> java.util.stream.Stream<T> awaitAll​(java.util.List<IPromise<T>> futures)
    • awaitAll

      public static <T> java.util.stream.Stream<T> awaitAll​(long timeoutMS, java.util.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​(java.util.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> java.util.stream.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​(java.lang.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, java.lang.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 IPromise 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 -