Requirement: I have a batch job that is processing 1 million records. I am storing the 1 million records in an Arraylist and iterating it to make a 3rd party external call for each record. Now the requirement is the third party will send a HTTP response 200 or 400 or 500. In the only case the response is 500, I have to update the database for that specific record.
Problem: To speed up processing I am trying to implement threading for the 3rd party call. But I am stuck with the fact that after implementing threading how can I process the response from the 3rd party call to make the database update. I don't want to include the DB update inside the thread because in case there are multiple threads trying to update the DB, there will be a DB deadlock.
My Effort: What I was trying is to declare a singleton arraylist and store the record number for which the response from the 3rd party call is 500 in the singleton object. When all the 3rd party call is complete I would iterate that singleton arraylist to fetch the records and update in the DB.
RoadBlock: Even in this case I am unable to figure out how can I make the threading sequential so that I can store the record in the singleton arraylist.
Code:
class callExtPrty implements Runnable{
public callExtPrty(String recordNumber)
this.recordNumber = recordNumber;
public void run(){
int response = externalCall(String recordNumber);
if response == 500
singletonList.add(recordNumber);
}
class recordProcessorDAO{
public void processRecords(){
List<String> dbRecordList= new ArrayList<String>();
//DB call to add 1 million records to dbRecordList
Iterator<String> recordList = dbRecordList.iterator();
while (recordList.hasNext()) {
new callExtPrty(recordList.next());
}
//Getting the singleton list populated by the 3rd party call
Iterator<String> singletonList = singletonList.iterator();
while (singletonList .hasNext()) {
//DB call to update the record fetched from singletonList
}
}
Can anyone help me in getting this designed in the proper way. Threading needs to be implemented for performance improvement as the job processes 1 million records in one go and the job runs for around 12-13 hours.
Thanks