Skip to main content

Monitor a Bulk Operation

This guide demonstrates how to upload Contacts using the Complero Java SDK and check the status of the Operation.

Below is a complete Java example that shows how to:

  1. Initialize the API clients and authenticate.
  2. Prepare Contacts with personal data.
  3. Perform a bulk upload of Contacts.
  4. Monitor the status of the Operation.

Full Example Code

package com.example.sdkclient;

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 App {
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());
}
}

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 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;

System.out.println("OperationID: " + operationID);

boolean statusChecking = true;
int tries = 0;
do {
try {
// inspect the progress of the batch operation
operation = operationsApi.getOperation(operationID);
} catch (ApiException ex) {
throw new RuntimeException("Error during HTTP GET request: " + ex.getMessage());
}
System.out.println("HTTP GET request succeeded! Status Code: " + operation.getStatus());

switch (Objects.requireNonNull(operation.getStatus())) {
case "pending":
// still in progress, let's try later
System.out.println("Status is 'pending'. Attempt number: " + ++tries);
sleep(3000);
break;

case "success":
System.out.println("Status is 'success'. Ending loop.");
statusChecking = false;
break;

case "error":
// api returned that an error occurred for the upload
throw new RuntimeException("Status is 'error'. Ending loop.");

default:
throw new RuntimeException("Unknown status: " + operation.getStatus());
}
} while (statusChecking);

return operation;
}

static void sleep(int ms) {
System.out.println("Repeating after " + ms + "ms...");
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}