org.springframework.core.NamedThreadLocal类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(549)

本文整理了Java中org.springframework.core.NamedThreadLocal类的一些代码示例,展示了NamedThreadLocal类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NamedThreadLocal类的具体详情如下:
包路径:org.springframework.core.NamedThreadLocal
类名称:NamedThreadLocal

NamedThreadLocal介绍

[英]ThreadLocal subclass that exposes a specified name as #toString() result (allowing for introspection).
[中]ThreadLocal子类,将指定的名称公开为#toString()结果(允许内省)。

代码示例

代码示例来源:origin: spring-projects/spring-framework

static InjectionPoint setCurrentInjectionPoint(@Nullable InjectionPoint injectionPoint) {
  InjectionPoint old = currentInjectionPoint.get();
  if (injectionPoint != null) {
    currentInjectionPoint.set(injectionPoint);
  }
  else {
    currentInjectionPoint.remove();
  }
  return old;
}

代码示例来源:origin: a466350665/smart

@Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    long endTime = System.currentTimeMillis();// 2、结束时间
    long beginTime = startTimeThreadLocal.get();// 得到线程绑定的局部变量(开始时间)
    long consumeTime = endTime - beginTime;// 3、消耗的时间
    if (consumeTime > 500) {// 此处认为处理时间超过500毫秒的请求为慢请求
      logger.info(String.format("%s consume %d millis", request.getRequestURI(), consumeTime));
    }
  }
}

代码示例来源:origin: a466350665/smart

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  long beginTime = System.currentTimeMillis();// 1、开始时间
  startTimeThreadLocal.set(beginTime);// 线程绑定变量(该数据只有当前请求的线程可见)
  return true;// 继续流程
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void defaultRequest() {
  ThreadLocal<String> context = new NamedThreadLocal<>("foo");
  Map<String, Object> actual = new HashMap<>();
  ExchangeFilterFunction filter = (request, next) -> {
    actual.putAll(request.attributes());
    return next.exchange(request);
  };
  WebClient client = this.builder
      .defaultRequest(spec -> spec.attribute("foo", context.get()))
      .filter(filter)
      .build();
  try {
    context.set("bar");
    client.get().uri("/path").attribute("foo", "bar").exchange();
  }
  finally {
    context.remove();
  }
  assertEquals("bar", actual.get("foo"));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Overridden in order to implicitly register the currently created bean as
 * dependent on further beans getting programmatically retrieved during a
 * {@link Supplier} callback.
 * @since 5.0
 * @see #obtainFromSupplier
 */
@Override
protected Object getObjectForBeanInstance(
    Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
  String currentlyCreatedBean = this.currentlyCreatedBean.get();
  if (currentlyCreatedBean != null) {
    registerDependentBean(beanName, currentlyCreatedBean);
  }
  return super.getObjectForBeanInstance(beanInstance, name, beanName, mbd);
}

代码示例来源:origin: Cosium/spring-data-jpa-entity-graph

@Override
 public Object invoke(MethodInvocation invocation) throws Throwable {
  if (invocation.getMethod().getName().startsWith(FETCH_METHOD_NAME_PREFIX)) {
   IS_COUNT_QUERY.set(true);
  }
  try {
   return invocation.proceed();
  } finally {
   IS_COUNT_QUERY.set(false);
  }
 }
}

代码示例来源:origin: apache/servicemix-bundles

new NamedThreadLocal<Map<Object, ScriptEngine>>("ScriptTemplateView engines");

代码示例来源:origin: org.springframework/spring-beans

static InjectionPoint setCurrentInjectionPoint(@Nullable InjectionPoint injectionPoint) {
  InjectionPoint old = currentInjectionPoint.get();
  if (injectionPoint != null) {
    currentInjectionPoint.set(injectionPoint);
  }
  else {
    currentInjectionPoint.remove();
  }
  return old;
}

代码示例来源:origin: org.springframework/spring-beans

/**
 * Overridden in order to implicitly register the currently created bean as
 * dependent on further beans getting programmatically retrieved during a
 * {@link Supplier} callback.
 * @since 5.0
 * @see #obtainFromSupplier
 */
@Override
protected Object getObjectForBeanInstance(
    Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
  String currentlyCreatedBean = this.currentlyCreatedBean.get();
  if (currentlyCreatedBean != null) {
    registerDependentBean(beanName, currentlyCreatedBean);
  }
  return super.getObjectForBeanInstance(beanInstance, name, beanName, mbd);
}

代码示例来源:origin: com.wuyushuo/vplus-data

public void setUpDataSourceTableLookup(Lookup lookup) {
  if (lookup != null) {
    if (!lookup.isShard()) {
      this.lookup.set(lookup);
    } else {
      String shardType = lookup.getShardType();
      ShardStrategy shardStrategy = (ShardStrategy)this.shardStrategies.get(shardType);
      if (shardStrategy == null) {
        throw new IllegalArgumentException("未确定散表算法");
      } else {
        ShardStrategy.DataTableName dataTableName = shardStrategy.handle(lookup.getTableName(), lookup.getDataSourceKey(), lookup.getTableShardNum(), lookup.getDbShardNum(), lookup.getShardValue());
        lookup.setDataTableName(dataTableName);
        this.lookup.set(lookup);
      }
    }
  }
}

代码示例来源:origin: apache/servicemix-bundles

new NamedThreadLocal<JmsUserCredentials>("Current JMS user credentials");

代码示例来源:origin: spring-projects/spring-framework

/**
 * Obtain a bean instance from the given supplier.
 * @param instanceSupplier the configured supplier
 * @param beanName the corresponding bean name
 * @return a BeanWrapper for the new instance
 * @since 5.0
 * @see #getObjectForBeanInstance
 */
protected BeanWrapper obtainFromSupplier(Supplier<?> instanceSupplier, String beanName) {
  Object instance;
  String outerBean = this.currentlyCreatedBean.get();
  this.currentlyCreatedBean.set(beanName);
  try {
    instance = instanceSupplier.get();
  }
  finally {
    if (outerBean != null) {
      this.currentlyCreatedBean.set(outerBean);
    }
    else {
      this.currentlyCreatedBean.remove();
    }
  }
  if (instance == null) {
    instance = new NullBean();
  }
  BeanWrapper bw = new BeanWrapperImpl(instance);
  initBeanWrapper(bw);
  return bw;
}

代码示例来源:origin: spring-projects/spring-framework

InjectionPoint injectionPoint = currentInjectionPoint.get();
if (injectionPoint == null) {
  throw new IllegalStateException("No current InjectionPoint available for " + param);

代码示例来源:origin: apache/servicemix-bundles

new NamedThreadLocal<Object>("Prototype beans currently in creation");

代码示例来源:origin: org.springframework/spring-beans

/**
 * Obtain a bean instance from the given supplier.
 * @param instanceSupplier the configured supplier
 * @param beanName the corresponding bean name
 * @return a BeanWrapper for the new instance
 * @since 5.0
 * @see #getObjectForBeanInstance
 */
protected BeanWrapper obtainFromSupplier(Supplier<?> instanceSupplier, String beanName) {
  Object instance;
  String outerBean = this.currentlyCreatedBean.get();
  this.currentlyCreatedBean.set(beanName);
  try {
    instance = instanceSupplier.get();
  }
  finally {
    if (outerBean != null) {
      this.currentlyCreatedBean.set(outerBean);
    }
    else {
      this.currentlyCreatedBean.remove();
    }
  }
  if (instance == null) {
    instance = new NullBean();
  }
  BeanWrapper bw = new BeanWrapperImpl(instance);
  initBeanWrapper(bw);
  return bw;
}

代码示例来源:origin: org.springframework/spring-beans

InjectionPoint injectionPoint = currentInjectionPoint.get();
if (injectionPoint == null) {
  throw new IllegalStateException("No current InjectionPoint available for " + param);

代码示例来源:origin: apache/servicemix-bundles

static InjectionPoint setCurrentInjectionPoint(InjectionPoint injectionPoint) {
  InjectionPoint old = currentInjectionPoint.get();
  if (injectionPoint != null) {
    currentInjectionPoint.set(injectionPoint);
  }
  else {
    currentInjectionPoint.remove();
  }
  return old;
}

代码示例来源:origin: Cosium/spring-data-jpa-entity-graph

static boolean isCountQuery() {
 return IS_COUNT_QUERY.get() == null ? false : IS_COUNT_QUERY.get();
}

代码示例来源:origin: com.wuyushuo/vplus-data

public static String getDataSourceKey() {
  Lookup dsLookup = (Lookup)lookup.get();
  String dataSourceKey = null;
  if (dsLookup != null) {
    dataSourceKey = dsLookup.getDataSourceKey();
    return dataSourceKey;
  } else {
    throw new IllegalArgumentException("数据源指定错误 or 获取连接错误 or SQL错误");
  }
}

代码示例来源:origin: com.wuyushuo/vplus-data

public static String getTableName() {
  return ((Lookup)lookup.get()).getTableName();
}

相关文章

微信公众号

最新文章

更多