/** * Kills the session. * * @return A completable future to be completed once the session has been killed. */ public CompletableFuture<Void> kill() { return submitter .close() .thenCompose(v -> listener.close()) .thenCompose(v -> manager.kill()) .thenCompose(v -> connection.close()); }
/** * Closes the session. * * @return A completable future to be completed once the session is closed. */ public CompletableFuture<Void> close() { CompletableFuture<Void> future = new CompletableFuture<>(); submitter .close() .thenCompose(v -> listener.close()) .thenCompose(v -> manager.close()) .whenComplete( (managerResult, managerError) -> { connection .close() .whenComplete( (connectionResult, connectionError) -> { if (managerError != null) { future.completeExceptionally(managerError); } else if (connectionError != null) { future.completeExceptionally(connectionError); } else { future.complete(null); } }); }); return future; }
/** * Registers an event listener. * * <p>The registered {@link Consumer} will be {@link Consumer#accept(Object) called} when an event * is received from the Raft cluster for the session. {@link Session} implementations must * guarantee that consumers are always called in the same thread for the session. Therefore, no * two events will be received concurrently by the session. Additionally, events are guaranteed to * be received in the order in which they were sent by the state machine. * * @param event The event to which to listen. * @param callback The session receive callback. * @param <T> The session event type. * @return The listener context. * @throws NullPointerException if {@code event} or {@code callback} is null */ public <T> Listener<T> onEvent(String event, Consumer<T> callback) { return listener.onEvent(event, callback); }
/** * Registers a void event listener. * * <p>The registered {@link Runnable} will be {@link Runnable#run() called} when an event is * received from the Raft cluster for the session. {@link Session} implementations must guarantee * that consumers are always called in the same thread for the session. Therefore, no two events * will be received concurrently by the session. Additionally, events are guaranteed to be * received in the order in which they were sent by the state machine. * * @param event The event to which to listen. * @param callback The session receive callback. * @return The listener context. * @throws NullPointerException if {@code event} or {@code callback} is null */ public Listener<Void> onEvent(String event, Runnable callback) { return listener.onEvent(event, callback); }