io.keen.client.java
Class KeenClient.Builder

java.lang.Object
  extended by io.keen.client.java.KeenClient.Builder
Enclosing class:
KeenClient

public abstract static class KeenClient.Builder
extends java.lang.Object

Builder class for instantiating Keen clients. Subclasses should override this and implement the getDefault* methods to provide new default behavior.

This builder doesn't include any default implementation for handling JSON serialization and de-serialization. Subclasses must provide one.

This builder defaults to using HttpURLConnection to handle HTTP requests.

To cache events in between batch uploads, this builder defaults to a RAM-based event store.

This builder defaults to a fixed thread pool (constructed with Executors.newFixedThreadPool(int)) to run asynchronous requests.


Constructor Summary
KeenClient.Builder()
           
 
Method Summary
 KeenClient build()
          Builds a new Keen client using the interfaces which have been specified explicitly on this builder instance via the set* or with* methods, or the default interfaces if none have been specified.
protected  KeenClient buildInstance()
          Builds an instance based on this builder.
protected  KeenEventStore getDefaultEventStore()
          Gets the default KeenEventStore to use if none is explicitly set for this builder.
protected  HttpHandler getDefaultHttpHandler()
          Gets the default HttpHandler to use if none is explicitly set for this builder.
protected abstract  KeenJsonHandler getDefaultJsonHandler()
          Gets the default KeenJsonHandler to use if none is explicitly set for this builder.
protected  java.util.concurrent.Executor getDefaultPublishExecutor()
          Gets the default Executor to use if none is explicitly set for this builder.
 KeenEventStore getEventStore()
          Gets the KeenEventStore that this builder is currently configured to use for storing events between batch publish operations.
 HttpHandler getHttpHandler()
          Gets the HttpHandler that this builder is currently configured to use for making HTTP requests.
 KeenJsonHandler getJsonHandler()
          Gets the KeenJsonHandler that this builder is currently configured to use for handling JSON operations.
 java.util.concurrent.Executor getPublishExecutor()
          Gets the Executor that this builder is currently configured to use for asynchronous publishing operations.
 void setEventStore(KeenEventStore eventStore)
          Sets the KeenEventStore to use for storing events in between batch publish operations.
 void setHttpHandler(HttpHandler httpHandler)
          Sets the HttpHandler to use for making HTTP requests.
 void setJsonHandler(KeenJsonHandler jsonHandler)
          Sets the KeenJsonHandler to use for handling JSON operations.
 void setPublishExecutor(java.util.concurrent.Executor publishExecutor)
          Sets the Executor to use for asynchronous publishing operations.
 KeenClient.Builder withEventStore(KeenEventStore eventStore)
          Sets the KeenEventStore to use for storing events in between batch publish operations.
 KeenClient.Builder withHttpHandler(HttpHandler httpHandler)
          Sets the HttpHandler to use for making HTTP requests.
 KeenClient.Builder withJsonHandler(KeenJsonHandler jsonHandler)
          Sets the KeenJsonHandler to use for handling JSON operations.
 KeenClient.Builder withPublishExecutor(java.util.concurrent.Executor publishExecutor)
          Sets the Executor to use for asynchronous publishing operations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeenClient.Builder

public KeenClient.Builder()
Method Detail

getDefaultHttpHandler

protected HttpHandler getDefaultHttpHandler()
                                     throws java.lang.Exception
Gets the default HttpHandler to use if none is explicitly set for this builder. This implementation returns a handler that will use HttpURLConnection to make HTTP requests. Subclasses should override this to provide an alternative default HttpHandler.

Returns:
The default HttpHandler.
Throws:
java.lang.Exception - If there is an error creating the HttpHandler.

getHttpHandler

public HttpHandler getHttpHandler()
Gets the HttpHandler that this builder is currently configured to use for making HTTP requests. If null, a default will be used instead.

Returns:
The HttpHandler to use.

setHttpHandler

public void setHttpHandler(HttpHandler httpHandler)
Sets the HttpHandler to use for making HTTP requests.

Parameters:
httpHandler - The HttpHandler to use.

withHttpHandler

public KeenClient.Builder withHttpHandler(HttpHandler httpHandler)
Sets the HttpHandler to use for making HTTP requests.

Parameters:
httpHandler - The HttpHandler to use.
Returns:
This instance (for method chaining).

getDefaultJsonHandler

protected abstract KeenJsonHandler getDefaultJsonHandler()
                                                  throws java.lang.Exception
Gets the default KeenJsonHandler to use if none is explicitly set for this builder. Subclasses must override this to provide a default KeenJsonHandler.

Returns:
The default KeenJsonHandler.
Throws:
java.lang.Exception - If there is an error creating the KeenJsonHandler.

getJsonHandler

public KeenJsonHandler getJsonHandler()
Gets the KeenJsonHandler that this builder is currently configured to use for handling JSON operations. If null, a default will be used instead.

Returns:
The KeenJsonHandler to use.

setJsonHandler

public void setJsonHandler(KeenJsonHandler jsonHandler)
Sets the KeenJsonHandler to use for handling JSON operations.

Parameters:
jsonHandler - The KeenJsonHandler to use.

withJsonHandler

public KeenClient.Builder withJsonHandler(KeenJsonHandler jsonHandler)
Sets the KeenJsonHandler to use for handling JSON operations.

Parameters:
jsonHandler - The KeenJsonHandler to use.
Returns:
This instance (for method chaining).

getDefaultEventStore

protected KeenEventStore getDefaultEventStore()
                                       throws java.lang.Exception
Gets the default KeenEventStore to use if none is explicitly set for this builder. This implementation returns a RAM-based store. Subclasses should override this to provide an alternative default KeenEventStore.

Returns:
The default KeenEventStore.
Throws:
java.lang.Exception - If there is an error creating the KeenEventStore.

getEventStore

public KeenEventStore getEventStore()
Gets the KeenEventStore that this builder is currently configured to use for storing events between batch publish operations. If null, a default will be used instead.

Returns:
The KeenEventStore to use.

setEventStore

public void setEventStore(KeenEventStore eventStore)
Sets the KeenEventStore to use for storing events in between batch publish operations.

Parameters:
eventStore - The KeenEventStore to use.

withEventStore

public KeenClient.Builder withEventStore(KeenEventStore eventStore)
Sets the KeenEventStore to use for storing events in between batch publish operations.

Parameters:
eventStore - The KeenEventStore to use.
Returns:
This instance (for method chaining).

getDefaultPublishExecutor

protected java.util.concurrent.Executor getDefaultPublishExecutor()
                                                           throws java.lang.Exception
Gets the default Executor to use if none is explicitly set for this builder. This implementation returns a simple fixed thread pool with the number of threads equal to the number of available processors. Subclasses should override this to provide an alternative default Executor.

Returns:
The default Executor.
Throws:
java.lang.Exception - If there is an error creating the Executor.

getPublishExecutor

public java.util.concurrent.Executor getPublishExecutor()
Gets the Executor that this builder is currently configured to use for asynchronous publishing operations. If null, a default will be used instead.

Returns:
The Executor to use.

setPublishExecutor

public void setPublishExecutor(java.util.concurrent.Executor publishExecutor)
Sets the Executor to use for asynchronous publishing operations.

Parameters:
publishExecutor - The Executor to use.

withPublishExecutor

public KeenClient.Builder withPublishExecutor(java.util.concurrent.Executor publishExecutor)
Sets the Executor to use for asynchronous publishing operations.

Parameters:
publishExecutor - The Executor to use.
Returns:
This instance (for method chaining).

build

public KeenClient build()
Builds a new Keen client using the interfaces which have been specified explicitly on this builder instance via the set* or with* methods, or the default interfaces if none have been specified.

Returns:
A newly constructed Keen client.

buildInstance

protected KeenClient buildInstance()
Builds an instance based on this builder. This method is exposed only as a test hook to allow test classes to modify how the KeenClient is constructed (i.e. by providing a mock Environment.

Returns:
The new KeenClient.