@SuppressWarnings("unchecked") default <T extends ActorExtension> T getFirstExtension(Class<T> itemType) { final List<ActorExtension> extensions = getExtensions(); return extensions == null ? null : (T) extensions.stream().filter(p -> itemType.isInstance(p)).findFirst().orElse(null); }
@SuppressWarnings("unchecked") public static <T> T cast(Class<T> remoteInterface, Actor actor) { return (T) Proxy.newProxyInstance(DefaultDescriptorFactory.class.getClassLoader(), new Class[]{ remoteInterface }, (proxy, method, args) -> { // TODO: throw proper exceptions for the expected error scenarios (non task return), final int methodId = instance.dynamicReferenceFactory.getMethodId(method); return ActorRuntime.getRuntime() .invoke(RemoteReference.from(actor), method, method.isAnnotationPresent(OneWay.class), methodId, args); }); } }
protected StreamProvider getStreamProvider(String name) { StreamProvider provider = runtime.getStreamProvider(name); // obs.: the actor runtime wraps the StreamProvider // to use an actor executor for call backs // and to ensure that the actor unsubscribes on deactivation. return provider; } }
public static Task<Void> flushAll(Actor actor) { final List<NodeAddress> nodes = ActorRuntime.getRuntime().getAllNodes(); return Task.allOf(nodes.stream() .map(nodeAddress -> DefaultDescriptorFactory.observerRef(nodeAddress, ExecutionCacheFlushObserver.class, "").flush(actor))); } }
ActorRuntime.getRuntime().clock().millis(), transactionId, event, args)); return (CompletableFuture) super.interceptStateMethod(method, event, args); ActorRuntime.getRuntime().clock().millis(), transactionId, event, args));
if (oldContext == null) final ActorRuntime runtime = ActorRuntime.getRuntime(); if (runtime != null) runtime.bind();
@SuppressWarnings("unchecked") public Task<Void> unsubscribe(String handle) { StreamSubscription subscription = observerMap.remove(handle); // no more messages if (subscription != null && subscription.valid) { subscription.valid = false; await(subscription.handle); await(runtime.getStream(subscription.provider, subscription.dataClass, subscription.streamId) .unsubscribe(subscription.handle.join())); observerMap.remove(handle); } return Task.done(); }
/** * Registers a timer for the current actor. The timer is automatically disposed ondeactivation. * The timer calls will keep the actor active. Timer calls are serialized according to the actor policy. * Each stateless actor activation has it's on set of timers. * * @param futureCallable a callable that returns a Task * @param dueTime Time to the first timer call * @param period Interval between calls, if period <= 0 then the timer will be single shot. * @param timeUnit Time unit for dueTime and period * @return A registration object that allows the actor to cancel the timer. */ protected Registration registerTimer(Callable<Task<?>> futureCallable, long dueTime, long period, TimeUnit timeUnit) { return runtime.registerTimer(this, futureCallable, dueTime, period, timeUnit); }
/** * Registers or updated a persisted reminder. * Reminders are low frequency persisted timers. * They survive the actor's deactivation and even a cluster restart. * * @param reminderName the remainder's name * @return completion promise for this operation */ protected Task<?> registerReminder(String reminderName, long dueTime, long period, TimeUnit timeUnit) { if (!(this instanceof Remindable)) { throw new IllegalArgumentException("This must implement IRemindable: " + this.getClass().getName()); } return runtime.registerReminder((Remindable) reference, reminderName, dueTime, period, timeUnit); }
@SuppressWarnings("unchecked") public Task<String> subscribe(String provider, Class dataClass, String streamId) { String handle = IdUtils.urlSafeString(128); StreamSubscription subscription = new StreamSubscription(); subscription.provider = provider; subscription.dataClass = dataClass; subscription.streamId = streamId; subscription.handle = runtime.getStream(provider, dataClass, streamId).subscribe(new AsyncObserver() { @Override public Task<Void> onNext(final Object data, final StreamSequenceToken sequenceToken) { if (subscription.valid && peer.getPipeline().isActive()) { return peer.getPipeline().write(new Invocation()); } return Task.done(); } }, null); observerMap.put(handle, subscription); await(subscription.handle); return Task.fromValue(handle); }
@SuppressWarnings("unchecked") default <T extends ActorExtension> List<T> getAllExtensions(Class<T> itemType) { final List<ActorExtension> extensions = getExtensions(); return extensions == null ? Collections.emptyList() : (List<T>) extensions.stream().filter(p -> itemType.isInstance(p)).collect(Collectors.toList()); }