0

is it possible to get the http request information from a thread (not the current thread)?

I want to be able to enumerate all the live threads and get the request uri for each of them.

any ideas?

thanks

isapir
  • 21,295
  • 13
  • 115
  • 116
  • how will u get all threads from container? – Ramesh PVK Dec 21 '11 at 07:31
  • Your threads may finish while you iterate, I'd rather log the URIs inside the threads (servlets), maybe to an MBean... – home Dec 21 '11 at 09:34
  • there are different ways to get the threads, but I am mostly interested in the Stack Traces so the simplest way I found is with the static method: Thread.getAllStackTraces() – isapir Dec 22 '11 at 01:46

1 Answers1

0

Try this:

  1. Create a servlet Filter.
  2. Make it implement DynamicMBean. Register the bean in the Filter's init method (and unregister it in the destroy method)
  3. Define a synchronized WeakHashMap field.
  4. In the filter's doFilter method, capture the URI of the request before the FilterChain's doFilter method is called. Insert the thread and the request URI into the WeakHashMap.
  5. Call the chain.
  6. In a finally block, insert the current thread and some arbitrary constant like NO REQUEST into the WeakHashMap.
  7. Implement the DynamicMBean so that the MBeanInfo presents one MBeanAttributeInfo per thread in the WeakHashMap. Make the attribute names the names of the threads and the type a URI (or a String).
  8. Implement the DynamicMBean so that the getAttribute method returns the URI of the thread that corresponds to the requested attribute name.
  9. Configure the filter so that it is called for all URI ranges you want to track.

When you view the attributes of the MBean, you will see the URI (or NO REQUEST) for each thread that is still active in the JVM that has processed at least one request. When a thread terminates (and perhaps after a few GC cycles), the WeakHashMap entry will be removed.

It looks a bit arduous now that I read it, but it should be pretty straightforward.

//Nicholas

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • hi Nicholas, thank you for your detailed explanation. I managed implementing half of it (the filter and the synchronized weak-hash-map) but I got stuck when I got to the MBeans section. I will try to read some more about MBeans and get that part worked out as well. thanks again :) – isapir Dec 22 '11 at 01:56
  • If you are going to do that, I think you may as well add the request object to a `ThreadLocal` in the filter – Adam Burley Nov 18 '15 at 19:29