简体   繁体   中英

How to get thread ID in Java

I can specify log4j formatter as follows to print current thread id in spring mvc application

log4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n

How do I programmatically get current thread id in my spring application. More precisely, I want to get current thread id in an aspect that intercepts controller methods. My aspect is as follows:

@Configurable
@Aspect
public class TimingAspect {

    @Autowired
    private HttpServletRequest httpServletRequest;

    //Generic performance logger for any mothod
    private Object logPerfomanceInfo(ProceedingJoinPoint joinPoint) {
       // do something with thread id
       // do something with httprequest
        ...
    }

    // Performance info for API calls
    @Around("execution(* package.controller.*.*(..))")
    public Object logAroundApis(ProceedingJoinPoint joinPoint) throws Throwable {
        return logPerfomanceInfo(joinPoint);
    }
}

我希望我能正确理解你的问题,这就是你要找的:

Thread.currentThread().getId()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM