/** * Executes the given block as an operation, on a blocking thread. * * @param operation a block of code to be executed, on a blocking thread * @return an operation * @since 1.4 */ default Operation blockingNext(Block operation) { return next(Blocking.op(operation)); }
public static void exec(Block block) { op(block).then(); }
@Override public Operation addMeeting(Meeting meeting) { return Blocking.op(() -> context.newRecord(MEETING, meeting).store() ); } }
/** * Executes the given action with the promise value, on a blocking thread. * <p> * Similar to {@link #blockingMap(Function)}, but does not provide a new value. * This can be used to do something with the value, without terminating the promise. * * @param action the action to to perform with the value, on a blocking thread * @return a promise for the same value given to the action */ default Promise<T> blockingOp(Action<? super T> action) { return flatMap(t -> Blocking.op(action.curry(t)).map(() -> t)); }
/** * Writes the given bytes to the given file, starting at the given position. * Use {@link #open(Path, Set, FileAttribute[])} to create a file promise. * <p> * The file channel is closed on success or failure. * * @param bytes the bytes to write * @param position the position in the file to start writing * @param file the file to write to * @return a write operation */ public static Operation write(ByteBuf bytes, long position, Promise<? extends AsynchronousFileChannel> file) { return file.flatMap(channel -> Promise.async(down -> channel.write(bytes.nioBuffer(), position, null, new CompletionHandler<Integer, Void>() { @Override public void completed(Integer result, Void attachment) { bytes.readerIndex(bytes.readerIndex() + result); down.success(null); } @Override public void failed(Throwable exc, Void attachment) { down.error(exc); } }) ) .close(bytes::release) .close(Blocking.op(((AsynchronousFileChannel) channel)::close)) ).operation(); }
/** Writes the bytes of the given publisher to the given file, returning the number of bytes written. * <p> * Use {@link #open(Path, Set, FileAttribute[])} to create a file promise. * <p> * The file channel is closed on success or failure. * <p> * As file system writes are expensive, * you may want to consider using {@link ByteBufStreams#buffer(Publisher, long, int, ByteBufAllocator)} * to “buffer” the data in memory before writing to disk. * * @param publisher the bytes to write * @param position the position in the file to start writing (must be >= 0) * @param file a promise for the file to write to * @return a promise for the number of bytes written */ public static Promise<Long> write(Publisher<? extends ByteBuf> publisher, long position, Promise<? extends AsynchronousFileChannel> file) { return file.flatMap(fileChannel -> Promise.<Long>async(down -> publisher.subscribe(new FileWritingSubscriber(fileChannel, position, down)) ) .close(Blocking.op(((AsynchronousFileChannel) fileChannel)::close)) ); }