1
votes

I've successfully implemented (using the java client library) authentication and retrieval of calendars. My problem is, I want to retrieve a list of events from multiple calendars, but the time it takes to send an individual API request for each calendar starts to add up, resulting in 20 second page load times if I have a lot of calendars enabled. I understand that the API doesn't provide a way to request events from multiple calendars, but can I use the BatchRequest class to group my requests?

I've been able to get this example to work to insert multiple calendars in batch.

https://code.google.com/p/google-api-java-client/source/browse/calendar-cmdline-sample/src/main/java/com/google/api/services/samples/calendar/cmdline/CalendarSample.java?repo=samples

But I can't extend this to listing events. Essentially, I want to do this:

for (int i=0; i < calendarIDs.length; i++) {
    client.events().list(calendarIDs[i]).queue(batch, callback);
}
batch.execute();
2

2 Answers

1
votes

I just finally figured it out using a slightly different approach. Rather than using the queue function for the list object, using the client's batch.queue function:

BatchRequest batch = client.batch();            
for (int i=0; i< calendarIDs.length; i++) {    
    List req = client.events().list( calendarIDs[i]);        
    batch.queue(req.buildHttpRequest(), Calendar.class, GoogleJsonErrorContainer.class, mycallback);
}           
batch.execute();

This seems to have improved performance a lot.

0
votes

I'm doing something that look like what you want, but i'm not using batch, here is an example of my code :

CalendarEventFeed resultFeed = service.getFeed(calendarUrl, CalendarEventFeed.class); 

for (int i = 0; i < resultFeed.getEntries().size(); i++) { 
     CalendarEventEntry entry = resultFeed.getEntries().get(i); 
} 

So the only thing you will need to change to get event from different calendar is the "calendarUrl", this how you can get your calendar URL

calendarUrl = new URL(calendar.getLink(Link.Rel.ALTERNATE, Link.Type.ATOM).getHref()); 

where "calendar" is a CalendarEntry from google lib.