3

Lets say you have a machine with multiple JVMs on it with different configurations. Is there a way to get a GUID/UUID from each one to pass into the app running on it? Not instances of the JVM but specific JVM that launched process. I'm ok with writing a program that seeks out all JVMs on a machine and gives them some metadata to pull into app during ClassLoader phase (or something).

I have been over in .NET space for a while and forget what might be the simplest way to solve this issue in Java. If there is something already built into Java that can do this per JVM great, if not I am willing to go the separate assigning phase route.

The end goal is every single transaction flowing through the system will have a main GUID associated with it... BUT I would also like to stamp each part of the work within a "job" with the JVM that did that part of the work. Think multiple JVMs per machine and perhaps later... multiple machines.

Note: If users feel I used wrong Tags please edit them. Thanks.

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • 1
    Does this help? http://stackoverflow.com/questions/8302634/how-to-obtain-unique-jvm-identifier – Prahalad Deshpande Oct 03 '16 at 18:23
  • The VMID route changes each time you ask for it. The server socket method mentioned is not exactly what I am looking for either. But valuable ideas to solve similar problems. – BuddyJoe Oct 04 '16 at 02:32
  • Is there a way to associate a .properties file with a JVM and not a .jar running underneath it? Is there a convention to be able to pass a GUID in every time the JVM starts? That could be replicated across 40 other machines easily? – BuddyJoe Oct 04 '16 at 02:38

1 Answers1

1

Here is a possible way to address your use case. We can specify JVM arguments where we can specify properties using the -D option. These properties are then accessible as System properties across the entire lifespan of the JVM to all the classes.

So for example let's assume we launch a Java program like below:

java MyProgram -DJVMID=A18342FB-68DD-4C64-B606-301366B8ABB0

Now within the Java program wherever you intend to log the ID of the JVM all you need to do is the below:

String jvmID = (String)(System.getProperty("JVMID");
//log the JVM ID
logger.log(jvmID + " : " + " Activity happened");

Please note that instead of a UUID you could also use your own readable string as a JVM id here.

In case you want to associate a properties file with a JVM that contains various JVM properties; here is how you could do it. Assume that the name of the properties file is MyProgram.properties.

We launch the JVM as below

java MyProgram -DPROPERTIES_FILE=MyProgram.properties

You can then load the properties file as below. This has been taken from the Oracle docs:

public static void main(String[] args)
{
    // set up new properties object
        // from file "myProperties.txt"
        String propertyFiletoLoad = (String)(System.getProperty("PROPERTIES_FILE"));
        FileInputStream propFile =
            new FileInputStream( propertyFileToLoad);
        Properties p =
            new Properties();
        p.load(propFile);

        // set the system properties
        System.setProperties(p);

}

With the above done in main() you would be able to access any property you require anywhere in the Java program by simply invoking System.getProperty("propertyName");

Hope this helps.

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22