Java Code Examples for reactor.core.Exceptions#failWithRejected()

The following examples show how to use reactor.core.Exceptions#failWithRejected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FluxPublishOnTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public Worker createWorker() {
	return new Worker() {

		@Override
		public Disposable schedule(Runnable task) {
			throw Exceptions.failWithRejected();
		}

		@Override
		public void dispose() {

		}

		@Override
		public boolean isDisposed() {
			return isWorkerTerminated;
		}
	};
}
 
Example 2
Source File: VirtualTimeScheduler.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public Disposable schedule(Runnable run) {
	if (shutdown) {
		throw Exceptions.failWithRejected();
	}
	final TimedRunnable timedTask = new TimedRunnable(this,
			0,
			run,
			COUNTER.getAndIncrement(VirtualTimeScheduler.this));
	queue.add(timedTask);
	drain();
	return () -> {
		queue.remove(timedTask);
		drain();
	};
}
 
Example 3
Source File: VirtualTimeScheduler.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public Disposable schedule(Runnable run, long delayTime, TimeUnit unit) {
	if (shutdown) {
		throw Exceptions.failWithRejected();
	}
	final TimedRunnable timedTask = new TimedRunnable(this,
			nanoTime + unit.toNanos(delayTime),
			run,
			COUNTER.getAndIncrement(VirtualTimeScheduler.this));
	queue.add(timedTask);
	drain();
	return () -> {
		queue.remove(timedTask);
		drain();
	};
}
 
Example 4
Source File: Operators.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
/**
 * Return a wrapped {@link RejectedExecutionException} which can be thrown by the
 * operator. This exception denotes that an execution was rejected by a
 * {@link reactor.core.scheduler.Scheduler}, notably when it was already disposed.
 * <p>
 * Wrapping is done by calling both {@link Exceptions#failWithRejected(Throwable)} and
 * {@link #onOperatorError(Subscription, Throwable, Object, Context)} (with the passed
 * {@link Subscription}).
 *
 * @param original the original execution error
 * @param subscription the subscription to pass to onOperatorError.
 * @param suppressed a Throwable to be suppressed by the {@link RejectedExecutionException} (or null if not relevant)
 * @param dataSignal a value to be passed to {@link #onOperatorError(Subscription, Throwable, Object, Context)} (or null if not relevant)
 * @param context a context that might hold a local error consumer
 */
public static RuntimeException onRejectedExecution(Throwable original,
		@Nullable Subscription subscription,
		@Nullable Throwable suppressed,
		@Nullable Object dataSignal,
		Context context) {
	//we "cheat" to apply the special key for onRejectedExecution in onOperatorError
	if (context.hasKey(Hooks.KEY_ON_REJECTED_EXECUTION)) {
		context = context.put(Hooks.KEY_ON_OPERATOR_ERROR, context.get(Hooks.KEY_ON_REJECTED_EXECUTION));
	}

	//don't create REE if original is a reactor-produced REE (not including singletons)
	RejectedExecutionException ree = Exceptions.failWithRejected(original);
	if (suppressed != null) {
		ree.addSuppressed(suppressed);
	}
	if (dataSignal != null) {
		return Exceptions.propagate(Operators.onOperatorError(subscription, ree,
				dataSignal, context));
	}
	return Exceptions.propagate(Operators.onOperatorError(subscription, ree, context));
}
 
Example 5
Source File: ExecutorScheduler.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	if(terminated){
		throw Exceptions.failWithRejected();
	}
	Objects.requireNonNull(task, "task");
	ExecutorPlainRunnable r = new ExecutorPlainRunnable(task);
	//RejectedExecutionException are propagated up, but since Executor doesn't from
	//failing tasks we'll also wrap the execute call in a try catch:
	try {
		executor.execute(r);
	}
	catch (Throwable ex) {
		if (executor instanceof ExecutorService && ((ExecutorService) executor).isShutdown()) {
			terminated = true;
		}
		Schedulers.handleError(ex);
		throw Exceptions.failWithRejected(ex);
	}
	return r;
}
 
Example 6
Source File: ExecutorScheduler.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	Objects.requireNonNull(task, "task");

	ExecutorTrackedRunnable r = new ExecutorTrackedRunnable(task, this, true);
	if (!tasks.add(r)) {
		throw Exceptions.failWithRejected();
	}

	try {
		executor.execute(r);
	}
	catch (Throwable ex) {
		tasks.remove(r);
		Schedulers.handleError(ex);
		throw Exceptions.failWithRejected(ex);
	}

	return r;
}
 
Example 7
Source File: OperatorsTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnRejectedWithReactorRee() {
	Exception originalCause = new Exception("boom");
	RejectedExecutionException original = Exceptions.failWithRejected(originalCause);
	Exception suppressed = new Exception("suppressed");

	RuntimeException test = Operators.onRejectedExecution(original,
			null, suppressed, null, Context.empty());

	assertThat(test)
			.isSameAs(original)
			.hasSuppressedException(suppressed);
}
 
Example 8
Source File: VirtualTimeScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	if (shutdown) {
		throw Exceptions.failWithRejected();
	}
	return directWorker.schedule(task);
}
 
Example 9
Source File: Schedulers.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
static Disposable workerSchedule(ScheduledExecutorService exec,
		Disposable.Composite tasks,
		Runnable task,
		long delay,
		TimeUnit unit) {
	task = onSchedule(task);

	WorkerTask sr = new WorkerTask(task, tasks);
	if (!tasks.add(sr)) {
		throw Exceptions.failWithRejected();
	}

	try {
		Future<?> f;
		if (delay <= 0L) {
			f = exec.submit((Callable<?>) sr);
		}
		else {
			f = exec.schedule((Callable<?>) sr, delay, unit);
		}
		sr.setFuture(f);
	}
	catch (RejectedExecutionException ex) {
		sr.dispose();
		//RejectedExecutionException are propagated up
		throw ex;
	}

	return sr;
}
 
Example 10
Source File: BoundedElasticScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
private void ensureQueueCapacity(int taskCount) {
	if (queueCapacity == Integer.MAX_VALUE) {
		return;
	}
	int queueSize = super.getQueue().size();
	if ((queueSize + taskCount) > queueCapacity) {
		throw Exceptions.failWithRejected("Task capacity of bounded elastic scheduler reached while scheduling " + taskCount + " tasks (" + (queueSize + taskCount) + "/" + queueCapacity + ")");
	}
}
 
Example 11
Source File: ExecutorScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	Objects.requireNonNull(task, "task");
	if (terminated) {
		throw Exceptions.failWithRejected();
	}

	ExecutorTrackedRunnable r = new ExecutorTrackedRunnable(task, this, false);
	synchronized (this) {
		if (terminated) {
			throw Exceptions.failWithRejected();
		}
		queue.offer(r);
	}

	if (WIP.getAndIncrement(this) == 0) {
		try {
			executor.execute(this);
		}
		catch (Throwable ex) {
			r.dispose();
			Schedulers.handleError(ex);
			throw Exceptions.failWithRejected(ex);
		}
	}

	return r;
}
 
Example 12
Source File: ImmediateScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
    if (shutdown) {
        throw Exceptions.failWithRejected();
    }
    task.run();
    return FINISHED;
}
 
Example 13
Source File: VirtualTimeScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public Disposable schedulePeriodically(Runnable task,
		long initialDelay,
		long period, TimeUnit unit) {
	if (shutdown) {
		throw Exceptions.failWithRejected();
	}

	PeriodicDirectTask periodicTask = new PeriodicDirectTask(task);

	directWorker.schedulePeriodically(periodicTask, initialDelay, period, unit);

	return periodicTask;
}
 
Example 14
Source File: VirtualTimeScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public Disposable schedule(Runnable task, long delay, TimeUnit unit) {
	if (shutdown) {
		throw Exceptions.failWithRejected();
	}
	return directWorker.schedule(task, delay, unit);
}
 
Example 15
Source File: FluxPublishOnTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	throw Exceptions.failWithRejected();
}
 
Example 16
Source File: FluxPublishOnTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	throw Exceptions.failWithRejected();
}
 
Example 17
Source File: FluxPublishOnTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public Disposable schedule(Runnable task) {
	throw Exceptions.failWithRejected();
}