Package org.nustaq.kontraktor
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.StringCONTuse value as error to indicate more messages are to come (else remoting will close channel).static intDEFAULT_TIMOUTstatic java.util.function.Supplier<Scheduler>defaultSchedulerstatic java.util.TimerdelayedCallsstatic java.util.concurrent.ThreadPoolExecutorexecstatic ActorsImplinstancestatic intMAX_EXTERNAL_THREADS_POOL_SIZEstatic java.lang.Stringversion -
Constructor Summary
Constructors Constructor Description Actors() -
Method Summary
Modifier and Type Method Description static voidAddDeadLetter(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 settledstatic <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 settledstatic <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 settledstatic <T extends Actor>
TAsActor(java.lang.Class<T> actorClazz)create an new actor.static <T extends Actor>
TAsActor(java.lang.Class<T> actorClazz, int qSize)create an new actor.static <T extends Actor>
TAsActor(java.lang.Class<T> actorClazz, Scheduler scheduler)create an new actor dispatched in the given DispatcherThreadstatic <T extends Actor>
TAsActor(java.lang.Class<T> actorClazz, Scheduler scheduler, int qsize)create an new actor dispatched in the given DispatcherThreadstatic ActorAsUntypedActor(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 resultsstatic <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 IPromisecomplete()abbreviation for Promise creation to make code more concisestatic <T> IPromise<T>complete(T res, java.lang.Object err)abbreviation for Promise creation to make code more concisestatic java.util.Queue<java.lang.String>DeadLetters()messages that have been dropped or have been sent to stopped actorsstatic <T> KFlow<T>flow()static <T> TInThread(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, NOPstatic booleanisComplete(java.lang.Object error)static booleanisCont(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 booleanisError(java.lang.Object err)helper to check for "special" error objects.static booleanisErrorOrComplete(java.lang.Object error)return if given error Object signals an error or a 'complete' signalstatic booleanisResult(java.lang.Object error)static booleanisTimeout(java.lang.Object error)static voidkYield()processes messages from mailbox / callbackqueue until no messages are left NOP if called from non actor thread.static voidkYield(long timeout)process messages on the mailbox/callback queue until timeout is reached.static <T> Promise<T>promise()shorthand constructorstatic <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 settledstatic <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 settledstatic <T> IPromise<T>reject(java.lang.Object err)abbreviation for Promise creation to make code more concisestatic IPromiseresolve()abbreviation for Promise creation to make code more concisestatic <T> IPromise<T>resolve(T res)abbreviation for Promise creation to make code more concisestatic <T> java.util.stream.Stream<T>stream(T... t)utility addition to java 8 streamsstatic voidSubmitDelayed(long millis, java.lang.Runnable task)utility function.static voidyieldCallbacks(long timeout)only process callbacks until timeout or cbQ is empty.
-
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 java.util.concurrent.ThreadPoolExecutor exec -
version
public static final java.lang.String version- See Also:
- Constant Field Values
-
instance
-
delayedCalls
public static java.util.Timer delayedCalls -
defaultScheduler
-
CONT
public static final java.lang.String CONTuse value as error to indicate more messages are to come (else remoting will close channel).- See Also:
- Constant Field Values
-
-
Constructor Details
-
Actors
public Actors()
-
-
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
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
-
AsActor
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
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
-
all
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
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
similar all but map promises to their content returns a future which is settled once all promises provided are settled -
awaitAll
await until all futures are settled and stream their results -
awaitAll
await until all futures are settled and stream their results. Uses Actors.DEFAULT_TIMEOUT -
awaitAll
-
awaitAll
public static <T> java.util.stream.Stream<T> awaitAll(long timeoutMS, java.util.List<IPromise<T>> futures) -
race
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
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
-
resolve
abbreviation for Promise creation to make code more concise- Type Parameters:
T-- Parameters:
res-- Returns:
-
reject
abbreviation for Promise creation to make code more concise -
promise
shorthand constructor- Type Parameters:
T-- Returns:
-
complete
abbreviation for Promise creation to make code more concise -
complete
abbreviation for Promise creation to make code more concise -
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-
-