io.vertx.core.streams.Pump Java Examples

The following examples show how to use io.vertx.core.streams.Pump. 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: VertxDemoApp.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 6 votes vote down vote up
@Override
public void start() {
    LogService logsService = new MockLogService();
    vertx.createHttpServer()
         .requestHandler(request -> {

             ReactiveReadStream<Buffer> rrs = ReactiveReadStream.readStream();
             HttpServerResponse response = request.response();

             Flowable<Buffer> logs = Flowable.fromPublisher(logsService.stream())
                                             .map(Buffer::buffer)
                                             .doOnTerminate(response::end);

             logs.subscribe(rrs);

             response.setStatusCode(200);
             response.setChunked(true);
             response.putHeader("Content-Type", "text/plain");
             response.putHeader("Connection", "keep-alive");

             Pump.pump(rrs, response)
                 .start();
         })
         .listen(8080);
    System.out.println("HTTP server started on port 8080");
}
 
Example #2
Source File: RemoteFileSyncer.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
private void pumpFileFromRequest(
        HttpClientResponse httpClientResponse, AsyncFile asyncFile, Promise<Void> promise) {

    logger.info("Trying to download file from {0}", downloadUrl);
    httpClientResponse.pause();
    final Pump pump = Pump.pump(httpClientResponse, asyncFile);
    pump.start();
    httpClientResponse.resume();

    final long idTimer = setTimeoutTimer(asyncFile, pump, promise);

    httpClientResponse.endHandler(responseEndResult -> handleResponseEnd(asyncFile, idTimer, promise));
}
 
Example #3
Source File: RemoteFileSyncer.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
private void handleTimeout(AsyncFile asyncFile, Pump pump, Promise<Void> promise) {
    pump.stop();
    asyncFile.close();
    if (!promise.future().isComplete()) {
        promise.fail(new TimeoutException("Timeout on download"));
    }
}
 
Example #4
Source File: AsyncISTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testPumpWithBoundedWriteStream() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    vertx = Vertx.vertx();
    final AsyncInputStream in = new AsyncInputStream(
            vertx,
            executor,
            new ByteArrayInputStream(content),
            512);
    final BoundedWriteStream buffer = new BoundedWriteStream(1024);

    vertx.runOnContext(event -> {
        Pump.pump(in, buffer).start();
    });

    while (AsyncInputStream.STATUS_PAUSED != in.getState()) {
        sleep(1);
    }
    byte[] data = buffer.drain();
    assertData(data, 0);

    while (AsyncInputStream.STATUS_PAUSED != in.getState()) {
        sleep(1);
    }
    data = buffer.drain();
    assertData(data, 1024);
    assertEquals(1024, data.length);
    latch.countDown();
    latch.await(30, TimeUnit.SECONDS);
}
 
Example #5
Source File: RemoteFileSyncer.java    From prebid-server-java with Apache License 2.0 4 votes vote down vote up
private long setTimeoutTimer(AsyncFile asyncFile, Pump pump, Promise<Void> promise) {
    return vertx.setTimer(timeout, timerId -> handleTimeout(asyncFile, pump, promise));
}
 
Example #6
Source File: S3Client.java    From vertx-s3-client with Apache License 2.0 4 votes vote down vote up
/**
 * Adaptively upload a file to S3 and take away the burden to choose between direct or multipart upload.
 * Since the minimum size of the multipart part has to be 5MB this method handles the upload automatically.
 * It either chooses between the direct upload if the stream contains less then 5MB or the multipart upload
 * if the stream is bigger then 5MB.
 *
 * @param bucket                The bucket
 * @param key                   The key of the final file
 * @param adaptiveUploadRequest The request
 * @param handler               Success handler
 * @param exceptionHandler      Exception handler
 */
public void adaptiveUpload(String bucket,
                           String key,
                           AdaptiveUploadRequest adaptiveUploadRequest,
                           Handler<Response<CommonResponseHeaders, Void>> handler,
                           Handler<Throwable> exceptionHandler) {
    checkNotNull(StringUtils.trimToNull(bucket), "bucket must not be null");
    checkNotNull(StringUtils.trimToNull(key), "key must not be null");
    checkNotNull(adaptiveUploadRequest, "adaptiveUploadRequest must not be null");
    checkNotNull(handler, "handler must not be null");
    checkNotNull(exceptionHandler, "exceptionHandler must not be null");

    final ChunkedBufferReadStream chunkedBufferReadStream = new ChunkedBufferReadStream(vertx, adaptiveUploadRequest.getReadStream(), FIVE_MB_IN_BYTES);
    chunkedBufferReadStream.exceptionHandler(throwable -> exceptionHandler.handle(throwable));
    chunkedBufferReadStream.setChunkHandler(chunk -> {
        if (chunkedBufferReadStream.numberOfChunks() == 0) {
            if (chunk.length() < FIVE_MB_IN_BYTES) {
                final Buffer buffer = Buffer.buffer();
                chunkedBufferReadStream.handler(buffer::appendBuffer);
                chunkedBufferReadStream.endHandler(aVoid -> {
                    putObject(bucket, key, mapAdaptiveUploadRequestToPutObjectRequest(buffer, adaptiveUploadRequest), event -> handler.handle(new HeaderOnlyResponse(event.getHeader())), exceptionHandler);
                });
                chunkedBufferReadStream.resume();
            } else {
                chunkedBufferReadStream.pause();
                initMultipartUpload(bucket, key, mapAdaptiveUploadRequestToInitMultipartUploadRequest(adaptiveUploadRequest),
                        event -> {
                            try {
                                if(adaptiveUploadRequest.getWriteQueueMaxSize() != null) {
                                    event.getData().setWriteQueueMaxSize(adaptiveUploadRequest.getWriteQueueMaxSize());
                                }
                                if(adaptiveUploadRequest.getBufferSize() != null) {
                                    event.getData().bufferSize(adaptiveUploadRequest.getBufferSize());
                                }
                                event.getData().exceptionHandler(throwable -> exceptionHandler.handle(throwable));
                                Pump.pump(chunkedBufferReadStream, event.getData()).start();
                                chunkedBufferReadStream.endHandler(aVoid -> event.getData().end(endResponse -> handler.handle(new HeaderOnlyResponse(event.getHeader()))));
                                chunkedBufferReadStream.resume();
                            } catch (Throwable t) {
                                exceptionHandler.handle(t);
                            }
                        },
                        exceptionHandler
                );
            }
        }
    });
    chunkedBufferReadStream.resume();
}
 
Example #7
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
public void toReadStream(io.vertx.rxjava.core.Vertx vertx, HttpServerResponse response) {
  Observable<Buffer> observable = getObservable();
  ReadStream<Buffer> readStream = RxHelper.toReadStream(observable);
  Pump pump = Pump.pump(readStream, response);
  pump.start();
}
 
Example #8
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
public void toReadStream(HttpServerResponse response) {
  Flowable<Buffer> observable = getFlowable();
  ReadStream<Buffer> readStream = FlowableHelper.toReadStream(observable);
  Pump pump = Pump.pump(readStream, response);
  pump.start();
}
 
Example #9
Source File: ReactiveStreamsExamples.java    From vertx-reactive-streams with Apache License 2.0 3 votes vote down vote up
public void example1(HttpServerResponse response, Publisher<Buffer> otherPublisher) {

    ReactiveReadStream<Buffer> rrs = ReactiveReadStream.readStream();

    // Subscribe the read stream to the publisher
    otherPublisher.subscribe(rrs);

    // Pump from the read stream to the http response
    Pump pump = Pump.pump(rrs, response);

    pump.start();

  }
 
Example #10
Source File: ReactiveStreamsExamples.java    From vertx-reactive-streams with Apache License 2.0 3 votes vote down vote up
public void example2(Vertx vertx, HttpServerRequest request, Subscriber<Buffer> otherSubscriber) {

    ReactiveWriteStream<Buffer> rws = ReactiveWriteStream.writeStream(vertx);

    // Subscribe the other subscriber to the write stream
    rws.subscribe(otherSubscriber);

    // Pump the http request to the write stream
    Pump pump = Pump.pump(request, rws);

    pump.start();
  }