Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:39:48+00:00 2026-06-12T23:39:48+00:00

I have requirement in which I continuously receive messages that needs to be written

  • 0

I have requirement in which I continuously receive messages that needs to be written in a file. Every time a new message is received it needs to be written in a separate file. What I want is to generate an unique identifier to be used as a file-name. I also want to preserve the order of the messages as well. By this I mean, the identifier generated as a file-name should always be incremental.

I was using UUID.randomUUID() to generate file-names but the problem with this approach is that UUID only assures randomness of the identifier but is not incremental. As a result I am losing the ordering of the file (I want file generated first should appear first in the list).

Approaches known

  1. Can use System.currentTimeMillis() but I can receive multiple messages at same time stamp.

2.Another approach could be to implement static long value and increment it whenever a file is to be created and use the long value as a file-name. But I am not sure about this approach. Also it doesn’t seem to be a proper solution to my problem. I think there could be far better solutions than this one.

If someone could suggest me a better solution to this problem, will be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:39:49+00:00Added an answer on June 12, 2026 at 11:39 pm

    If you want your id value to uniformly rise even between server restarts, then you must either base it on the system time or have some elaborately robust logic that persists the last ID used. Note that achieving robustness on its own is not hard, but achieving it in a performant and scalable way is.

    If you additionally need the id to be unique across multiple nodes in a redundant server cluster, then you need even more elaborate logic, which definitely involves a persistent store to which all the boxes synchronize access. Making this performant is, of course, even harder.

    The best option I can see is to have a quite long ID so there’s room for these parts:

    1. System.currentTimeMillis for long-term uniqueness (across restarts);
    2. System.nanotime for finer granularity;
    3. a unique id of each server node (determined in a platform-specific way).

    The method will still have to remember the last value generated and retry in case of a duplicate. It won’t have to retry too many times, though, just until the next nanoTime clock tick—it could even busy-wait for it.

    Sketch of code without point 3 (single-node implementation):

    private static long lastNanos;
    public static synchronized String uniqueId() {
      for (;/*ever*/;) {
        final long n = System.nanoTime();
        if (n == lastNanos) continue;
        lastNanos = n;
        return "" + System.currentTimeMillis() + n;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to JQgrid. I have a requirement which i'm not able to
I have a requirement which needs to go to a webpage and take a
I am new to IOS development.. I have a requirement in which i need
I have requirement for an application which work in background only. When first time
I have a requirement which says I have to create only once class file
i have a requirement in which i have a container div that contains 3
I have requirement to build new rule engine in which i have rule like
I have a requirement in which I have to check whether any file exists
I am new to Ruby and Rails . I have a requirement in which
I have a requirement which sounds like kind of simple but hard to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.