When generating an ack message for a message I have received, I sometimes receive a NumberFormatException.
This is the code:
Message ack;
try {
ack = theMessage.generateACK();
return ack;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The issue is in the DelegatingHiLoGenerator:
@Override
protected int getNextHiId() throws IOException {
if (delegate == null)
throw new NullPointerException(
"Must initialize delegate IDGenerator");
return Integer.parseInt(delegate.getID());
}
In my case, the delegate is a FileBasedGenerator:
public String getID() throws IOException {
try {
lock.lock();
// If ID is 0, read initial value from file if possible if (!minimizeReads || !used) { long readInitialValue = readInitialValue(getFilePath()); if (readInitialValue >= 0) { set(readInitialValue); } used = true; } String id = super.getID(); // The id held in the file is always <increment> larger so that // the ID is still unique after a restart. writeNextValue(Long.parseLong(id) + getIncrement()); return id; } finally { lock.unlock(); } }
When generating an ID with the FileBasedGenerator, we are generating Long. But when trying to return an ID, we use Integer.parseInt(...)
When I run into this exception, the file has a large ID number, exceeding the limits of an integer and result in a NumberFormatException.
Fixed. Thanks for spotting this.