hrishikeshp19
hrishikeshp19

Reputation: 9028

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);
    }
}

Upvotes: 5

Views: 18145

Answers (1)

kriegaex
kriegaex

Reputation: 67297

I hope I understand your question correctly and this is what you're looking for:

Thread.currentThread().getId()

Upvotes: 40

Related Questions