Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this:</p> <ol> <li>Create a <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html" rel="nofollow">servlet Filter</a>. </li> <li>Make it implement <a href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/management/DynamicMBean.html" rel="nofollow">DynamicMBean</a>. Register the bean in the Filter's <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#init%28javax.servlet.FilterConfig%29" rel="nofollow">init</a> method (and unregister it in the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#destroy%28%29" rel="nofollow">destroy</a> method)</li> <li>Define a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#synchronizedMap%28java.util.Map%29" rel="nofollow">synchronized</a> <a href="http://docs.oracle.com/javase/6/docs/api/java/util/WeakHashMap.html" rel="nofollow">WeakHashMap</a> field.</li> <li>In the filter's <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain%29" rel="nofollow">doFilter</a> method, capture the URI of the request <em>before</em> the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/FilterChain.html" rel="nofollow">FilterChain</a>'s <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/FilterChain.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29" rel="nofollow">doFilter</a> method is called. Insert the thread and the request URI into the WeakHashMap.</li> <li>Call the chain.</li> <li>In a finally block, insert the current thread and some arbitrary constant like <strong>NO REQUEST</strong> into the WeakHashMap.</li> <li>Implement the DynamicMBean so that the <a href="http://docs.oracle.com/javase/6/docs/api/javax/management/MBeanInfo.html" rel="nofollow">MBeanInfo</a> presents one <a href="http://docs.oracle.com/javase/6/docs/api/javax/management/MBeanAttributeInfo.html" rel="nofollow">MBeanAttributeInfo</a> per thread in the WeakHashMap. Make the attribute names the names of the threads and the type a <strong>URI</strong> (or a <strong>String</strong>).</li> <li>Implement the DynamicMBean so that the <a href="http://docs.oracle.com/javase/6/docs/api/javax/management/DynamicMBean.html#getAttribute%28java.lang.String%29" rel="nofollow">getAttribute</a> method returns the URI of the thread that corresponds to the requested attribute name.</li> <li>Configure the filter so that it is called for all URI ranges you want to track.</li> </ol> <p>When you view the attributes of the MBean, you will see the URI (or <strong>NO REQUEST</strong>) 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.</p> <p>It looks a bit arduous now that I read it, but it should be pretty straightforward.</p> <p>//Nicholas</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload