Last active
March 16, 2017 16:34
-
-
Save thomasdarimont/f49d25f03ca089153313 to your computer and use it in GitHub Desktop.
Simple example for using a custom Servlet-Filter to alter the HTTP Request handling thread-name to contain the request URI - to ease debugging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import static java.util.Collections.*; | |
import java.io.IOException; | |
import java.util.Map; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.http.HttpServletRequest; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
/** | |
* @author Thomas Darimont | |
*/ | |
@SpringBootApplication | |
public class App { | |
public static void main(String[] args) { | |
SpringApplication.run(App.class, args); | |
} | |
@Bean | |
public AdjustRequestThreadNameFilter threadNameFilter() { | |
return new AdjustRequestThreadNameFilter(); | |
} | |
} | |
package de.eurodata.labs.demo.web.support; | |
import java.io.IOException; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.http.HttpServletRequest; | |
import org.springframework.core.Ordered; | |
import org.springframework.core.annotation.Order; | |
@Order(Ordered.HIGHEST_PRECEDENCE) | |
class AdjustRequestThreadNameFilter implements Filter { | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { /* unused */} | |
@Override | |
public void destroy() { /* unused */} | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, | |
ServletException { | |
HttpServletRequest httpRequest = (HttpServletRequest) request; | |
Thread requestThread = Thread.currentThread(); | |
String previousThreadName = requestThread.getName(); | |
try { | |
requestThread.setName(formatRequestThreadName(httpRequest, previousThreadName)); | |
chain.doFilter(request, response); | |
} finally { | |
requestThread.setName(previousThreadName); | |
} | |
} | |
private static String formatRequestThreadName(HttpServletRequest httpRequest, String previousThreadName) { | |
return previousThreadName + " - " + httpRequest.getMethod() + " " + extractFullUrlFrom(httpRequest); | |
} | |
private static String extractFullUrlFrom(HttpServletRequest request) { | |
if (request.getQueryString() == null) { | |
return request.getRequestURI(); | |
} | |
return request.getRequestURI() + "?" + request.getQueryString(); | |
} | |
} | |
@RestController | |
class Controller { | |
@RequestMapping("/") | |
public String index() { | |
return "Hello"; | |
} | |
@RequestMapping("dummy1") | |
public Map<String, Object> dummy1() { | |
return singletonMap("dummy", 1); // set breakpoint here and look at the thread-name in the debugger :) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment