Watching files in Java 7 with WatchService
In this article I show how to watch files and directories for additions/changes/deletions using the new file system support in Java 7. Finally, after years of native solutions, it is now possible to listen for file changes without resorting to OS specific solutions. In the code below, first we use the new file system support class Paths to get a Path object that represents our particular directory. We then create a watching service, that we will use to listen for changes and finally we register our path (directory) with the watcher for files that are created.
Formatting numbers using NumberFormat
java.text.NumberFormat is the class used to convert numeric values such as int, long and double into Strings. It has been around since the early days of Java, and generally performs well. Especially if you cache an instance of the class for repeated use. IMHO the only down side to using NumberFormat is that it can look a little verbose. To get hold of a copy of the NumberFormat class, call one of the static factory methods on the NumberFormat object:
Reading a GZIP file using Java
Another common compression file format on Linux is the GZIP format. Java again has out of the box support for this file format. Gzip files differ from zip files in that they only contain one file, the compressed form of the original file with a .gz extension. Java’s GZipInputStream takes such a file type and decompresses it. We can treat GZipInputStream directly like a FileInputStream. Here is an example that expands such a file to disk
Reading a zip file from java using ZipInputStream
Java provides support for reading zip files in the form of ZipInputStream. This class provides an API where you can iterate over all the items in a given zip file, reading the data from the archive for each file. In order to do this, first you must create the ZipInputStream instance giving the file that you wish to expand. Then you iterate using the getNextEntry method on the stream, which returns the header data for each entry in turn.
Setting up role based security in tomcat
Tomcat and most other Java JEE servers support role based security, form based authentication and SSL. These technologies are integrated into your web application declaratively. At first the configuration for this security can look a little daughnting but once understood it is actually not difficult to configure. This article is split over several pages and requires an understanding of tomcat and how a JEE webapp is structured. Most of the article is structured towards tomcat, but if you use another application server, many of the concepts are the same.
Using thread local in Java
This article assumes that you are already familiar with concurrent programming and design. Java 1.5 introduced the concurrent library java.util.concurrent, which provides an extensive set of classes for dealing with concurrency issues, these sit alongside some existing classes that have been around since earlier times. I've noticed that some of the classes I mention here don't get used as often as they should, especially given that they are quick wins. This article only scratches the surface of the new classes available, but should provide a starting point for further reading.
Are we prone to over optimising code?
As a developer I often feel the need to performance tune code, or write “the best piece of code ever”. Sometimes this happens even before I know which sections of the code will be executed frequently? These days I make every attempt to hold back from this approach. In applications where performance is critical I usually write it the most natural way and apply any optimisations required later. Usually in such systems I am more worried about garbage collection / not getting a cache hit for memory, than outright CPU performance.
Concurrent Maps and CopyOnWriteArrayList
Copy on write lists provide a very quick win in terms of removing synchronization. I believe that a significant use case is providing a thread safe list for java's Listener (AKA observer) pattern. Copy on write lists make the assumption that the list does not update frequently, and is mainly used for reading. If this is not the case, the overhead may be worse than synchronizing. java.util.concurrent.CopyOnWriteArrayList implements the List interface and infact is also a random access container (implements RandomAccess).
GC monitoring in Java with Jstat
Recently I had a few performance problems with an application that required JVM GC monitoring. It had been so long since the last time I’d had to perform any CG analysis that I had to remind myself of what to do! Using jps and jstat to get JVM GC statistics First, ensure that an appropriate JDK is on your path [dave@titan ~]$ java -version java version "1.7.0_45" OpenJDK Runtime Environment (fedora-2.