Skip to content

Get and Process Events

Nick Kasprzak edited this page Jun 5, 2019 · 1 revision

Introduction

In this guide I will show you how to use our Java SDK to get and process events in real time.

To follow along with this example git clone the project and cd into the get-events directory.

Before using the API we need to login and get make a Client object. You can find out how to do that here.

Getting events

First we must instantiate the EventResource like this:

EventResource eventResource = apiClient.getEventResource();

Here we can see functions in EventResource that we can use.

We are going to use getEvents for this example. As you can see it takes a parameter an object called EventFilterParams.

Building this object is key to getting the events you want. There is a lot of ways to customize this object to get the specific type of events that you want.

We will continuously poll with SDK in a specific interval and then process the events.

To do this I use a while loop that will run forever unless you kill the java process or an error occurs.

EventFilterParams

This object is doing most of the work here for us. Since it is very customizable we can query for the exact events we want. In the following code is an example of some of the customization you can do:

final EventFilterParams eventFilterParams =
            new EventFilterParams(EntityType.COMPANY, COMPANY_ID);
        final Instant currentTime = Instant.now();
        final Instant fiveSecondsAgo = Instant.now().minus(5, ChronoUnit.SECONDS);
        eventFilterParams.setIncludeDescendantEvents(true);
        eventFilterParams.setTimestampAfter(fiveSecondsAgo.toEpochMilli());
        eventFilterParams.setTimestampBefore(currentTime.toEpochMilli());

Here is the documentation of EventFilterParams to see everything you can do.

EventResponse

So after we construct the EventFilterParams we can call the function getEvents(eventFilterParams). We get an object back called EventListResponse which is a list of EventResponse.

We iterate through that last and check to see if the events we got back are the type of events we are looking for. We do that with this code:

if (EntityType.IAAS_VM.equals(event.getEntityType()) && vmEvents
              .contains(event.getType())
              || EntityType.IAAS_ORGANIZATION.equals(event.getEntityType())
              && orgEvents.contains(event.getType()))

We log basic information about the event if it matches what we want, though we have the ability to process the EventResponse in more complex ways.

The documentation for the EventResponse is here.

After processing the events we get back we wait for 5 seconds using Thread.sleep which uses milliseconds as a parameter.