Using VisualVM to monitor remote application server
As you all know, you can use tools like VisualVM to monitor remote Java processes. Only problem with that is you can't use Sampler and Profiler options on remote Java process.
So, how can we use this tool to monitor our application server that is running somewhere else? We can use power of Linux and little something called X11 server.
The one thing that you will need is a local PC that is running Linux (this is presumably that your server is also running Linux, but headless).
Since VisualVM comes as .zip file, you can use:
unzip visualvm_137.zip
If are you using Java older than 1.7 on your server, you must also provide JDK 1.7 so you can run VisualVM. Since Java for Linux comes as tar.gz you can use:
tar xvzf jdk-7u67-linux-x64.gz
Note: VisualVM must be started as the user who is running the Java proces you are trying to monitor, so it would be the easiest to do all this as that same user.
After this you are done with "configuring" your server for monitoring. Now on your local Linux machine, you configure your X11 to accept connections from all sources by opening your terminal and typing:
xhost +
After that, you can use ssh to connect to your server, but use -Y as option:
ssh -Y user@remotemachine
When you login into your server, position yourself in bin folder where you unzipped VisualVM and start it the usual way:
./visualvm --jdkhome "PATH_TO_JDK"
Path to JDK home is required only if you don't have Java 1.7 (or higher) on your path.
After running this command you should get a VisualVM window in your desktop, but only difference it that it will be running locally on your server with all features enabled.
If you are using Crystal Reports Viewer for viewing RPT files in browser, then you should be aware that JDBC connections and other resources aren't closed for you after user session expire.
And to make things worse, the official example that show how to clean resource is, at best, just bad practice. It depends on JavaScript and presume that:
your users have enabled popups for your domain
they are using browser that has implemented onunload event
As we all know those two things in these days are really hard to force on users, but I will show you how you can clean your resources when session expires by using HttpSessionListener and forget about that JavaScript and iframe.
Let's presume for this example, that you have followed official guide on setting up you'r CR viewer. It that case your ReportClientDocument will be save in session by name oClientDoc.
session.setAttribute("oClientDoc", clientDoc);
So now we will create HttpSessionListener that will do some job for us just before session is destroyed.
package com.tumblr.enterprise.java; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class CRViewerCleanupListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent session) { //we don't do anything on session create event } @Override public void sessionDestroyed(HttpSessionEvent session) { ReportClientDocument client = (ReportClientDocument)session.getAttribute("oClientDoc"); client.close(); } }
If you are using Servlet 3.0 you can register your listener by placing @WebListener annotation above class declaration. If your server, doesn't support Servlet 3.0 specification, you can still use good old xml and place this line of code into your web.xml: