I need to generate a number from 10000 for a field. everytime user starts a new case, it should increment +1. So if first case has a number 10000, next should be 10001,10002… I am using the following code but i get an error when i use it.
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger atomicInteger=new AtomicInteger(10000);
int b= atomicInteger.get();
return atomicInteger.getandIncrement(b);
Error :
No signature of method: java.util.concurrent.atomic.AtomicInteger.getandIncrement() is applicable for arguement types( java.lang.integer) values:10000
Possible solutions: getandIncrement() and getandDecrement()
groovy.lang.missing method exception.
Could anyone give me an idea?
Regards
A.
First things first,
I would suggest this number is a business object number rather than a BPM case number, for example and invoice number or purchase order number. A BPM case number is the number assigned to a case and is not the same thing.
My first concern is where do you get the number from? Do you store it anywhere? If not then you could find you will generate duplicates when, for example the portal is restarted, will you start again at 10000 or the last number used? If it’s hardcoded as in your sample above then you will get duplicate Business Object numbers, probably not what you want.
A common method of doing this is to have a one column/row table in a database to store it.
This way you would literally do this:
Get number from Database with lock,
Increment by one
Store Back to Database and unlock
this way rather than using an Atomicnumber you just use it as an integer…
HOWEVER to address your realy problem…
return atomicInteger.getandIncrement(b); should be
return atomicInteger.getAndIncrement(b);
The and should be And
Don;t worry I’ve been there and done it too…
Hope I’ve given you food for thought on the use of the BO number, be careful sometimes they come back to haunt you.
regards
Sean
Thanks Sean. Now the error is gone.
But the other doubt i have here is this:
Like you mentioned.Yes it a business object number( a unique form number), say for a form ; the form number is 10000 when i fill in the form.
A new user comes. He wants to fill in the details. So the new form will have form number:10001… and then goes on like that,
But using the atomic integer code., it does not increment the to the next number.( remains 10000).
Any suggestions?
Regards,
A.
Like I said, you need to store this number outside the code, in a database and then increment and update the database accordingly.
This is one of those infamous issues in software engineering, getting a record, updating it and saving it, especially when others may also be using the same system. Hotels are a great one for reservations, airlines also.
Simply put your connector needs to be rewritten to get the start number from DBase or XML and then you need to rewrite it immediately afterwards
So for example in a DB connector you could do, all in one connector:
var no_
Select no_ from no_table where bo_ is "thisbo";
no_++;
Update no_table where bo_ is "thisbo" value(no_);
return No_
Note the above is pseudo code and will change based on Database in use. You may also want to lock the record for the duration of the Select, add and update, to ensure the record isn’t overwritten/used by someone else, again database dependent.
This will work for X number of users all at the same time and give you unique numbers.
Hope this helps, regards
Thanks Sean for your quick response.
Best is to use the method you mentioned. I shall try it out.
Regards
A.