简体   繁体   English

来自JVM的唯一ID,用于跟踪目的

[英]Unique ID from a JVM for tracking purposes

Lets say you have a machine with multiple JVMs on it with different configurations. 假设你有一台机器上有多个JVM,配置不同。 Is there a way to get a GUID/UUID from each one to pass into the app running on it? 有没有办法从每个GUID / UUID传入其中运行的应用程序? Not instances of the JVM but specific JVM that launched process. 不是JVM的实例,而是启动进程的特定JVM。 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). 我可以编写一个程序来查找计算机上的所有JVM,并为它们提供一些元数据,以便在ClassLoader阶段(或其他东西)进入应用程序。

I have been over in .NET space for a while and forget what might be the simplest way to solve this issue in Java. 我已经在.NET领域工作了一段时间,并忘记了在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. 如果已经在Java中构建了可以为每个JVM做到这一点的东西,那么如果不是,我愿意去单独的分配阶段路由。

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. 最终目标是流经系统的每一个事务都将有一个与之关联的主GUID ......但是我还希望在执行该部分工作的JVM的“工作”中标记工作的每个部分。 Think multiple JVMs per machine and perhaps later... multiple machines. 考虑每台机器多个JVM,也许以后......多台机器。

Note: If users feel I used wrong Tags please edit them. 注意:如果用户觉得我使用了错误的标签,请编辑它们。 Thanks. 谢谢。

Here is a possible way to address your use case. 这是一种解决您的用例的可能方法。 We can specify JVM arguments where we can specify properties using the -D option. 我们可以指定JVM参数 ,我们可以使用-D选项指定属性。 These properties are then accessible as System properties across the entire lifespan of the JVM to all the classes. 然后,可以在JVM的整个生命周期内将这些属性作为系统属性访问到所有类。

So for example let's assume we launch a Java program like below: 例如,让我们假设我们启动如下的Java程序:

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: 现在,在Java程序中,只要您想要记录JVM的ID,您需要做的就是以下内容:

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. 请注意,您也可以在此处使用自己的可读字符串作为JVM ID,而不是UUID。

In case you want to associate a properties file with a JVM that contains various JVM properties; 如果要将属性文件与包含各种JVM属性的JVM关联; here is how you could do it. 这是你怎么做的。 Assume that the name of the properties file is MyProgram.properties . 假设属性文件的名称是MyProgram.properties

We launch the JVM as below 我们按如下方式启动JVM

java MyProgram -DPROPERTIES_FILE=MyProgram.properties

You can then load the properties file as below. 然后,您可以加载属性文件,如下所示。 This has been taken from the Oracle docs : 这取自Oracle文档

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"); 通过在main()完成上述操作,您可以通过简单地调用System.getProperty("propertyName");来访问Java程序中任何位置所需的任何System.getProperty("propertyName");

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM