Java Requires Chilkat v11.5.0+
Chilkat2 Java
Box: Poll User Events and Save a Checkpoint
See more Box Examples
Poll the authenticated user event stream in bounded batches. The local JSON checkpoint keeps processed event IDs and the next stream position. This single-process demonstration logs events; a production worker should commit its business action, deduplication record, and cursor transactionally and prune old IDs.
See the Box API guide for setup and the complete workflow.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.HttpCurl;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false;
// Supply an unexpired token obtained with one of the Box authentication examples.
// A token represents a particular user or service account. IDs must be accessible to it.
HttpCurl curl = new HttpCurl();
curl.setVar("access_token", "BOX_ACCESS_TOKEN");
JsonObject state = new JsonObject();
FileAccess files = new FileAccess();
String statePath = "qa_data/box_events_state.json";
if (files.fileExists(statePath)) {
try {
state.loadFile(statePath);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if (!state.hasMember("position")) {
System.out.println("Invalid checkpoint: position missing.");
return;
}
} else {
// "now" starts a new subscription. It deliberately skips historical events.
state.updateString("position", "now");
}
curl.setVar("position", state.stringOf("position"));
JsonObject page = new JsonObject();
StringBuilder seenPath = new StringBuilder();
String eventId = "";
String nextPosition = "";
int batch = 0;
int i = 0;
int count = 0;
boolean more = true;
while ((more) && (batch < 20)) {
curl.clearTargetOutput("*");
StringBuilder authorizedCommand1 = new StringBuilder();
authorizedCommand1.setString("curl -X GET \"https://api.box.com/2.0/events?stream_type=all&stream_position={{position}}&limit=100\" -H \"Authorization: Bearer {{access_token}}\"");
int headerReplacement1 = authorizedCommand1.replace("{{access_token}}", curl.getVar("access_token"));
try {
curl.doYourThing(authorizedCommand1.getAsString());
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (curl.getStatusCode() != 200)) {
System.out.println("Box request failed. HTTP status: " + curl.getStatusCode());
System.out.println("Failure reason: " + curl.getFailReason());
System.out.println(curl.getResponseBodyStr());
return;
}
try {
curl.getResponseJson(page);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
count = page.sizeOfArray("entries");
i = 0;
while (i < count) {
page.setI(i);
eventId = page.stringOf("entries[i].event_id");
if ("".equals(eventId)) {
System.out.println("Event without an event_id; checkpoint not advanced.");
return;
}
seenPath.clear();
seenPath.append("seen.");
seenPath.append(eventId);
if (!state.hasMember(seenPath.getAsString())) {
// Replace this logging with your idempotent business operation.
// Record success only after that operation succeeds.
System.out.println(eventId + " " + page.stringOf("entries[i].event_type") + " " + page.stringOf("entries[i].source.id"));
state.updateBool(seenPath.getAsString(), true);
}
i = i + 1;
}
nextPosition = page.stringOf("next_stream_position");
if ("".equals(nextPosition)) {
System.out.println("Missing next_stream_position; checkpoint not advanced.");
return;
}
more = (count > 0) && (!nextPosition.equals(curl.getVar("position")));
state.updateString("position", nextPosition);
try {
state.writeFile(statePath);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
curl.setVar("position", nextPosition);
batch = batch + 1;
}
System.out.println("Checkpoint saved. Run again later to continue polling.");
}
}