Monthly Archives: March 2014

JAVA_OPTS, how to use them

For most of you this is probably a very silly post, but after five years of experience I was still shocked by something about the infamous JAVA_OPTS, also known as the “-D” system properties… -Dkey=value

JAVA_OPTS is only the name under which some applications, servers, etc… group a few of these -Dkey=value properties to be passed into Java when it gets executed.

You can always pass them directly from the command-line or whatever script you’re using to trigger the execution of your program. Even combine some of them inside a JAVA_OPTS environment variable and some others outside of it.

But there are certain rules you must respect when using them, as I learnt today. I wrote a silly Java class which prints out the values for three properties that it expects with certain names, and I called this class on many different manners.

This is the (simplest) class (ever):

public class JavaOpts101 {
    public static void main(String[] args) {
        System.out.println("Property 2: " + System.getProperty("property1"));
        System.out.println("Property 2: " + System.getProperty("property2"));
        System.out.println("Property 3: " + System.getProperty("property3"));
    }
}

These are the results:

Doing it right, using JAVA_OPTS:

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2 -Dproperty3=value3"
computer$ java $JAVA_OPTS JavaOpts101
Property 1: value1
Property 2: value2
Property 3: value3

Doing it right, without grouping them in JAVA_OPTS:

computer$ java -Dproperty1=value1 -Dproperty2=value2 -Dproperty3=value3 JavaOpts101
Property 1: value1
Property 2: value2
Property 3: value3

Doing it right, grouping two and passing the third one:

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2"
computer$ java $JAVA_OPTS -Dproperty3=value3 JavaOpts101
Property 1: value1
Property 2: value2
Property 3: value3

So how do you do it wrong?
Basically, you need to be passing the properties in the wrong place, like this:

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2"
computer$ java $JAVA_OPTS JavaOpts101 -Dproperty3=value3
Property 1: value1
Property 2: value2
Property 3: null

Even if they are grouped on JAVA_OPTS, they won’t be correctly passed unless they go before the class name…

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2 -Dproperty3=value3"
computer$ java JavaOpts101 $JAVA_OPTS
Property 1: null
Property 2: null
Property 3: null

A little bit of documentation:

If you do man java on your command-line and have a read, apart from discovering a new spelling for impletmentations (sic), you’ll get something along this:

SYNOPSIS
  java [ options ] class [ argument... ]

  java [ options ] -jar file.jar
    [ argument... ]
...
...
OPTIONS
...
...
  -Dproperty=value
    Sets a system property value.

A couple of caveats:

Notice that you only need to use the quotes when you group the system properties under an environment variable (in these examples, JAVA_OPTS), but you don’t need to use the quotes when the properties are passed directly on the command line.
It may be worthy to note here: the order in which the properties are passed among themselves is irrelevant.

The code is on Github, even if it’s completely silly :/

AngularJS fundamentals

If you’re a backend focused developer as I’ve been in the last few years, but you can never get rid of that “I know too litle about frontend development” feeling from time to time, I guess it would be as good for you as it’s been for me to watch this video on Angular JS:

Sometimes it’s not even about the message, but about the messenger. In the case of this video, both message and messenger are really clear and easy to follow. I had no clue about Angular JS before I watched this video, and I probably still have no clue about what real life development cycle or day to day basis is working with it, but I understand now the efforts the frontend developers go through in order to do their work, and how frameworks like Angular JS may alleviate some that pain.

Gauge vs Counter

Let’s say you need to keep track of certain values on your application, to decide whether is under-performing or achieving any given intended goal, etc… so you think about what you need to measure and decide to insert some KPI’s in your codebase.

gauge

Now think about what you need to measure… is it something that increments in time, or is it something that may increment and decrement? is it something which value depends on previous values, or is it something where we only care about the value on the moment of measurement, which won’t very likely be related to its value maybe a couple of minutes before?

In the first case you will need a counter. In the second, a gauge. Why do I make this distinction?, why is it important?, why are you probably reading this?, basically because any single monitoring software I’ve used make this distinction when creating a metric or defining an alarm. Zenoss, when defining a MIB; Nagios, when specifying the data source type

Ok, yeah, very nice. But very theoretical. Give me some examples so I can understand this better.

There you go then. Examples:

– Number of concurrent visitors in a website: that’d be a gauge, we’re only interested in the number of visitors now browsing a website; grab it, record it, keep moving.

– Number of visitors in the last five minutes in a website: that’d be a counter, plus some basic arithmetics (current value – value five minutes ago).

– Memory use: gauge. We’ll send info on % of memory use at the time when the info is requested.

– CPU use: gauge, as the previous one.

– Number of requests: counter.

There is a bit of complication when you want to measure how many times something happens in certain amount of time, since you need a counter to track down the number of occurrences of the event, but you need to keep track of the previous value of the counter when the time interval comes to an end. To make your life easier, some libraries written to help you track down values for your KPI’s contemplate a third type of metric, called, precisely, meters… but that’s getting out of the scope of the present post.