Search in sources :

Example 1 with SignalType

use of reactor.core.publisher.SignalType in project cf-java-client by cloudfoundry.

the class OperationsLogging method log.

@SuppressWarnings("unchecked")
public static <T extends Publisher<U>, U> Function<T, T> log(String message) {
    if (!LOGGER.isDebugEnabled()) {
        return f -> f;
    }
    AtomicLong startTimeHolder = new AtomicLong();
    Consumer<Subscription> start = subscription -> {
        startTimeHolder.set(System.currentTimeMillis());
        LOGGER.debug("START  {}", message);
    };
    Consumer<SignalType> finish = signalType -> {
        String elapsed = TimeUtils.asTime(System.currentTimeMillis() - startTimeHolder.get());
        LOGGER.debug("FINISH {} ({}/{})", message, signalType, elapsed);
    };
    return f -> {
        if (f instanceof Mono) {
            return (T) ((Mono<U>) f).doOnSubscribe(start).doFinally(finish);
        }
        if (f instanceof Flux) {
            return (T) ((Flux<U>) f).doOnSubscribe(start).doFinally(finish);
        } else {
            return f;
        }
    };
}
Also used : Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimeUtils(org.cloudfoundry.util.TimeUtils) Logger(org.slf4j.Logger) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) SignalType(reactor.core.publisher.SignalType) Function(java.util.function.Function) AtomicLong(java.util.concurrent.atomic.AtomicLong) SignalType(reactor.core.publisher.SignalType) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) Subscription(org.reactivestreams.Subscription)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 TimeUtils (org.cloudfoundry.util.TimeUtils)1 Publisher (org.reactivestreams.Publisher)1 Subscription (org.reactivestreams.Subscription)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Flux (reactor.core.publisher.Flux)1 Mono (reactor.core.publisher.Mono)1 SignalType (reactor.core.publisher.SignalType)1