Java Code Examples for reactor.core.publisher.Flux#using()

The following examples show how to use reactor.core.publisher.Flux#using() . 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: DataBufferUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Obtain a {@code AsynchronousFileChannel} from the given supplier, and
 * read it into a {@code Flux} of {@code DataBuffer}s, starting at the given
 * position. Closes the channel when the Flux is terminated.
 * @param channelSupplier the supplier for the channel to read from
 * @param position the position to start reading from
 * @param bufferFactory the factory to create data buffers with
 * @param bufferSize the maximum size of the data buffers
 * @return a Flux of data buffers read from the given channel
 */
public static Flux<DataBuffer> readAsynchronousFileChannel(
		Callable<AsynchronousFileChannel> channelSupplier, long position,
		DataBufferFactory bufferFactory, int bufferSize) {

	Assert.notNull(channelSupplier, "'channelSupplier' must not be null");
	Assert.notNull(bufferFactory, "'dataBufferFactory' must not be null");
	Assert.isTrue(position >= 0, "'position' must be >= 0");
	Assert.isTrue(bufferSize > 0, "'bufferSize' must be > 0");

	Flux<DataBuffer> flux = Flux.using(channelSupplier,
			channel -> Flux.create(sink -> {
				ReadCompletionHandler handler =
						new ReadCompletionHandler(channel, sink, position, bufferFactory, bufferSize);
				sink.onDispose(handler::dispose);
				DataBuffer dataBuffer = bufferFactory.allocateBuffer(bufferSize);
				ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, bufferSize);
				channel.read(byteBuffer, position, dataBuffer, handler);
			}),
			channel -> {
				// Do not close channel from here, rather wait for the current read callback
				// and then complete after releasing the DataBuffer.
			});

	return flux.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}
 
Example 2
Source File: DataBufferUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Obtain a {@code AsynchronousFileChannel} from the given supplier, and read it into a
 * {@code Flux} of {@code DataBuffer}s, starting at the given position. Closes the
 * channel when the flux is terminated.
 * @param channelSupplier the supplier for the channel to read from
 * @param position the position to start reading from
 * @param dataBufferFactory the factory to create data buffers with
 * @param bufferSize the maximum size of the data buffers
 * @return a flux of data buffers read from the given channel
 */
public static Flux<DataBuffer> readAsynchronousFileChannel(Callable<AsynchronousFileChannel> channelSupplier,
		long position, DataBufferFactory dataBufferFactory, int bufferSize) {

	Assert.notNull(channelSupplier, "'channelSupplier' must not be null");
	Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null");
	Assert.isTrue(position >= 0, "'position' must be >= 0");
	Assert.isTrue(bufferSize > 0, "'bufferSize' must be > 0");

	DataBuffer dataBuffer = dataBufferFactory.allocateBuffer(bufferSize);
	ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, bufferSize);

	Flux<DataBuffer> result = Flux.using(channelSupplier,
			channel -> Flux.create(sink -> {
				AsynchronousFileChannelReadCompletionHandler completionHandler =
						new AsynchronousFileChannelReadCompletionHandler(channel,
								sink, position, dataBufferFactory, bufferSize);
				channel.read(byteBuffer, position, dataBuffer, completionHandler);
				sink.onDispose(completionHandler::dispose);
			}),
			DataBufferUtils::closeChannel);

	return result.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}
 
Example 3
Source File: DataBufferUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Obtain a {@link ReadableByteChannel} from the given supplier, and read
 * it into a {@code Flux} of {@code DataBuffer}s. Closes the channel when
 * the Flux is terminated.
 * @param channelSupplier the supplier for the channel to read from
 * @param bufferFactory the factory to create data buffers with
 * @param bufferSize the maximum size of the data buffers
 * @return a Flux of data buffers read from the given channel
 */
public static Flux<DataBuffer> readByteChannel(
		Callable<ReadableByteChannel> channelSupplier, DataBufferFactory bufferFactory, int bufferSize) {

	Assert.notNull(channelSupplier, "'channelSupplier' must not be null");
	Assert.notNull(bufferFactory, "'dataBufferFactory' must not be null");
	Assert.isTrue(bufferSize > 0, "'bufferSize' must be > 0");

	return Flux.using(channelSupplier,
			channel -> Flux.generate(new ReadableByteChannelGenerator(channel, bufferFactory, bufferSize)),
			DataBufferUtils::closeChannel);

	// No doOnDiscard as operators used do not cache
}
 
Example 4
Source File: ReactorEssentialsTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@Test
public void usingOperator() {
    Flux<String> ioRequestResults = Flux.using(
        Connection::newConnection,
        connection -> Flux.fromIterable(connection.getData()),
        Connection::close
    );

    ioRequestResults
        .subscribe(
            data -> log.info("Received data: {}", data),
            e -> log.info("Error: {}", e.getMessage()),
            () -> log.info("Stream finished"));
}
 
Example 5
Source File: GuideTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void errorHandlingUsing() {
	AtomicBoolean isDisposed = new AtomicBoolean();
	Disposable disposableInstance = new Disposable() {
		@Override
		public void dispose() {
			isDisposed.set(true); // <4>
		}

		@Override
		public String toString() {
			return "DISPOSABLE";
		}
	};

	Flux<String> flux =
	Flux.using(
			() -> disposableInstance, // <1>
			disposable -> Flux.just(disposable.toString()), // <2>
			Disposable::dispose // <3>
	);

	StepVerifier.create(flux)
                .expectNext("DISPOSABLE")
                .verifyComplete();

	assertThat(isDisposed.get()).isTrue();
}