private static final int MAX_RETRY_LIMIT = 5; private static final long BASE_WAIT_TIME = 2000; public static void runExample(DfpServices dfpServices, DfpSession session) throws Exception { int pageLimit = StatementBuilder.SUGGESTED_PAGE_LIMIT; // Get the LineItemService. LineItemServiceInterface lineItemService = dfpServices.get(session, LineItemServiceInterface.class); // Create a statement to select all line items. StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(pageLimit).offset(0); // Default for total result set size. int totalResultSetSize = 0, retryCount = 0; LineItemPage page = null; long waitTime = BASE_WAIT_TIME; do { // Reset these values for each page. retryCount = 0; pageLimit = StatementBuilder.SUGGESTED_PAGE_LIMIT; waitTime = BASE_WAIT_TIME; page = null; do { try { System.out.printf( "Getting line items with page size of %d at offset %d (attempt " + "%d)...\n", pageLimit, statementBuilder.getOffset(), retryCount + 1); // Get line items by statement. page = lineItemService.getLineItemsByStatement( statementBuilder.toStatement()); System.out.printf("Attempt %d succeeded.\n\n", retryCount + 1); } catch (Exception e) { pageLimit /= 2; System.out.printf( "Attempt %d failed. Retrying in %d milliseconds with page size " + "of %d...\n", retryCount + 1, waitTime, pageLimit); Thread.sleep(waitTime); waitTime *= 2; statementBuilder.limit(pageLimit); retryCount++; } } while (page == null && retryCount < MAX_RETRY_LIMIT); if (page != null) { totalResultSetSize = page.getTotalResultSetSize(); // Your code to handle the line item page, e.g., save it to a database. } else { throw new Exception(String.format( "Failed to get line items at offset %s with page size %d after " + "%d attempts.", statementBuilder.getOffset(), pageLimit, retryCount)); } statementBuilder.increaseOffsetBy(pageLimit); } while (statementBuilder.getOffset() < totalResultSetSize); System.out.printf("Number of results found: %d\n", totalResultSetSize); }
Getting line items with page size of 500 at offset 0 (attempt 1)... Attempt 1 failed. Retrying in 2000 milliseconds with page size of 250... Getting line items with page size of 250 at offset 0 (attempt 2)... Attempt 2 failed. Retrying in 4000 milliseconds with page size of 125... Getting line items with page size of 125 at offset 0 (attempt 3)... Attempt 3 failed. Retrying in 8000 milliseconds with page size of 62... Getting line items with page size of 62 at offset 0 (attempt 4)... Attempt 4 succeeded. Getting line items with page size of 500 at offset 62 (attempt 1)... Attempt 1 succeeded.
requestId