Debugging Extensions
Printing Debug Information
A simple way to view information about a running Extension is to print statements detailing the Extensions status.
To print debugging information, you can use the log function (a member function of GenericServlet) as shown below:
log( "Hello log!" );This will output the string "Hello log!" to the main event log, where it will appear as shown below:
INFO: MyPackage.MyServlet: Hello log!Java Exception Stack Traces
Java exception stack traces are useful for identifying where your code is failing. The main doGet/doPost functions can only “throw” (send) IOExceptions or ServletExceptions (and any type of RuntimeException). Be sure to address code failures before proceeding. Print an error report that you can use for debugging the code or print a stack trace to the event log.
This example shows how to “catch” an exception and write its stack to the event log:
public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
try {
throw new IOException( "Hello" );
} catch( Exception e ) {
res.sendError( 500, e.toString() );
// Save stack trace as a string and print to the log
StringWriter sw = new StringWriter();
e.printStackTrace( new PrintWriter( sw ) );
log( sw.toString() );
}
}Remote Debugging
Java has a remote debugging facility that allows you to use a Java debugger on an Extension running on the Traffic Manager.
Setting up the Traffic Manager to Accept Debugging
In this section, Eclipse is used but any Java debugger that supports remote debugging can be used.
To set up the Traffic Manager to accept debugging connections, complete the following steps:
- Go to the Catalogs > Java Extensions Catalog page of the user interface.
- Add the following line to the end of java!command, setting the following content:
-Xdebug -Xnoagent
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=nThis code must be entered on a single line.
The address value sets the port on which the Java runner “listens” for debugging connections. You can set this port to whatever port you choose.
After applying these settings, the Java Extension Runner restarts and prints the following line:
WARN: Java: Listening for transport dt_socket at address: 8000This message shows that the Traffic Manager is now ready to receive debugging connections. Next, “point” your Java debugger at the Traffic Manager server on the correct port. To do this using Eclipse, complete the following steps:
- Add your Extension code to a Java project and ensure that the project compiles correctly. (If you are developing your Extension in Eclipse, you may have already completed this step.)
- Go to the debugger and select Open Debug Dialog...
- Right-click Remote Java Application and click New.
- Under Connection Properties, enter the Traffic Manager’s hostname and the port you set as the remote debugging port (for example, 8000).
- Click Debug.
Eclipse connects to the Java Extension Runner process, and the debugger can now be used as if the code was being used locally.
Remote debugging also has the ability to “hot swap” altered code into the running system. Therefore, altering and saving code in Eclipse also updates the Java Extension in use.
However, this change only lasts until the debugging session ends. You have to upload the changes manually if you want them to be permanent.