Monitor a Bulk Operation
This guide demonstrates how to upload Contacts using the Complero Java SDK and check the status of the Operation.
src/main/java/com/complero/examples/MonitorBulkOperations.java
package com.complero.examples;
import com.complero.api.ContactsApi;
import com.complero.api.OperationsApi;
import com.complero.api.models.Address;
import com.complero.api.models.ContactUpload;
import com.complero.api.models.Operation;
import com.complero.api.models.Section;
import com.complero.invoker.ApiClient;
import com.complero.invoker.ApiException;
import com.complero.invoker.ApiResponse;
import com.complero.invoker.auth.ApiKeyAuth;
import com.complero.invoker.auth.Authentication;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MonitorBulkOperations {
public static void main(String[] args) {
ApiClient apiClient = createApiClient(System.getenv("API_KEY"));
List<ContactUpload> contacts = collectContactsToUpload();
try {
String operationId = uploadContacts(contacts, apiClient);
// upload was started asynchronously, lets investigate the ongoing Operation
Operation operation = getOperation(operationId, apiClient);
// processing should be completed, lets look at the Operation
System.out.println(operation.getId());
System.out.println(operation.getStatus());
System.out.println(operation.getType());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
contacts = collectFaultyContactToUpload();
// Upload failing contacts
try {
String operationId = uploadContacts(contacts, apiClient);
Operation operation = getOperation(operationId, apiClient);
System.out.println(operation.getId());
System.out.println(operation.getStatus());
System.out.println(operation.getType());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
static ApiClient createApiClient(String apiKey) {
ApiClient apiClient = new ApiClient();
// set auth
Authentication auth = apiClient.getAuthentications().get("ApiKeyAuth");
((ApiKeyAuth) auth).setApiKey("Bearer ".concat(apiKey));
return apiClient;
}
static List<ContactUpload> collectContactsToUpload() {
List<ContactUpload> uploadContacts = new ArrayList<>();
ContactUpload contact1 = new ContactUpload();
contact1.setFirstName("Max");
contact1.setLastName("Berg");
contact1.setId("api-1");
contact1.setEmployeeId("1");
uploadContacts.add(contact1);
ContactUpload contact2 = new ContactUpload();
Section section = new Section();
Address address = new Address();
contact2.setFirstName("Petra");
contact2.setLastName("Bauer");
contact2.setId("api-2");
contact2.setEmployeeId("1");
address.setZipCode("53111");
address.setCountry("DE");
address.setCity("Bonn");
address.setStreet("Friedensplatz");
section.addAddressesItem(address);
contact2.setSectionPrivate(section);
uploadContacts.add(contact2);
return uploadContacts;
}
static List<ContactUpload> collectFaultyContactToUpload() {
ContactUpload contact = new ContactUpload();
contact.setFirstName("Pete");
contact.setLastName("FaultyChar!ns!d3OfLastn@me");
contact.setId("api-3");
contact.setEmployeeId("1");
List<ContactUpload> uploadList = new ArrayList<>();
uploadList.add(contact);
return uploadList;
}
static String uploadContacts(List<ContactUpload> contacts, ApiClient apiClient) {
ContactsApi contactsApi = new ContactsApi(apiClient);
// Uploading contacts
ApiResponse<Void> apiResponse;
try {
apiResponse = contactsApi.bulkUploadContactsWithHttpInfo(contacts);
} catch (ApiException e) {
e.printStackTrace();
throw new RuntimeException("Error when uploading contacts", e);
}
System.out.println("HTTP POST request succeeded! Status Code: " + apiResponse.getStatusCode());
System.out.println("Headers: " + apiResponse.getHeaders());
Map<String, List<String>> headers = apiResponse.getHeaders();
String operationID = headers.get("operation-id").get(0);
return operationID;
}
static Operation getOperation(String operationID, ApiClient apiClient) {
OperationsApi operationsApi = new OperationsApi(apiClient);
Operation operation;
boolean statusChecking = true;
do {
try {
operation = operationsApi.getOperation(operationID);
} catch (ApiException ex) {
throw new RuntimeException("Error during HTTP GET request: " + ex.getMessage());
}
switch (Objects.requireNonNull(operation.getStatus())) {
case "pending":
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
break;
case "success":
statusChecking = false;
break;
case "error":
statusChecking = false;
break;
default:
throw new RuntimeException("Unknown status: " + operation.getStatus());
}
} while (statusChecking);
return operation;
}
}
- Command
- Output
mvn clean install exec:java -Dexec.mainClass="com.complero.examples.MonitorBulkOperations"
...
HTTP POST request succeeded! Status Code: 202
Headers: {content-length=[0], date=[Tue, 30 Sep 2025 10:37:40 GMT], operation-id=[2e445521-9abf-42d0-b90e-eb11f3ed802e], operation-link=[https://api.complero.com/v1/operations/2e445521-9abf-42d0-b90e-eb11f3ed802e], request-id=[DDhljVfucdfYxIscQseQGrdhaZdnvvEs], strict-transport-security=[max-age=15724800; includeSubDomains], vary=[Accept-Encoding, Origin]}
2e445521-9abf-42d0-b90e-eb11f3ed802e
success
import
com.complero.invoker.ApiException: Message:
HTTP response code: 422
HTTP response body: {"message":"there was a problem with the validation of the following","validation_errors":{"api-3":{"last_name":{"code":"v1c003","message":"name is not a valid"}}}}
HTTP response headers: {content-type=[application/json], date=[Tue, 30 Sep 2025 10:37:44 GMT], request-id=[SRPQzQTbTLJWMkYoBYKBTmlpHYXMFAPx], strict-transport-security=[max-age=15724800; includeSubDomains], vary=[Accept-Encoding, Origin]}
at com.complero.invoker.ApiClient.handleResponse(ApiClient.java:1131)
at com.complero.invoker.ApiClient.execute(ApiClient.java:1044)
at com.complero.invoker.ApiClient.execute(ApiClient.java:1027)
at com.complero.api.ContactsApi.bulkUploadContactsWithHttpInfo(ContactsApi.java:187)
at com.complero.examples.MonitorBulkOperations.uploadContacts(MonitorBulkOperations.java:112)
at com.complero.examples.MonitorBulkOperations.main(MonitorBulkOperations.java:43)
at org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:371)
at org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:289)
at java.base/java.lang.Thread.run(Thread.java:1447)
Error: Error when uploading contacts
Warning: thread Thread[#35,OkHttp TaskRunner,5,com.complero.examples.MonitorBulkOperations] was interrupted but is still alive after waiting at least 15000msecs
Warning: thread Thread[#35,OkHttp TaskRunner,5,com.complero.examples.MonitorBulkOperations] will linger despite being asked to die via interruption
Warning: thread Thread[#36,OkHttp api.complero.com,5,com.complero.examples.MonitorBulkOperations] will linger despite being asked to die via interruption
Warning: thread Thread[#37,OkHttp TaskRunner,5,com.complero.examples.MonitorBulkOperations] will linger despite being asked to die via interruption
Warning: thread Thread[#38,Okio Watchdog,5,com.complero.examples.MonitorBulkOperations] will linger despite being asked to die via interruption
Warning: thread Thread[#39,OkHttp TaskRunner,5,com.complero.examples.MonitorBulkOperations] will linger despite being asked to die via interruption
Warning: NOTE: 5 thread(s) did not finish despite being asked to via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.748 s
[INFO] Finished at: 2025-09-30T10:37:59Z
[INFO] ------------------------------------------------------------------------