91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring MVC Interceptor 實現性能監控的功能代碼

發布時間:2020-09-23 10:01:55 來源:腳本之家 閱讀:155 作者:程序員筆記 欄目:編程語言

利用Spring MVC 的 Interceptor 實現個簡易的性能監控,計算一下每個url的執行時間。

在 preHandle 方法中記錄當前的時間戳到線程局部變量里,然后在afterCompletion方法中,用當前的時間戳剪掉線程局部變量里的時間戳得出個運行時間,并記錄到log里。

這只是個簡易的性能監控,如果想長期正式的監控性能,請選擇其它成熟的產品。

Interceptor

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NamedThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
 
public class PerformanceInterceptor implements HandlerInterceptor {
 
  private final static Logger LOGGER = LoggerFactory.getLogger(PerformanceInterceptor.class);
 
  private NamedThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("performance");
 
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    startTimeThreadLocal.set(System.currentTimeMillis());
    return true;
  }
 
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
      ModelAndView modelAndView) throws Exception {
    return;
  }
 
  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    long currentTime = System.currentTimeMillis();
    long executeTime = currentTime - startTimeThreadLocal.get();
    LOGGER.info("uri:{} 執行了 {} 毫秒", request.getRequestURI(), executeTime);
  }
 
}

這里的是NamedThreadLocal沒啥神器的,源碼如下:

package org.springframework.core;
 
import org.springframework.util.Assert;
 
public class NamedThreadLocal<T> extends ThreadLocal<T> {
 
  private final String name;
 
  public NamedThreadLocal(String name) {
    Assert.hasText(name, "Name must not be empty");
    this.name = name;
  }
 
  @Override
  public String toString() {
    return this.name;
  }
 
}

定義完Interceptor,注冊即可。

import java.util.List;
 
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
import com.vnierlai.scm.webapp.interceptor.PerformanceInterceptor;
 
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
 
  @Bean
  @ConditionalOnMissingBean(CharacterEncodingFilter.class)
  public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    return filter;
  }
 
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new PerformanceInterceptor()).addPathPatterns("/**");
    super.addInterceptors(registry);
  }
 
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    // registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  }
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

四平市| 北川| 平谷区| 如皋市| 尼勒克县| 嘉禾县| 玛多县| 新乐市| 延长县| 惠来县| SHOW| 明光市| 灵山县| 武乡县| 梅州市| 泾阳县| 棋牌| 敦化市| 信宜市| 施甸县| 吴川市| 合肥市| 伊宁县| 聂拉木县| 镇宁| 册亨县| 潼南县| 南宁市| 仙游县| 额尔古纳市| 敦化市| 酒泉市| 汝城县| 隆化县| 株洲县| 迁安市| 安塞县| 乌兰浩特市| 电白县| 梁河县| 灵璧县|