com.facebook.ads.sdk.serverside.BatchProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of facebook-java-business-sdk Show documentation
Show all versions of facebook-java-business-sdk Show documentation
Facebook Business Solutions SDK for Java
package com.facebook.ads.sdk.serverside;
import com.facebook.ads.sdk.APIException;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class BatchProcessor {
private int batchSize;
private int concurrentRequests;
public BatchProcessor(int batchSize, int concurrentRequests) {
this.batchSize = batchSize;
this.concurrentRequests = concurrentRequests;
}
public void processEventRequests(List eventRequests) throws ExecutionException, InterruptedException {
Iterator>> it = processEventRequestsIterator(eventRequests);
while (it.hasNext()) {
List> futures = it.next();
Futures
.allAsList(futures)
.get();
}
}
public Iterator>> processEventsIterator(EventRequest eventRequestToClone, List events) {
return new EventIterator(eventRequestToClone, events, this.batchSize, this.concurrentRequests);
}
public void processEvents(EventRequest eventRequestToClone, List events) throws ExecutionException, InterruptedException {
Iterator>> it = processEventsIterator(eventRequestToClone, events);
while (it.hasNext()) {
List> futures = it.next();
Futures
.allAsList(futures)
.get();
}
}
public Iterator>> processEventRequestsIterator(List eventRequests) {
return new EventRequestIterator(eventRequests, this.concurrentRequests);
}
protected static class EventIterator implements Iterator>> {
private final EventRequest eventRequestToClone;
private final List events;
int concurrentRequests;
int batchSize;
int index;
public EventIterator(EventRequest eventRequestToClone, List events, int batchSize, int concurrentRequests) {
this.eventRequestToClone = eventRequestToClone;
this.events = events;
this.batchSize = batchSize;
this.concurrentRequests = concurrentRequests;
this.index = 0;
}
public boolean hasNext() {
return index < events.size();
}
public List> next() {
List> responses = new ArrayList>();
int i = index;
while (i < events.size() && responses.size() < concurrentRequests) {
EventRequest eventRequest = eventRequestToClone.cloneWithoutData();
int maxIndex = Math.min(i + batchSize, events.size());
eventRequest.setData(events.subList(i, maxIndex));
try {
responses.add(
eventRequest.executeAsync()
);
} catch (APIException e) {
e.printStackTrace();
}
i += batchSize;
}
this.index = i;
return responses;
}
public void remove() {
}
}
protected static class EventRequestIterator implements Iterator>> {
private final List eventRequests;
int concurrentRequests;
int index;
public EventRequestIterator(List eventRequests, int concurrentRequests) {
this.eventRequests = eventRequests;
this.concurrentRequests = concurrentRequests;
this.index = 0;
}
public boolean hasNext() {
return index < eventRequests.size();
}
public List> next() {
List> responses = new ArrayList>();
int i = index;
while (i < (index + concurrentRequests) && i < eventRequests.size()) {
try {
responses.add(
eventRequests.get(i).executeAsync()
);
} catch (APIException e) {
e.printStackTrace();
}
i++;
}
this.index = i;
return responses;
}
public void remove() {
}
}
}