Is it possible to get a truly unique id for a particular JVM instance?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 11:42:45

You could generate a UUID at the start of your program and use that during the time the program is running.

UUID id = UUID.randomUUID();

Besides the method randomUUID() there are other methods to generate UUIDs that might fit your needs better, see the API documentation of class java.util.UUID for more information.

You could create a UUID on application startup and use that for identification.

UUID UniqueID = UUID.randomUUID();

If UUIDs are too heavy-weight, you need to review what you mean by "uniquely and permanently".

For example, if you were to limit "unique and permanent" to mean unique for machines on a given network, and permanent for the next (say) 10 years, you could use the same approach used in UUID generation but with fewer bits.

Alternatively, if you were to create a unique id generation service with a persistent store, and tie the notion of "permanent" to the lifetime of that store, then the unique ids could simply be an incrementing sequence of integers.

Why not use Singleton holding instance of java.rmi.server.UID?

I do not know of a standard library for this, but you can get far on your own with the start time (do some calculations with new java.util.Date() and System.currentMillis), the path to the installation, and the path to the JVM (consider just concatenating all System.getProperties()).

Do you have a central server these can talk to? Then a uuid can be retreived from the central server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!