5

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

1 Answer 1

39

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

Thread.currentThread().getId()
5
  • He probably meant the interceptor thread (triggered by some event), not the currently executed code thread, if that makes sense. I want to get current thread id in an aspect that intercepts controller methods.
    – Dropout
    Commented Jun 3, 2014 at 6:19
  • No, that does not make sense because an aspect is executed in the same thread as the code it intercepts.
    – kriegaex
    Commented Jun 3, 2014 at 9:42
  • my servlet container manages a pool of threads. When a request comes in, one of the threads is used. I want that thread id. Commented Jun 3, 2014 at 17:14
  • Have you tried my code snippet? If that does not work, how about providing more context and describing the problem better?
    – kriegaex
    Commented Jun 3, 2014 at 20:14
  • @riship89 you should either accept kriegaex's answer as a solution, or describe the problem better
    – likejudo
    Commented Aug 18, 2014 at 16:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.