Search in sources :

Example 11 with ContextMapHolder

use of io.servicetalk.context.api.ContextMapHolder in project servicetalk by apple.

the class ContextPreservingCancellable method cancel.

@Override
public void cancel() {
    final Thread currentThread = Thread.currentThread();
    if (currentThread instanceof ContextMapHolder) {
        final ContextMapHolder asyncContextMapHolder = (ContextMapHolder) currentThread;
        ContextMap prev = asyncContextMapHolder.context();
        try {
            asyncContextMapHolder.context(saved);
            delegate.cancel();
        } finally {
            asyncContextMapHolder.context(prev);
        }
    } else {
        slowPath();
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Example 12 with ContextMapHolder

use of io.servicetalk.context.api.ContextMapHolder in project servicetalk by apple.

the class ContextPreservingBiConsumer method accept.

@Override
public void accept(T t, U u) {
    final Thread currentThread = Thread.currentThread();
    if (currentThread instanceof ContextMapHolder) {
        final ContextMapHolder asyncContextMapHolder = (ContextMapHolder) currentThread;
        ContextMap prev = asyncContextMapHolder.context();
        try {
            asyncContextMapHolder.context(saved);
            delegate.accept(t, u);
        } finally {
            asyncContextMapHolder.context(prev);
        }
    } else {
        slowPath(t, u);
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Example 13 with ContextMapHolder

use of io.servicetalk.context.api.ContextMapHolder in project servicetalk by apple.

the class ContextPreservingCallable method call.

@Override
public V call() throws Exception {
    final Thread currentThread = Thread.currentThread();
    if (currentThread instanceof ContextMapHolder) {
        final ContextMapHolder asyncContextMapHolder = (ContextMapHolder) currentThread;
        ContextMap prev = asyncContextMapHolder.context();
        try {
            asyncContextMapHolder.context(saved);
            return delegate.call();
        } finally {
            asyncContextMapHolder.context(prev);
        }
    } else {
        return slowPath();
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Example 14 with ContextMapHolder

use of io.servicetalk.context.api.ContextMapHolder in project servicetalk by apple.

the class AsyncContextMapThreadLocal method get.

ContextMap get() {
    final Thread t = Thread.currentThread();
    if (t instanceof ContextMapHolder) {
        final ContextMapHolder contextMapHolder = (ContextMapHolder) t;
        ContextMap map = contextMapHolder.context();
        if (map == null) {
            map = newContextMap();
            contextMapHolder.context(map);
        }
        return map;
    } else {
        return CONTEXT_THREAD_LOCAL.get();
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Example 15 with ContextMapHolder

use of io.servicetalk.context.api.ContextMapHolder in project servicetalk by apple.

the class ContextPreservingCompletableSubscriber method onSubscribe.

@Override
public final void onSubscribe(final Cancellable cancellable) {
    final Thread currentThread = Thread.currentThread();
    if (currentThread instanceof ContextMapHolder) {
        final ContextMapHolder asyncContextMapHolder = (ContextMapHolder) currentThread;
        ContextMap prev = asyncContextMapHolder.context();
        try {
            asyncContextMapHolder.context(saved);
            invokeOnSubscribe(cancellable);
        } finally {
            asyncContextMapHolder.context(prev);
        }
    } else {
        onSubscribeSlowPath(cancellable);
    }
}
Also used : ContextMapHolder(io.servicetalk.context.api.ContextMapHolder) ContextMap(io.servicetalk.context.api.ContextMap)

Aggregations

ContextMap (io.servicetalk.context.api.ContextMap)20 ContextMapHolder (io.servicetalk.context.api.ContextMapHolder)20