'Use Async or paging to fetch all devices in Microsoft graph sdk

I am fetching around 61 thousand devices using Microsoft graph SDK.

  • Spring boot version: 2.3.7
  • Graph SDK: 5.14.0

Code:

private void getAllDevices(){
    List<Device> devicesList = new ArrayList<>();
    this.graphClient.devices().buildRequest().getAsync().thenApply(res->{
        devicesList.addAll(res.pgetCurrentPage());
        getNextPage(devicesList, res);
        return devicesList;
    }).thenAccept(devices->{
        System.out.println("Devices size: "+devices.size());
    });
}

private void getNextPage(List<Device> devicesList, DeviceCollectionPage page){
    if(page !=null && page.getNextPage() != null){
        page.getNextPage().buildRequest().getAsync().thenApply(res->{
            devicesList.addAll(res.pgetCurrentPage());
            getNextPage(devicesList, res);
            return devicesList;
        });
    }else{
        System.out.println("Page END");
    }
}

I have followed this document. The main thread continues execution after starting this request and it's taking around 45-60 OR more mins to fetch all devices.

I want to improve the time required for fetching devices, is there any way where I can start fetching devices on multiple pages?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source