Configure the Java SDK event dispatcher
The Optimizely Feature Experimentation SDKs make HTTP requests for every decision event or conversion eventdecision event or conversion event that gets triggered. Each SDK has a built-in event dispatcher for handling these events, but you should overriding it based on the specifics of your environment.
The Java SDK has an out-of-the-box asynchronous dispatcher. You should customizing the event dispatcher you use in production to ensure that you queue and send events in a manner that scales to the volumes handled by your application. Customizing the event dispatcher lets you take advantage of features like batching, which makes it easier to handle large event volumes efficiently or to implement retry logic when a request fails. You can build your dispatcher from scratch or start with the provided dispatcher.
The examples show that to customize the event dispatcher, initialize the Optimizely client (or manager) with an event dispatcher instance.
import com.optimizely.ab.Optimizely;
import com.optimizely.ab.event.AsyncEventHandler;
import com.optimizely.ab.event.EventHandler;
/**
* Creates an async event handler with a max buffer of
* 20,000 events and a single dispatcher thread
*/
EventHandler eventHandler = new AsyncEventHandler(20000, 1);
// Create an Optimizely client with the default event dispatcher.
// build() does not throw on an invalid datafile; parse errors go to the error handler.
Optimizely optimizelyClient = Optimizely.builder(datafile,
eventHandler).build();The event dispatcher should implement a dispatchEvent method, which takes a single LogEvent argument created by the SDK. In this method, you should send a POST request to LogEvent.getEndpointUrl() using LogEvent.getBody() as the body of the request and {content-type: 'application/json'} in the headers.
Important If you are using a custom event dispatcher, do not modify the event payload returned from Optimizely. Modifying this payload will alter your results.
Optimizely provides an asynchronous event dispatcher, optimizely-sdk-httpclient, that requires org.apache.httpcomponents:httpclient:4.5.2 and is parameterized by in-memory queue size and number of dispatch worker threads.
To use your own event dispatcher, implement the dispatchEvent method in the EventHandler interface. dispatchEvent takes in a LogEvent object containing all of the information needed to dispatch an event to the Optimizely backend. Events should be sent to LogEvent.getEndpointUrl as a POST request with LogEvent.getBody as the payload and content-type: "application/json" as the header.