I need to get messages from a service in a given interval.
I make a request to my service, where I specify FROM date and TO date. But the service starts from the FROM date, and goes up to 2000 messages and then it returns me those 2000 messages, no more, even if the TO date is not reached.
So if I want to get all the messages, I have to check every response and if there are 2000 messages, then I need to make another request where I specify the FROM date as the date of the last message returned. And so on until I get to the TO date.
Let's say the method looks like this:
public void getMessages(Date from, Date to, Callback<List<Message>> onSuccess);
So I tried to write it like so:
List<Message> allMessages = new ArrayList<>();
getMessages(fromDate, toDate, messages -> {
//save all the messages returned
allMessages.addAll(messages);
if(messages.size()==2000){
//get the last message date
Date nextDate = messages.get(messages.size() - 1).getDateTime();
//query next messages
getMessages(nextDate, to, onSuccess); //and here I am screwed in what to put in the onSuccess again...
}
});
How to do it so it would continue certain number of steps, or stop when it finally reaches the TO date? I can even change the getMessages() method, but I get the data from HTTP request, so it needs to be done via Callback.
UPDATE: Thanks for all the answers, based on them I came up with a simple solution.
I created a method like this:
private void checkMessages(List<Message> messages, Date toDate, List<Message> allMessages) {
// save all the messages returned
allMessages.addAll(messages);
if (messages.size() == 2000) {
// get the last message date
Date nextDate = messages.get(messages.size() - 1).getDateTime();
// query next messages
getMessages(nextDate, toDate,
msgs -> checkMessages(msgs, toDate, allMessages));
} else {
// else we are finished
messageTable.setMessages(allMessages);
}
}
and the I used this method in the callback:
List<Message> allMessages = new ArrayList<>();
getMessages(fromDate, toDate,
messages -> checkMessages(messages, toDate, allMessages));
It's definitely not ideal, but it seems to be working.