Tag Archives: Java

Presentation – Java Web Start : How Zhara POS Works

This is a presentation that I did recently for my team, on Zhara POS (which is the Point of Sale module of Zhara Hospitality Suite, the product that I’m working on at JKCS). We developed the application using Swing, and deployed it over the web via Java Web Start.

The presentation discusses the rationale for using Java Web Start, and how we applied it to meet with the requirements.

The audience of the presentation was the entire Zhara Team, which consists of designers, developers, QA, business analysts working on many modules.

This presentation was part of the Zhara Tech Talks Series (session 03), which is held bi-weekly, where the team meets up and discuss about various topics of interest, both technical and non-technical.

The Dark Art of Logging

Logging is a mechanism of keeping track of the runtime behavior of an application, mainly for debugging / tracing purposes. In almost all of significantly complex / vital applications, maintaining a proper application log is a must; hence, almost all developers are quite familiar with the paradigm of logging. And it isn’t rocket science. You just have to call a simple method with whatever you want to log, and mess around with a simple configuration file, and that’s all. So what’s the big deal?

The problem is that though logging is a simple thing to do, doing it correctly needs a lot of practice, experience and understanding. That makes it more of an art than a science. In my personal experience, mastering the art of logging requires lots of patience, experience and forward thinking. It’s quite difficult to teach someone how to do it (sort of tacit knowledge), and most freshers tend to see the effort put into writing to log files as something additional. But all that effort pays up when you go through long nights trying to figure out what went wrong in a production system. But for that, it is important that proper logging is done through-out the system, which requires all of the team to master this dark art. Just one guy doing it right in a team is simply not enough.
Continue reading

Know the JVM Series – 4 – Thread Locals

Thread Local Storage (TLS) is a special construct in multi-threaded programming, which allows to associate a variable with a particular thread. This is different from normal variables, which are associated with a process. In other words, a normal variable will be shared by threads of a same process (in procedural programming terms). However, a Thread Local variable has a copy of it per thread, and modifications by a particular thread is applicable for code running inside that thread only.

With the inherent multi-threaded nature of most enterprise Java applications, having some understanding about Thread Local variables are useful to solve some problems which would otherwise be difficult. We’ll start our discussion by looking into a practical problem that is faced by JEE applications, so that we have a good understanding about the power Thread Locals.

Consider a web based JEE application, which consists of JSP/Servlet based presentation tier, EJB based service facade tier, and business logic implemented in POJOs, and finally Hibernate in the persistence tier. In this application, users are required to authenticate before using secured parts of the application, and we will be using the standard JAAS to facilitate this. Assume that in our POJO based Business Logic classes and Hibernate DAOs, we need to obtain the currently logged in users ID for auditing purposes. How can we solve this problem? Continue reading

Know the JVM Series -3- When Weaker is Better: Understanding Soft, Weak and Phantom References

How many times have we created various object instances, and assign those to reference variables? We all know very well that Java has automatic garbage collection; so we just play around the reference variables, and once those variables are assigned null or falls out of scope, JVM takes care of it. No need to worry about ‘free’ as in C / C++. It’s a headache-less approach, which minimizes the risk of introducing memory leaks to our programs, and it works out great day in day out in billions of Java applications running out there 24×7. Kudos to John McCarthy for inventing GC for Lisp, and to  all the folks who implemented the concept in Java.

But there are times, where we a little bit of more control over the process of garbage collection. I’m not talking about the dark art of tuning the garbage collector (which I might cover in a later article). This is about programmatic situations where we expect some object instances to be eligible for garbage collection, to release some unwanted memory that might get accumulated over the time. Well, the classic solution of explicitly assigning null could help us out; given that particular object is referred only through that particular ref variable. What if assigning null doesn’t work out for the problem at hand?

Continue reading

Know the JVM Series – 2 – Shutdown Hooks

In this article of the Know the JVM Series, we will be looking at yet another feature that is offered by the JVM, which is not very well known, and used by the developers. Shutdown Hooks are a special construct that allows developers to plug in a piece of code to be executed when the JVM is shutting down. This comes in handy in cases where we need to do special clean up operations in case the VM is shutting down.

Handling this using the general constructs such as making sure that we call a special procedure before the application exists (calling System.exit(0) ) will not work for situations where the VM is shutting down due to an external reason (ex. kill request from O/S), or due to a resource problem (out of memory). As we will see soon, shutdown hooks solve this problem easily, by allowing you to provide an arbitrary code block, which will be called by the JVM when it is shutting down.

From the surface, using a shutdown hook is downright straight forward. All you have to do is simply write a class which extends the java.lang.Thread class, and provide the logic that you want to perform when the VM is shutting down, inside the public void run() method.  Then you register an instance of this class as a shutdown hook to the VM by calling Runtime.getRuntime().addShutdownHook(Thread) method. If you need to remove a previously registered shutdown hook, the Runtime class provides the removeShutdownHook(Thread) method as well.

For example,

public class TestHook {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                System.out.println("Shutdown Hook is running !");
        System.out.println("Application Terminating ...");

That’s it, and when you run the above code, you will see that the shutdown hook is getting called by the JVM when it finishes execution of the main method. The output you will get is,

Application Terminating ...
Shutdown Hook is running !

Simple right? Yes it is.

So the question popping in your mind would be “what’s the purpose of writing an article to demonstrate something that is so simple, that could have easily fit within the 140 character limit of a tweet?”;  well, the answer is that story doesn’t end there.

While it is pretty simple to write a shutdown hook, one needs to know the internals behind the shutdown hooks to make use of those properly. Therefore, in this article, we will be exploring some of the ‘gotchas’ behind the shutdown hook design. Continue reading