Java Code Examples for com.linecorp.armeria.common.RequestContext#mapCurrent()

The following examples show how to use com.linecorp.armeria.common.RequestContext#mapCurrent() . 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
Source File: RequestContextCurrentTraceContext.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Armeria code should always have a request context available, and this won't work without it.
 */
@Nullable
private RequestContext getRequestContextOrWarnOnce() {
    if (Boolean.TRUE.equals(THREAD_NOT_REQUEST_THREAD.get())) {
        return null;
    }
    if (!nonRequestThreadPatterns.isEmpty()) {
        final String threadName = Thread.currentThread().getName();
        for (Pattern pattern : nonRequestThreadPatterns) {
            if (pattern.matcher(threadName).find()) {
                // A matched thread will match forever, so it's worth avoiding this regex match on every
                // time the thread is used by saving into the ThreadLocal.
                setCurrentThreadNotRequestThread(true);
                return null;
            }
        }
    }
    return RequestContext.mapCurrent(Function.identity(), LogRequestContextWarningOnce.INSTANCE);
}
 
Example 2
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public void execute(Runnable command) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    delegate.execute(ctx.makeContextAware(command));
  } else {
    delegate.execute(command);
  }
}
 
Example 3
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("ParameterPackage")
public <T> ListenableFuture<T> submit(Callable<T> task) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    task = ctx.makeContextAware(task);
  }
  return delegate().submit(task);
}
 
Example 4
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<?> submit(Runnable task) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    task = ctx.makeContextAware(task);
  }
  return delegate().submit(task);
}
 
Example 5
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submit(Runnable task, T result) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    task = ctx.makeContextAware(task);
  }
  return delegate().submit(task, result);
}
 
Example 6
Source File: CentralDogmaExceptions.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
static void log(String operationName, CentralDogmaException e) {
    final RequestContext ctx =
            RequestContext.mapCurrent(Function.identity(), () -> null);

    final ErrorCode errorCode = e.getErrorCode();
    switch (errorCode) {
        case BAD_REQUEST:
        case PROJECT_NOT_FOUND:
        case PROJECT_EXISTS:
        case REPOSITORY_NOT_FOUND:
        case REPOSITORY_EXISTS:
        case REVISION_NOT_FOUND:
        case REVISION_EXISTS:
        case ENTRY_NOT_FOUND:
        case CHANGE_CONFLICT:
        case QUERY_FAILURE:
            if (logger.isDebugEnabled()) {
                if (ctx != null) {
                    logger.debug("{} Exception with error code {} ({}) from: {}()",
                                 ctx, errorCode, errorCode.getValue(), operationName, e);
                } else {
                    logger.debug("Exception with error code {} ({}) from: {}()",
                                 errorCode, errorCode.getValue(), operationName, e);
                }
            }
            break;
        case REDUNDANT_CHANGE:
        case SHUTTING_DOWN:
            // Do not log the stack trace for REDUNDANT_CHANGE and SHUTTING_DOWN error code
            // because it is expected to occur very often.
            if (logger.isDebugEnabled()) {
                if (ctx != null) {
                    logger.debug("{} Exception with error code {} ({}) from: {}()",
                                 ctx, errorCode, errorCode.getValue(), operationName);
                } else {
                    logger.debug("Exception with error code {} ({}) from: {}()",
                                 errorCode, errorCode.getValue(), operationName);
                }
            }
            break;
        default:
            if (ctx != null) {
                logger.warn("{} Unexpected exception with error code {} ({}) from: {}()",
                            ctx, errorCode, errorCode.getValue(), operationName, e);
            } else {
                logger.warn("Unexpected exception with error code {} ({}) from: {}()",
                            errorCode, errorCode.getValue(), operationName, e);
            }
    }
}
 
Example 7
Source File: FailFastUtil.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@Nullable
static ServiceRequestContext context() {
    return RequestContext.mapCurrent(ServiceRequestContext.class::cast, null);
}
 
Example 8
Source File: RequestContextAssembly.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public final T apply(T t) {
    return RequestContext.mapCurrent(requestContext -> applyActual(t, requestContext), () -> t);
}
 
Example 9
Source File: RequestContextAssembly.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public final T apply(T t) {
    return RequestContext.mapCurrent(requestContext -> applyActual(t, requestContext), () -> t);
}