What is the best way to create a debugging web page for a computation in Java?
I'm developing a website that uses some complex comput开发者_运维技巧ations (NLP-related). My customer wants to have "debugging" webpages for some of these computations where he can run them with arbitrary input and see all the intermediate results that occur during computation.
Before this request all of the computations were encapsulated in beans and intermediate results were logged into general log.
What is the best way to capture all these results on Java level to render them as webpage?
I'd recommend embedding a servlet container (Apache Tomcat or Jetty, I like Tomcat more) in your application and creating a set of JSP/JSF servlets for the web interface. This solution will let you use all java enterprise technologies (like JAAS) with your web interface and avoid reinventing the wheel.
I would estimate the effort of creating such solution with tomcat to 1 day of intensive copy/paste. After the skeleton starts breathing, web pages themselves can be created in Eclipse visual tool with several mouse drags/clicks.
The relevant links are how to embed tomcat 6 embedding Jetty Eclipse JSF Tools
Finally, I came up the following solution: use an optional ThreadLocal<Map>
in a computation to store intermediate values. In the end it looks approximately like this:
public class ComplexComputation {
public static final ThreadLocal<Map<String, Object>> DATA
= new ThreadLocal<Map<String, Object>>();
public Result compute(Inputs inputs) {
// ... do something ...
if (DATA.get() != null) { DATA.get().put("intermediate", intermediate); }
// ... do something ...
return result;
}
}
public void computationDebuggingCaller() {
ComplexComputation.DATA.set(new HashMap<String, Object>());
new ComplexComputation().compute(...);
Object intermediate = ComplexComputation.DATA.get().get("intermediate");
}
精彩评论