Java Code Examples for io.reactivex.rxjava3.schedulers.Schedulers#from()

The following examples show how to use io.reactivex.rxjava3.schedulers.Schedulers#from() . 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: ReactiveBatchProcessorV2.java    From code-examples with MIT License 5 votes vote down vote up
private Scheduler threadPoolScheduler(int poolSize, int queueSize) {
  return Schedulers.from(new ThreadPoolExecutor(
      poolSize,
      poolSize,
      0L,
      TimeUnit.SECONDS,
      new LinkedBlockingDeque<>(queueSize)
  ));
}
 
Example 2
Source File: ReactiveBatchProcessorV3.java    From code-examples with MIT License 5 votes vote down vote up
private Scheduler threadPoolScheduler(int poolSize, int queueSize) {
  return Schedulers.from(new ThreadPoolExecutor(
      poolSize,
      poolSize,
      0L,
      TimeUnit.SECONDS,
      new LinkedBlockingDeque<>(queueSize)
  ));
}
 
Example 3
Source File: ReactiveBatchProcessor.java    From code-examples with MIT License 5 votes vote down vote up
private Scheduler threadPoolScheduler(int poolSize, int queueSize) {
  return Schedulers.from(new ThreadPoolExecutor(
      poolSize,
      poolSize,
      0L,
      TimeUnit.SECONDS,
      new LinkedBlockingDeque<>(queueSize),
      new WaitForCapacityPolicy()
  ));
}
 
Example 4
Source File: ReactiveBatchProcessorV1.java    From code-examples with MIT License 5 votes vote down vote up
private Scheduler threadPoolScheduler(int poolSize, int queueSize) {
  return Schedulers.from(new ThreadPoolExecutor(
      poolSize,
      poolSize,
      0L,
      TimeUnit.SECONDS,
      new LinkedBlockingDeque<>(queueSize)
  ));
}
 
Example 5
Source File: MainService.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) {
    final Scheduler contextAwareScheduler = Schedulers.from(ctx.contextAwareExecutor());

    // This logic mimics using a blocking method, which would usually be something like a MySQL
    // database query using JDBC.
    final Flowable<Long> fetchNumsFromFakeDb =
            Single.fromCallable(
                    () -> {
                        // The context is mounted in a thread-local, meaning it is available to all
                        // logic such as tracing.
                        checkState(ServiceRequestContext.current() == ctx);
                        checkState(!ctx.eventLoop().inEventLoop());

                        Uninterruptibles.sleepUninterruptibly(Duration.ofMillis(50));
                        return ImmutableList.of(23L, -23L);
                    })
                  // Always run blocking logic on the blocking task executor. By using
                  // ServiceRequestContext.blockingTaskExecutor, you also ensure the context is mounted
                  // inside the logic (e.g., your DB call will be traced!).
                  .subscribeOn(Schedulers.from(ctx.blockingTaskExecutor()))
                  .flattenAsFlowable(l -> l);

    final Flowable<Long> extractNumsFromRequest =
            Single.fromCompletionStage(req.aggregate())
                           // Unless you know what you're doing, always use subscribeOn with the context
                           // executor to have the context mounted and stay on a single thread to reduce
                           // concurrency issues.
                           .subscribeOn(contextAwareScheduler)
                           .flatMapPublisher(request -> {
                               // The context is mounted in a thread-local, meaning it is available to all
                               // logic such as tracing.
                               checkState(ServiceRequestContext.current() == ctx);
                               checkState(ctx.eventLoop().inEventLoop());

                               final List<Long> nums = new ArrayList<>();
                               for (String token : Iterables.concat(
                                       NUM_SPLITTER.split(request.path().substring(1)),
                                       NUM_SPLITTER.split(request.contentUtf8()))) {
                                   nums.add(Long.parseLong(token));
                               }

                               return Flowable.fromIterable(nums);
                           });

    final Single<HttpResponse> response =
            Flowable.concatArrayEager(extractNumsFromRequest, fetchNumsFromFakeDb)
                    // Unless you know what you're doing, always use subscribeOn with the context executor
                    // to have the context mounted and stay on a single thread to reduce concurrency issues.
                    .subscribeOn(contextAwareScheduler)
                    // When concatenating flowables, you should almost always call observeOn with the
                    // context executor because we don't know here whether the subscription is on it or
                    // something like a blocking task executor.
                    .observeOn(contextAwareScheduler)
                    .flatMapSingle(num -> {
                        // The context is mounted in a thread-local, meaning it is available to all logic
                        // such as tracing.
                        checkState(ServiceRequestContext.current() == ctx);
                        checkState(ctx.eventLoop().inEventLoop());

                        return Single.fromCompletionStage(backendClient.get("/square/" + num).aggregate());
                    })
                    .map(AggregatedHttpResponse::contentUtf8)
                    .collectInto(new StringBuilder(), (current, item) -> current.append(item).append('\n'))
                    .map(content -> HttpResponse.of(content.toString()))
                    .onErrorReturn(HttpResponse::ofFailure);

    return HttpResponse.from(response.toCompletionStage());
}
 
Example 6
Source File: FlowableRxInvokerImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public FlowableRxInvokerImpl(SyncInvoker syncInvoker, ExecutorService ex) {
    this.syncInvoker = syncInvoker;
    this.sc = ex == null ? null : Schedulers.from(ex);
}
 
Example 7
Source File: ObservableRxInvokerImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public ObservableRxInvokerImpl(SyncInvoker syncInvoker, ExecutorService ex) {
    this.syncInvoker = syncInvoker;
    this.sc = ex == null ? null : Schedulers.from(ex);
}