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

The following examples show how to use reactor.core.publisher.Mono#using() . These examples are extracted from open source projects. 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
@Override
public Mono<Task.Result> executeTask(TaskWithId taskWithId) {
    if (!cancelledTasks.remove(taskWithId.getId())) {
        Mono<Task.Result> taskMono = Mono.fromCallable(() -> runWithMdc(taskWithId, listener)).subscribeOn(taskExecutor);
        CompletableFuture<Task.Result> future = taskMono.toFuture();
        runningTask.set(Tuples.of(taskWithId.getId(), future));

        return Mono.using(
            () -> pollAdditionalInformation(taskWithId).subscribe(),
            ignored -> Mono.fromFuture(future)
                .onErrorResume(exception -> Mono.from(handleExecutionError(taskWithId, listener, exception))
                        .thenReturn(Task.Result.PARTIAL)),
            Disposable::dispose);
    } else {
        return Mono.from(listener.cancelled(taskWithId.getId(), taskWithId.getTask().details()))
            .then(Mono.empty());
    }
}
 
Example 2
Source Project: sdn-rx   File: MovieRepository.java    License: Apache License 2.0 5 votes vote down vote up
public Mono<Movie> findByTitle(String title) {
	return Mono.using(driver::rxSession,
		session -> Mono.from(
			session.run("MATCH (m:Movie {title: $title}) RETURN m", Values.parameters("title", title))
				.records()).map(MovieRepository::mapToMovie),
		RxSession::close);
}
 
Example 3
Source Project: sdn-rx   File: MovieRepository.java    License: Apache License 2.0 5 votes vote down vote up
public Mono<Movie> save(Movie movie) {
	return Mono.using(driver::rxSession,
		session -> Mono.from(
			session.run("CREATE (m:Movie {title: $title, tagline: $tagline}) RETURN m",
				Values.parameters("title", movie.getTitle(), "tagline", movie.getTagline()))
				.records()).map(MovieRepository::mapToMovie),
		RxSession::close);
}
 
Example 4
Source Project: reactor-netty   File: NettyOutboundTest.java    License: Apache License 2.0 5 votes vote down vote up
static<S> Mono<Void> mockSendUsing(Connection c, Callable<? extends S> sourceInput,
		BiFunction<? super Connection, ? super S, ?> mappedInput,
		Consumer<? super S> sourceCleanup) {
	return Mono.using(
			sourceInput,
			s -> FutureMono.from(c.channel()
			                      .writeAndFlush(mappedInput.apply(c, s))),
			sourceCleanup
	);
}
 
Example 5
Source Project: james-project   File: JamesMailSpooler.java    License: Apache License 2.0 5 votes vote down vote up
private Mono<Void> processMail(MailQueueItem queueItem) {
    return Mono
        .using(
            queueItem::getMail,
            mail -> Mono.fromRunnable(() -> performProcessMail(queueItem, mail)),
            LifecycleUtil::dispose);
}
 
Example 6
@Override
public Mono<BlobId> putAndComputeId(ObjectStorageBucketName bucketName, Blob initialBlob, Supplier<BlobId> blobIdSupplier) {
    return Mono.using(
        () -> copyToTempFile(initialBlob),
        file -> putByFile(bucketName, blobIdSupplier, file),
        this::deleteFileAsync);
}
 
Example 7
@Override
public Mono<Void> save(BucketName bucketName, BlobId blobId, ByteSource content) {
    return Mono.using(content::openBufferedStream,
        stream -> save(bucketName, blobId, stream),
        Throwing.consumer(InputStream::close).sneakyThrow(),
        LAZY);
}
 
Example 8
@Override
public Mono<BlobId> save(BucketName bucketName, InputStream data, StoragePolicy storagePolicy) {
    Preconditions.checkNotNull(bucketName);
    Preconditions.checkNotNull(data);
    HashingInputStream hashingInputStream = new HashingInputStream(Hashing.sha256(), data);
    return Mono.using(
        () -> new FileBackedOutputStream(FILE_THRESHOLD),
        fileBackedOutputStream -> saveAndGenerateBlobId(bucketName, hashingInputStream, fileBackedOutputStream),
        Throwing.consumer(FileBackedOutputStream::reset).sneakyThrow(),
        LAZY_RESOURCE_CLEANUP);
}
 
Example 9
@Override
public Mono<Void> transferTo(Path dest) {
	return Mono.using(() -> AsynchronousFileChannel.open(dest, StandardOpenOption.WRITE),
			this::writeBody, this::close);
}