rx.Scheduler.Worker Java Examples

The following examples show how to use rx.Scheduler.Worker. 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: StaticServerPoller.java    From mantis with Apache License 2.0 6 votes vote down vote up
private Worker schedulePolling(final Subscriber<? super Set<ServerInfo>> subscriber) {
    final Worker worker = this.scheduler.createWorker();
    worker.schedulePeriodically(
            new Action0() {
                @Override
                public void call() {
                    if (subscriber.isUnsubscribed()) {
                        worker.unsubscribe();
                    } else {
                        subscriber.onNext(servers);
                    }
                }
            },
            0,
            this.periodSeconds,
            TimeUnit.SECONDS
    );

    return worker;
}
 
Example #2
Source File: RequestProcessor.java    From mantis with Apache License 2.0 6 votes vote down vote up
public Observable<Void> sendInfiniteStream(final HttpServerResponse<ByteBuf> response) {
    response.getHeaders().add(HttpHeaders.Names.CONTENT_TYPE, "text/event-stream");
    response.getHeaders().add(HttpHeaders.Names.TRANSFER_ENCODING, "chunked");

    return Observable.create(new OnSubscribe<Void>() {
        final AtomicLong counter = new AtomicLong();
        Worker worker = Schedulers.computation().createWorker();

        public void call(Subscriber<? super Void> subscriber) {
            worker.schedulePeriodically(
                    new Action0() {
                        @Override
                        public void call() {
                            System.out.println("In infinte stream");
                            byte[] contentBytes = ("data:" + "line " + counter.getAndIncrement() + "\n\n").getBytes();
                            response.writeBytes(contentBytes);
                            response.flush();
                        }
                    },
                    0,
                    100,
                    TimeUnit.MILLISECONDS
            );
        }
    });
}
 
Example #3
Source File: ListenableFutureObservable.java    From RxJavaGuava with Apache License 2.0 6 votes vote down vote up
/**
 * Converts from {@link ListenableFuture} to {@link rx.Observable}.
 * 
 * @param future  the {@link ListenableFuture} to register a listener on.
 * @param scheduler  the {@link Scheduler} where the callback will be executed.  The will be where the {@link Observer#onNext(Object)} call from.
 * @return an {@link Observable} that emits the one value when the future completes.
 */
public static <T> Observable<T> from(final ListenableFuture<T> future, final Scheduler scheduler) {
    final Worker worker = scheduler.createWorker();
    return from(future, new Executor() {
        @Override
        public void execute(final Runnable command) {
            worker.schedule(new Action0() {
                @Override
                public void call() {
                    try {
                        command.run();
                    } finally {
                        worker.unsubscribe();
                    }
                }
            });
        }
    });
}
 
Example #4
Source File: ObsTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testCachedScheduledReset() {
    TestScheduler scheduler = new TestScheduler();
    Worker worker = scheduler.createWorker();
    try {
        final AtomicInteger count = new AtomicInteger(0);
        Observable<Integer> source = Observable.defer(new Func0<Observable<Integer>>() {
            @Override
            public Observable<Integer> call() {
                return Observable.just(count.incrementAndGet());
            }
        })
                // cache
                .compose(Transformers.<Integer> cache(5, TimeUnit.MINUTES, worker));
        assertEquals(1, (int) source.toBlocking().single());
        scheduler.advanceTimeBy(1, TimeUnit.MINUTES);
        assertEquals(1, (int) source.toBlocking().single());
        scheduler.advanceTimeBy(1, TimeUnit.MINUTES);
        assertEquals(1, (int) source.toBlocking().single());
        scheduler.advanceTimeBy(3, TimeUnit.MINUTES);
        assertEquals(2, (int) source.toBlocking().single());
        assertEquals(2, (int) source.toBlocking().single());
    } finally {
        worker.unsubscribe();
    }
}
 
Example #5
Source File: Schedulers.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
public static void blockUntilWorkFinished(Scheduler scheduler, int numThreads, long timeout,
        TimeUnit unit) {
    final CountDownLatch latch = new CountDownLatch(numThreads);
    for (int i = 1; i <= numThreads; i++) {
        final Worker worker = scheduler.createWorker();
        worker.schedule(new Action0() {

            @Override
            public void call() {
                worker.unsubscribe();
                latch.countDown();
            }
        });
    }
    try {
        boolean finished = latch.await(timeout, unit);
        if (!finished) {
            throw new RuntimeException("timeout occured waiting for work to finish");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
 
Example #6
Source File: SubscriptionTimeout.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
public Subscriber<? super T> call(Subscriber<? super T> downstream) {
    TimeoutSubscriber<T> upstream = new TimeoutSubscriber<T>(downstream, timeout.get(), unit);
    downstream.add(upstream);
    downstream.setProducer(upstream.arbiter);

    // prevent all races and serialize onNext, onError, and onCompleted calls
    final Worker worker = scheduler.createWorker();
    final SerializedSubscriber<T> safeUpstream = new SerializedSubscriber<>(upstream, true);
    upstream.add(worker);

    Subscription task = worker.schedule(() -> safeUpstream.onError(new TimeoutException()), timeout.get(), unit);
    upstream.add(task);

    return safeUpstream;
}
 
Example #7
Source File: Obs.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
private static <T> void startScheduledResetAgain(final long duration, final TimeUnit unit,
        final Scheduler scheduler, final AtomicReference<CachedObservable<T>> cacheRef,
        final AtomicReference<Optional<Worker>> workerRef) {

    Action0 action = new Action0() {
        @Override
        public void call() {
            cacheRef.get().reset();
        }
    };
    // CAS loop to cancel the current worker and create a new one
    while (true) {
        Optional<Worker> wOld = workerRef.get();
        if (wOld == null) {
            // we are finished
            return;
        }
        Optional<Worker> w = Optional.of(scheduler.createWorker());
        if (workerRef.compareAndSet(wOld, w)) {
            if (wOld.isPresent())
                wOld.get().unsubscribe();
            w.get().schedule(action, duration, unit);
            break;
        }
    }
}
 
Example #8
Source File: PublishSubjectRaceTest.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Test
public void nonRacy() throws Exception {
    Worker worker = Schedulers.computation().createWorker();
    try {
        for (int i = 0; i < 1000; i++) {
            AtomicInteger wip = new AtomicInteger(2);
    
            Subject<Integer, Integer> ps = PublishSubject.<Integer>create().toSerialized();
            
            AssertableSubscriber<Integer> as = ps.test(1);
            
            CountDownLatch cdl = new CountDownLatch(1);
            
            worker.schedule(() -> {
                if (wip.decrementAndGet() != 0) {
                    while (wip.get() != 0) ;
                }
                ps.onNext(1);
                
                cdl.countDown();
            });
            if (wip.decrementAndGet() != 0) {
                while (wip.get() != 0) ;
            }
            ps.onNext(1);
            
            cdl.await();
            
            as.assertFailure(MissingBackpressureException.class, 1);
        }
    } finally {
        worker.unsubscribe();
    }
}
 
Example #9
Source File: MockResponse.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param id
 *            ID from client used to assert correct client/server interaction
 * @param delay
 *            How long to delay delivery to simulate server-side latency
 * @param itemSize
 *            Length of each item String.
 * @param numItems
 *            Number of items in response.
 * 
 * @return String json
 */
public static Observable<String> generateJson(long id, int delay, int itemSize, int numItems) {
    return Observable.create((Subscriber<? super String> subscriber) -> {
        Worker worker = Schedulers.computation().createWorker();
        subscriber.add(worker);
        worker.schedule(() -> {
            try {
                StringWriter jsonString = new StringWriter();
                JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);

                json.writeStartObject();

                // manipulate the ID such that we can know the response is from the server (client will know the logic)
                long responseKey = getResponseKey(id);

                json.writeNumberField("responseKey", responseKey);

                json.writeNumberField("delay", delay);
                if (itemSize > MAX_ITEM_LENGTH) {
                    throw new IllegalArgumentException("itemSize can not be larger than: " + MAX_ITEM_LENGTH);
                }
                json.writeNumberField("itemSize", itemSize);
                json.writeNumberField("numItems", numItems);

                json.writeArrayFieldStart("items");
                for (int i = 0; i < numItems; i++) {
                    json.writeString(RAW_ITEM_LONG.substring(0, itemSize));
                }
                json.writeEndArray();
                json.writeEndObject();
                json.close();

                subscriber.onNext(jsonString.toString());
                subscriber.onCompleted();
            } catch (Exception e) {
                subscriber.onError(e);
            }
        }, delay, TimeUnit.MILLISECONDS);
    });
}
 
Example #10
Source File: ResultSetToRowsTransformer.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
void execute(Runnable command) {
    Worker worker = scheduler.createWorker();
    worker.schedule(() -> {
        try {
            command.run();
        } finally {
            worker.unsubscribe();
        }
    });
}
 
Example #11
Source File: OnSubscribeWatchServiceEvents.java    From rxjava-file with Apache License 2.0 5 votes vote down vote up
@Override
public void call(final Subscriber<? super WatchEvent<?>> subscriber) {
    final Worker worker = scheduler.createWorker();
    subscriber.add(worker);
    subscriber.add(createSubscriptionToCloseWatchService(watchService));
    worker.schedule(new Action0() {
        @Override
        public void call() {
            if (emitEvents(watchService, subscriber, pollDurationMs, pollIntervalMs)) {
                worker.schedule(this);
            }
        }
    }, pollIntervalMs, TimeUnit.MILLISECONDS);
}
 
Example #12
Source File: OperatorBufferToFileTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
private static void waitUntilWorkCompleted(Scheduler scheduler, long duration, TimeUnit unit) {
    final CountDownLatch latch = new CountDownLatch(1);
    Worker worker = scheduler.createWorker();
    worker.schedule(Actions.countDown(latch));
    worker.schedule(Actions.unsubscribe(worker));
    try {
        if (!worker.isUnsubscribed() && !latch.await(duration, unit)) {
            throw new RuntimeException("did not complete");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
 
Example #13
Source File: OperatorBufferToFile.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
QueueProducer(QueueWithSubscription<T> queue, Subscriber<? super T> child, Worker worker,
        boolean delayError) {
    super();
    this.queue = queue;
    this.child = child;
    this.worker = worker;
    this.delayError = delayError;
    this.done = false;
}
 
Example #14
Source File: OperatorBufferToFile.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
OnSubscribeFromQueue(AtomicReference<QueueProducer<T>> queueProducer,
        QueueWithSubscription<T> queue, Worker worker, Options options) {
    this.queueProducer = queueProducer;
    this.queue = queue;
    this.worker = worker;
    this.options = options;
}
 
Example #15
Source File: OperatorBufferToFile.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Override
public Subscriber<? super T> call(Subscriber<? super T> child) {

    // create the file based queue
    final QueueWithSubscription<T> queue = createFileBasedQueue(dataSerializer, options);

    // hold a reference to the queueProducer which will be set on
    // subscription to `source`
    final AtomicReference<QueueProducer<T>> queueProducer = new AtomicReference<QueueProducer<T>>();

    // emissions will propagate to downstream via this worker
    final Worker worker = scheduler.createWorker();

    // set up the observable to read from the file based queue
    Observable<T> source = Observable
            .create(new OnSubscribeFromQueue<T>(queueProducer, queue, worker, options));

    // create the parent subscriber
    Subscriber<T> parentSubscriber = new ParentSubscriber<T>(queueProducer);

    // link unsubscription
    child.add(parentSubscriber);

    // close and delete file based queues in RollingQueue on unsubscription
    child.add(queue);

    // ensure onStart not called twice
    Subscriber<T> wrappedChild = Subscribers.wrap(child);

    // ensure worker gets unsubscribed (last)
    child.add(worker);

    // subscribe to queue
    source.unsafeSubscribe(wrappedChild);

    return parentSubscriber;
}
 
Example #16
Source File: Transformers.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
public static <T> Transformer<T, T> cache(final long duration, final TimeUnit unit,
        final Worker worker) {
    return new Transformer<T, T>() {
        @Override
        public Observable<T> call(Observable<T> o) {
            return Obs.cache(o, duration, unit, worker);
        }
    };
}
 
Example #17
Source File: PublishSubjectRaceTest.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Test
public void racy() throws Exception {
    Worker worker = Schedulers.computation().createWorker();
    try {
        for (int i = 0; i < 1000; i++) {
            AtomicInteger wip = new AtomicInteger(2);
    
            PublishSubject<Integer> ps = PublishSubject.create();
            
            AssertableSubscriber<Integer> as = ps.test(1);
            
            CountDownLatch cdl = new CountDownLatch(1);
            
            worker.schedule(() -> {
                if (wip.decrementAndGet() != 0) {
                    while (wip.get() != 0) ;
                }
                ps.onNext(1);
                
                cdl.countDown();
            });
            if (wip.decrementAndGet() != 0) {
                while (wip.get() != 0) ;
            }
            ps.onNext(1);
            
            cdl.await();
            
            as.assertFailure(MissingBackpressureException.class, 1);
        }
    } finally {
        worker.unsubscribe();
    }
}
 
Example #18
Source File: RxJavaComputationSchedulerSensor.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
WorkerTaskScheduler(Thread workerThread, Worker worker) {
    this.worker = worker;
    this.workerThread = workerThread;
    this.metrics = new HiccupSensorMetrics("titus.hiccup.computationScheduler", singletonMap("thread", workerThread.getName()), registry);
    this.recorder = new SingleWriterRecorder(LOWEST_TRACKABLE_VALUE, HIGHEST_TRACKABLE_VALUE, NUMBER_OF_SIGNIFICANT_DIGITS);

    registry.gauge(
            registry.createId("titus.hiccup.computationScheduler.pendingTaskDuration", singletonMap("thread", workerThread.getName())),
            0, v -> getPendingTaskRunningTime()
    );

    doRun();
}
 
Example #19
Source File: RxJavaComputationSchedulerSensor.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private List<WorkerTaskScheduler> findComputationSchedulerWorkers() {
    List<WorkerTaskScheduler> workerTaskSchedulers = new ArrayList<>();
    try {
        int cpuCount = Runtime.getRuntime().availableProcessors();
        Set<Thread> threads = new HashSet<>();
        int iterCount = 0;
        while (workerTaskSchedulers.size() < cpuCount && iterCount < MAX_LOOP_COUNT) {
            iterCount++;

            Worker worker = Schedulers.computation().createWorker();
            AtomicReference<Thread> workerThreadRef = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            worker.schedule(() -> {
                workerThreadRef.set(Thread.currentThread());
                latch.countDown();
            });
            if (!latch.await(MAX_INIT_PROCESSING_WAIT_TIME_MS, TimeUnit.MILLISECONDS)) {
                logger.warn("RxJava computation scheduler sensor initialization error");
                return Collections.emptyList();
            }
            if (threads.add(workerThreadRef.get())) {
                workerTaskSchedulers.add(new WorkerTaskScheduler(workerThreadRef.get(), worker));
            } else {
                worker.unsubscribe();
            }
        }
        return workerTaskSchedulers;
    } catch (Exception e) {
        logger.warn("RxJava computation scheduler sensor initialization error", e);
        workerTaskSchedulers.forEach(WorkerTaskScheduler::shutdown);
        return Collections.emptyList();
    }
}
 
Example #20
Source File: OperatorResumeOnError.java    From mantis with Apache License 2.0 4 votes vote down vote up
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
    final SerialSubscription serialSubscription = new SerialSubscription();
    child.add(serialSubscription);

    return new Subscriber<T>(child) {
        private final Worker worker = scheduler.createWorker();

        @Override
        public void onCompleted() {
            child.onCompleted();
        }

        @Override
        public void onError(final Throwable e) {
            worker.schedule(new Action0() {
                @Override
                public void call() {
                    try {
                        int newAttempts = currentAttempts + 1;
                        Observable<? extends T> resume = resumePolicy.call(newAttempts, e);

                        if (resume == null) {
                            child.onError(e);
                        } else {
                            resume = resume.lift(new OperatorResumeOnError<>(
                                    newAttempts,
                                    resumePolicy
                            ));

                            serialSubscription.set(resume.unsafeSubscribe(child));
                        }
                    } catch (Throwable e2) {
                        child.onError(e2);
                    }
                }
            });
        }

        @Override
        public void onNext(T t) {
            child.onNext(t);
        }

    };
}
 
Example #21
Source File: LooperScheduler.java    From letv with Apache License 2.0 4 votes vote down vote up
public Worker createWorker() {
    return new HandlerWorker(this.handler);
}
 
Example #22
Source File: HandlerScheduler.java    From letv with Apache License 2.0 4 votes vote down vote up
public /* bridge */ /* synthetic */ Worker createWorker() {
    return super.createWorker();
}
 
Example #23
Source File: TrampolineScheduler.java    From letv with Apache License 2.0 4 votes vote down vote up
public Worker createWorker() {
    return null;
}
 
Example #24
Source File: NewThreadScheduler.java    From letv with Apache License 2.0 4 votes vote down vote up
public Worker createWorker() {
    return null;
}
 
Example #25
Source File: ImmediateScheduler.java    From letv with Apache License 2.0 4 votes vote down vote up
public Worker createWorker() {
    return null;
}
 
Example #26
Source File: OperatorResumeOnCompleted.java    From mantis with Apache License 2.0 4 votes vote down vote up
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
    final SerialSubscription serialSubscription = new SerialSubscription();
    child.add(serialSubscription);

    return new Subscriber<T>(child) {
        private final Worker worker = scheduler.createWorker();

        @Override
        public void onCompleted() {
            worker.schedule(new Action0() {
                @Override
                public void call() {
                    try {
                        int newAttempts = currentAttempts + 1;

                        Observable<? extends T> resume = resumePolicy.call(newAttempts);
                        if (resume == null) {
                            child.onCompleted();
                        } else {
                            resume = resume.lift(new OperatorResumeOnCompleted<>(
                                    newAttempts,
                                    resumePolicy
                            ));

                            serialSubscription.set(resume.unsafeSubscribe(child));
                        }
                    } catch (Throwable e2) {
                        child.onError(e2);
                    }
                }
            });
        }

        @Override
        public void onError(final Throwable e) {
            child.onError(e);
        }

        @Override
        public void onNext(T t) {
            child.onNext(t);
        }
    };
}
 
Example #27
Source File: MockResponse.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param id
 *            ID from client used to assert correct client/server interaction
 * @param delay
 *            How long to delay delivery to simulate server-side latency
 * @param itemSize
 *            Length of each item String.
 * @param numItems
 *            Number of items in response.
 * 
 * @return String json
 */
public static Observable<ByteBuf> generateJson(long id, int delay, int itemSize, int numItems) {
    return Observable.create((Subscriber<? super ByteBuf> subscriber) -> {
        Worker worker = Schedulers.computation().createWorker();
        subscriber.add(worker);
        worker.schedule(() -> {
            try {
                ByteBuf buffer = Unpooled.buffer();
                ByteBufOutputStream jsonAsBytes = new ByteBufOutputStream(buffer);
                JsonGenerator json = jsonFactory.createJsonGenerator(jsonAsBytes);

                json.writeStartObject();

                // manipulate the ID such that we can know the response is from the server (client will know the logic)
                long responseKey = getResponseKey(id);

                json.writeNumberField("responseKey", responseKey);

                json.writeNumberField("delay", delay);
                if (itemSize > MAX_ITEM_LENGTH) {
                    throw new IllegalArgumentException("itemSize can not be larger than: " + MAX_ITEM_LENGTH);
                }
                json.writeNumberField("itemSize", itemSize);
                json.writeNumberField("numItems", numItems);

                json.writeArrayFieldStart("items");
                for (int i = 0; i < numItems; i++) {
                    json.writeString(RAW_ITEM_LONG.substring(0, itemSize));
                }
                json.writeEndArray();
                json.writeEndObject();
                json.close();

                subscriber.onNext(buffer);
                subscriber.onCompleted();
            } catch (Exception e) {
                subscriber.onError(e);
            }
        }, delay, TimeUnit.MILLISECONDS);
    });
}