本文整理汇总了Java中reactor.core.Disposable类的典型用法代码示例。如果您正苦于以下问题:Java Disposable类的具体用法?Java Disposable怎么用?Java Disposable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Disposable类属于reactor.core包,在下文中一共展示了Disposable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestPressure
import reactor.core.Disposable; //导入依赖的package包/类
@Override
public Mono<NumberProto.Number> requestPressure(Flux<NumberProto.Number> request) {
if (explicitCancel.get()) {
// Process a very long sequence
Disposable subscription = request.subscribe(n -> System.out.println("S: " + n.getNumber(0)));
return Mono
.just(protoNum(-1))
.delayElement(Duration.ofMillis(250))
// Explicitly cancel by disposing the subscription
.doOnSuccess(x -> subscription.dispose());
} else {
// Process some of a very long sequence and cancel implicitly with a take(10)
return request.map(req -> req.getNumber(0))
.doOnNext(System.out::println)
.take(10)
.last(-1)
.map(CancellationPropagationIntegrationTest::protoNum);
}
}
示例2: main
import reactor.core.Disposable; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
Flux<String> flux = client.get() //
.uri("/sse/messages") //
.retrieve() //
.bodyToFlux(String.class) //
.doOnNext(System.out::println);
System.out.println("Subscribing to SSE");
Disposable subscription = flux.subscribe();
System.out.println("Press any key to terminate subscription...");
System.in.read();
subscription.dispose();
}
示例3: clientCanCancelServerStreamExplicitly
import reactor.core.Disposable; //导入依赖的package包/类
@Test
public void clientCanCancelServerStreamExplicitly() throws InterruptedException {
AtomicInteger lastNumberConsumed = new AtomicInteger(Integer.MAX_VALUE);
ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(channel);
Flux<NumberProto.Number> test = stub
.responsePressure(Mono.just(Empty.getDefaultInstance()))
.doOnNext(number -> {lastNumberConsumed.set(number.getNumber(0)); System.out.println("C: " + number.getNumber(0));})
.doOnError(throwable -> System.out.println(throwable.getMessage()))
.doOnComplete(() -> System.out.println("Completed"))
.doOnCancel(() -> System.out.println("Client canceled"));
Disposable subscription = test.publish().connect();
Thread.sleep(1000);
subscription.dispose();
Thread.sleep(1000);
// Cancellation may or may not deliver the last generated message due to delays in the gRPC processing thread
assertThat(Math.abs(lastNumberConsumed.get() - svc.getLastNumberProduced())).isLessThanOrEqualTo(3);
assertThat(svc.wasCanceled()).isTrue();
}
示例4: clientCanCancelServerStreamImplicitly
import reactor.core.Disposable; //导入依赖的package包/类
@Test
public void clientCanCancelServerStreamImplicitly() throws InterruptedException {
ReactorNumbersGrpc.ReactorNumbersStub stub = ReactorNumbersGrpc.newReactorStub(channel);
Flux<NumberProto.Number> test = stub
.responsePressure(Mono.just(Empty.getDefaultInstance()))
.doOnNext(number -> System.out.println(number.getNumber(0)))
.doOnError(throwable -> System.out.println(throwable.getMessage()))
.doOnComplete(() -> System.out.println("Completed"))
.doOnCancel(() -> System.out.println("Client canceled"))
.take(10);
Disposable subscription = test.publish().connect();
Thread.sleep(1000);
assertThat(svc.wasCanceled()).isTrue();
}
示例5: coordinatorReachableThroughCacheInnerSubscriptionsOnly
import reactor.core.Disposable; //导入依赖的package包/类
@Test
public void coordinatorReachableThroughCacheInnerSubscriptionsOnly() throws InterruptedException {
TestPublisher<Integer> source = TestPublisher.create();
MonoCacheTime<Integer> cached = new MonoCacheTime<>(source.mono(),
Duration.ofMillis(100), //short cache TTL should trigger state change if source is not never
Schedulers.parallel());
Disposable d1 = cached.subscribe();
cached.subscribe();
WeakReference<Signal<Integer>> refCoordinator = new WeakReference<>(cached.state);
assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);
Thread.sleep(150);
source = null;
cached = null;
System.gc();
assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);
}
示例6: onCancel
import reactor.core.Disposable; //导入依赖的package包/类
@Override
public MonoSink<T> onCancel(Disposable d) {
Objects.requireNonNull(d, "onCancel");
SinkDisposable sd = new SinkDisposable(null, d);
if (!DISPOSABLE.compareAndSet(this, null, sd)) {
Disposable c = disposable;
if (c instanceof SinkDisposable) {
SinkDisposable current = (SinkDisposable) c;
if (current.onCancel == null) {
current.onCancel = d;
}
else {
d.dispose();
}
}
}
return this;
}
示例7: set
import reactor.core.Disposable; //导入依赖的package包/类
/**
* Atomically push the field to a {@link Disposable} and dispose the old content.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the new Disposable to push
* @return true if successful, false if the field contains the {@link #DISPOSED} instance.
*/
public static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) {
for (;;) {
Disposable current = updater.get(holder);
if (current == DISPOSED) {
if (newValue != null) {
newValue.dispose();
}
return false;
}
if (updater.compareAndSet(holder, current, newValue)) {
if (current != null) {
current.dispose();
}
return true;
}
}
}
示例8: request
import reactor.core.Disposable; //导入依赖的package包/类
@Override
public void request(long n) {
if (Operators.validate(n)) {
if (ONCE.compareAndSet(this, 0, 1)) {
try {
Disposable f = scheduler.schedule(this);
if (!FUTURE.compareAndSet(this,
null,
f) && future != FINISHED && future != OperatorDisposables.DISPOSED) {
f.dispose();
}
}
catch (RejectedExecutionException ree) {
if (future != FINISHED && future != OperatorDisposables.DISPOSED) {
actual.onError(Operators.onRejectedExecution(ree,
this,
null,
value, actual.currentContext()));
}
}
}
}
}
示例9: coordinatorCacheInnerDisposedOrNoReferenceNoLeak
import reactor.core.Disposable; //导入依赖的package包/类
@Test
public void coordinatorCacheInnerDisposedOrNoReferenceNoLeak() throws InterruptedException {
TestPublisher<Integer> source = TestPublisher.create();
MonoCacheTime<Integer> cached = new MonoCacheTime<>(source.mono(),
Duration.ofMillis(100), //short cache TTL should trigger state change if source is not never
Schedulers.parallel());
Disposable d1 = cached.subscribe();
cached.subscribe();
WeakReference<Signal<Integer>> refCoordinator = new WeakReference<>(cached.state);
assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);
Thread.sleep(150);
source = null;
cached = null;
d1.dispose();
System.gc();
assertThat(refCoordinator.get()).isNull();
}
示例10: subscribersComeAndGoBelowThreshold
import reactor.core.Disposable; //导入依赖的package包/类
@Test
public void subscribersComeAndGoBelowThreshold() {
Flux<Integer> p = Flux.range(1, 5).publish().refCount(2);
Disposable r = p.subscribe();
r.dispose();
p.subscribe().dispose();
p.subscribe().dispose();
p.subscribe().dispose();
p.subscribe().dispose();
AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
p.subscribe(ts1);
ts1.assertValueCount(0);
AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
p.subscribe(ts2);
ts1.assertValues(1, 2, 3, 4, 5);
ts2.assertValues(1, 2, 3, 4, 5);
}
示例11: workerSchedulePeriodically
import reactor.core.Disposable; //导入依赖的package包/类
static Disposable workerSchedulePeriodically(ScheduledExecutorService exec,
Disposable.Composite tasks,
Runnable task,
long initialDelay,
long period,
TimeUnit unit) {
PeriodicWorkerTask sr = new PeriodicWorkerTask(task, tasks);
if (!tasks.add(sr)) {
throw Exceptions.failWithRejected();
}
try {
Future<?> f = exec.scheduleAtFixedRate(sr, initialDelay, period, unit);
sr.setFuture(f);
}
catch (RejectedExecutionException ex) {
sr.dispose();
//RejectedExecutionException are propagated up
throw ex;
}
return sr;
}
示例12: subscribersComeAndGoBelowThreshold
import reactor.core.Disposable; //导入依赖的package包/类
@Test
public void subscribersComeAndGoBelowThreshold() {
Flux<Integer> p = Flux.range(1, 5).publish().refCount(2, Duration.ofMillis(500));
Disposable r = p.subscribe();
r.dispose();
p.subscribe().dispose();
p.subscribe().dispose();
p.subscribe().dispose();
p.subscribe().dispose();
AssertSubscriber<Integer> ts1 = AssertSubscriber.create();
p.subscribe(ts1);
ts1.assertValueCount(0);
AssertSubscriber<Integer> ts2 = AssertSubscriber.create();
p.subscribe(ts2);
ts1.assertValues(1, 2, 3, 4, 5);
ts2.assertValues(1, 2, 3, 4, 5);
}
示例13: schedule
import reactor.core.Disposable; //导入依赖的package包/类
@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) {
terminated = true;
Schedulers.handleError(ex);
throw Exceptions.failWithRejected(ex);
}
return r;
}
示例14: createWorker
import reactor.core.Disposable; //导入依赖的package包/类
@Override public Worker createWorker() {
return new Worker() {
private final List<Runnable> workerTasks = new ArrayList<>();
@Override public Disposable schedule(Runnable task) {
workerTasks.add(task);
tasks.add(task);
return () -> tasks.remove(task);
}
@Override public void dispose() {
tasks.removeAll(workerTasks);
}
};
}
示例15: cancelReceiver
import reactor.core.Disposable; //导入依赖的package包/类
final boolean cancelReceiver() {
Disposable c = receiverCancel;
if (c != CANCELLED) {
c = CANCEL.getAndSet(this, CANCELLED);
if (c != CANCELLED) {
c.dispose();
return true;
}
}
return false;
}