01.11.2010

Shutting down ehcache properly

by Mike Christianson

If you’re using ehcache’s disk persistence feature, which allows the cache to survive across JVM restarts, be sure to shut down ehcache properly.

To do so when using ehcache within a webapp, simply add its ShutdownListener as a listener in web.xml.

<listener>
    <listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
</listener>

Alternatively, or when not using ehcache inside a webapp, instruct ehcache to register its own shutdown hook by setting a system property.

net.sf.ehcache.enableShutdownHook=true

If you forget one of the above, your persisted cache may not be up-to-date, or worse, not persisted at all.

Advanced Installer 6.7 has a new licensing and registration feature which allows for time-limited demos/trials and registration of installed software.

Caphyon, the maker of Advanced Installer, provides code samples and documentation on licensing integration for C++ and C# applications. To use this feature, the application must make a call into an Advanced Installer library and handle its return code.

The library is a gatekeeper to the rest of the application, effectively deciding whether or not the application is allowed to run. It determines the software’s trial status and displays a registration dialog, terminates the application, or returns a code, accordingly. The Java implementation of this feature works a little differently.

For those using the Java Launcher, integrating the licensing feature is easy: simply add the Java Product on the Licensing Options tab. But, what about those who don’t use Java Launcher? Or those that wish to enable users to register from within the application?

There is no official documentation which answers these questions, but based on a discussion following a request for help I posted on the Advanced Installer forums, I was able integrate AI’s licensing feature into my Java application without using the Java Launcher. Read more

One month ago I wrote about the benefits of using Java’s built-in garbage collection logging.  With it, you can find answers to important important questions such as “how much memory is my app using” and “how much time is being spent doing garbage collection.”

There is, of course, a hitch: for any non-trivial application or problem, you will be quickly buried by a mountainous log file.  The longer your application runs, the larger the log file.  It keeps growing, and growing, and…

GCViewer

GCViewer

What will you do?  Write a second Java app, one that parses the log file — attempt a homegrown analysis tool?  No!  Instead, I suggest you use GCViewer, a free open-source tool for visualizing the Java gc log file.

GCViewer helps you get a quick and comprehensive look at how your application is behaving.  Things like total heap vs. heap used and full GC events become very apparent.  Useful statistics such as time spent during GC are calculated for your convenience.

GCViewer is created by tagtraum industries and is available under an LGPL license.

12.18.2008
SeaFlow screenshot from Dec. 18, 2008.

SeaFlow screenshot from Dec. 18, 2008.

Late in 1999, UltiMeth Systems created SeaFlow, a Java-based application for monitoring Seattle-area traffic conditions.  SeaFlow is a powerful desktop version of WSDOT’s web-based traffic map.  It uses the same WSDOT data but shows greater detail, allows customization, and reduces network traffic.

SeaFlow displays vehicle speeds, density, and count metrics.  (WSDOT’s map shows only density which is certainly useful, but not a complete picture.)  The visual color representation of each metric can be customized in SeaFlow.

In times of peak demand, such as December 18, 2008, WSDOT’s website may become overwhelemed and yield partial or missing maps.  SeaFlow’s use of the smaller WSDOT data file — with maps rendered on the client-side — reduces demand on WSDOT’s site and can provide more reliable and complete updates.

SeaFlow is a great Java desktop app — small, efficient, and cross-platform.  Its license is GPL.

12.11.2008

Java’s built-in garbage collection logging provides a quick, easy, and free way to profile or troubleshoot your Java application.  It can help you understand your application in terms of:

So, now you can answer questions like “how much memory is my app using,” “how much time is being spent doing garbage collection,” and “how big is the heap over time.”

For help analyzing and visualizing a GC log file, be sure to check out GCViewer, a garbage collection and heap analysis tool.

GC logging is enabled using JVM arguments; below are the arguments I use. Read more

12.03.2008

There’s no law which states web servers and servlet containers must be large, separate pieces of software.  If there were, Jetty would be a dangerous outlaw (possibly even public enemy number one).  Jetty is an open-source embeddable web server and servlet container, written in Java.  It’s small, fast, and easy to embed — perfect for self-contained applications.

Let’s use an example to discover just how easy embedding Jetty can be.  Read more

11.21.2008

Remote debugging

by Mike Christianson

I never knew how good life could be — at work, mind you — until I spent some quality time with the Eclipse debugger.  My days of relying solely on System.out.println() are long gone.  Print debugging is only useful if you can change the code.  Loggers like Log4j are only marginally better — assuming the code uses it properly in the first place.

And things get more interesting when the code is running on a different or remote machine.  Luckily, Java has a great remote debugging facility. Here’s an example of how I enable remote debugging using command-line arguments.

-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n

In this example, the remote application will start and run normally while listening in the background for a debugger to connect on port 8000.  For further details on how to enable and configure remote debugging, see Sun’s JPDA Connection and Invocation guide.

11.19.2008

Remote JMX

by Mike Christianson

The following Java system properties allow unauthenticated and unsecured remote JMX client connections. I use these system properties when remotely troubleshooting problems during development.  My JMX agent of choice is JConsole, part of the JDK.

-Dcom.sun.management.jmxremote.port=port
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

For further details on JMX and how to configure access, consult Sun’s Java JMX Management Guide.

11.17.2008

JScience unit formatting

by Mike Christianson

JScience uses Unicode characters in the String representations of some of its Units.  For example, Unicode U+2103 (℃) is used to represent the unit for degrees Celsius.  Those characters can cause problems with fonts and/or platforms (Windows, typically) which do not contain or recognize them.  Instead, you may see a question mark, box, or other unfamiliar glyph.  Here is the symbol for degrees Celsius again — ℃ — can you see it properly?

Using UnitFormat.label(), we can override the String representation to something more compatible, useful, or just different.  Here’s how I changed degrees Celsius to be more compatible with my co-workers Windows systems.

UnitFormat.getInstance().label(SI.CELSIUS, "\u00B0" + "C");

And here’s how I changed knots to be represented by kts instead of the default, kn.

UnitFormat.getInstance().label(NonSI.KNOT, "kts");
11.17.2008

Pretty XML from XOM

by Mike Christianson

Getting nicely indented XML from XOM as a String is not intuitive. One might reasonably expect to try document.toXML() only to find the output lacking indentation.

Here’s how I used XOM’s Serializer and Java’s ByteArrayOutputStream to produce pretty XML.

1
2
3
4
5
ByteArrayOutputStream out = new ByteArrayOutputStream();
Serializer serializer = new Serializer(out, "UTF-8");
serializer.setIndent(2);
serializer.write(document);
String xml = out.toString("UTF-8");

Next Page »