Endless loop when iterating results in SDK - workaround

Follow

Originally posted by ricardo.marques on 2012-03-21 17:20:13

We received the following request on support:

With the following code snippet causes endless loop because nextPage object is always valid even though next page does not contain any events. HTTP log which is enclosed shows better this problem.

Code snippet:


EventFilter filter = new EventFilter().byType("TestEvent").bySource(mo);
try {
PagedCollectionResource eventCollection = m_platform.getEventApi().getEventsByFilter(filter);
EventCollectionRepresentation eventPage = null;

eventPage = eventCollection.get();
for (; eventPage != null; eventPage = eventCollection.getNextPage(eventPage)) {
for (EventRepresentation event: eventPage.getEvents()) { System.out.println("Event ID: " + event.getId().toString()); }
}
} catch (SDKException e) { // TODO Auto-generated catch block e.printStackTrace(); }

User is using 0.13.1 Cumulocity SDK.

We know there is a problem with the implementation, due to the way the underlying database performs the queries. This problem affects all API (inventory, event, measurement, …), and is being tracked internally as MTM-2986.

Meanwhile, there is one workaround that you can use, which consists on checking if the returned page has no events. It is similar to this:


EventFilter filter = new EventFilter().byType("TestEvent").bySource(mo);
try {
    PagedCollectionResource eventCollection = m_platform.getEventApi().getEventsByFilter(filter);
    EventCollectionRepresentation eventPage = null;

    eventPage = eventCollection.get();
    for (; eventPage != null && eventPage.getEvents().size() > 0; eventPage = eventCollection.getNextPage(eventPage)) {
        for (EventRepresentation event: eventPage.getEvents()) { System.out.println("Event ID: " + event.getId().toString()); }
    }
} catch (SDKException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Have more questions? Submit a request

Comments