-1

I need generate id, with generate-rule defined by me. For example:

  1. 0x-111-111-PX-2013-11-11-00001

  2. 0x-111-111-PX-2013-11-11-00002

It's easy to do it in a single jvm. Question is, I need cross jvm.

cross jvm means generate unique id from different machines

tshepang
  • 12,111
  • 21
  • 91
  • 136
MagiX
  • 1
  • 1
  • 5
  • further description needed for someone to help. Its not clear how you are generating currently and how you want to proceed – Crickcoder Nov 15 '13 at 06:13
  • I need cross jvm.... What it means on different machines or different versions of jvm? Do you need to generate unique id's from different machines????? – Deepak Bhatia Nov 15 '13 at 06:13
  • ye,cross jvm means generate unique id from different machines. in short, my application can be deploy on different host. – MagiX Nov 15 '13 at 06:17
  • so you need that all id generated from the different machine should be unique i.e. Id generated from one machine should not be same on other – Deepak Bhatia Nov 15 '13 at 06:21
  • yes,forgive my poor english,you are right @dbw – MagiX Nov 15 '13 at 06:28
  • Do the IDs need to be in any type of sequence? Do the JVMs have access to any shared resources such as a database? – emills Nov 15 '13 at 06:35

2 Answers2

2

Generating a random number or a random string (UUID) is always a challenge. Even usual java.util.Random is not perfect. You already ruled out using datbases which would have ensured the randomness across the JVMs.

This problem is on the similar to the challenge faced by web container to generate unique session id. The uniqueness is guaranteed as long as there is single JVM involved. But in real world scenario this is hardly the case. A typical web application is deployed across a cluster of servers. Based on the session id, the load balancer forwards the requests to the appropriate server.

Since the uniqueness of session id is not guaranteed across JVMs, each server appends a unique identifier (e.g. .node1) to the session id which makes sure that even if two server generate same session id, the uniqueness will be preserved by the appended string.

Extending this to your case, you can always put JVM specific identifier in the id, so the generated id might look like,

  1. 0x-111-111-PX-2013-11-11-00001-M1
  2. 0x-111-111-PX-2013-11-11-00001-M2

You can put M1/M2 anywhere in the string.

I am not aware of any compulsions (on IDs) you might be having, but this is just a thought.

Community
  • 1
  • 1
Santosh
  • 17,667
  • 4
  • 54
  • 79
1

1) You can use a database with a stored procedure which will generate a next id as you want

2) Java app (service) running for only id generation purpose accessible thru eg RMI

3) Shared file which other java apps will update uing the same algorithm

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275