Skip to content
AnirudhRamanan edited this page Mar 9, 2016 · 1 revision

Batching Strategy is responsible for the generation of batches depending upon the configuration used by the client. Every batching strategy will have its own PersistenceStrategy and OnBatchReadyListener, which will get fired whenever the batch is ready depending upon the configuration. Eg. If we are using a SizeBatchingStrategy with BATCH_SIZE = 5, then the OnBatchReadyListener will get fired whenever the batch size reaches 5.

In-depth description

####Batching Strategy An interface that the BaseBatchingStrategy implements to provide his own implementation.

  • void onDataPushed(Collection<E> dataCollection) : This method tells the batching strategy that a new data/event has been added, and in turn it informs its persistence strategy

  • void flush(boolean forced) : This method fires the OnBatchReadyListener whenever the batch is ready. If flush is set to true, it will fire the OnBatchReadyListener regardless of the state of the batch.

  • boolean isInitialized() : This method returns true if the batching strategy gets initialized.

  • void onInitialized(Context context, OnBatchReadyListener<E, T> onBatchReadyListener, Handler handler) : This method takes in the context, the OnBatchReadyListener and handler for performing the tasks off the main thread.

####Base Batching Strategy An abstract class that implements the BatchingStrategy Interface, where in all the other strategies will extend this abstract class, and will have their own implementations.

  • public BaseBatchingStrategy(PersistenceStrategy<E> persistenceStrategy) : The constructor takes in the persistence strategy, wherein all the data/events for this batchingStrategy will be stored in their persistenceStrategy. Thus, every batching strategy will takes its own persistence strategy to persist their data.

Following are the different types of Batching Strategies that are provided by the library.

  • Size Batching Strategy : This strategy is based on the size of the events. The client will provide the maxBatchSize, and whenever the batch size reaches the maxBatchSize, it will fire the OnBatchReadyListener. This strategy has a SizeBatch associated with it. The SizeBatch which extends the Batch class, will contain all the information pertaining to the SizeBatchingStrategy such as the maxBatchSize, and the dataCollection.
      PersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy();

      //using sizeBatchingStrategy. Whenever the number of events is 5, a batch is formed
      SizeBatchingStrategy sizeBatchingStrategy = new SizeBatchingStrategy(5, persistenceStrategy);

      GsonSerializationStrategy gsonSerializationStrategy = new GsonSerializationStrategy();

      //Handler for all operations
      HandlerThread handlerThread = new HandlerThread("bg");
      handlerThread.start();
      Handler backgroundHandler = new Handler(handlerThread.getLooper());

      BatchManager batchManager = new BatchManager.Builder<>()
              .setBatchingStrategy(sizeBatchingStrategy)
              .setSerializationStrategy(gsonSerializationStrategy)
              .setHandler(backgroundHandler)
              .setOnBatchReadyListener(new OnBatchReadyListener() {
                  @Override
                  public void onReady(BatchingStrategy causingStrategy, Batch batch) {
                      //Callback that the batch is ready
                  }
              }).build(this);

      //push in data to the library
      batchManager.addToBatch(Collections.singleton(new EventData()));
  • Time Batching Strategy : This strategy is based on the timeouts. The client will provide the maxTimeOut, and whenever the time expries, it will fire the OnBatchReadyListener with all the events it has. Whenever a new event is added, the timer will start again. It means the timeout is calculated from the last event that has been added. This strategy has a TimeBatch associated with it. The TimeBatch which extends the Batch class, will contain all the information pertaining to the TimeBatchingStrategy such as the maxTimeOut, and the dataCollection.
      PersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy();

      //using timeBatchingStrategy. Whenever the timer expires, the batch is sent. 5000 ms is the timeout in this case
      TimeBatchingStrategy timeBatchingStrategy = new TimeBatchingStrategy(5000, persistenceStrategy);

      GsonSerializationStrategy gsonSerializationStrategy = new GsonSerializationStrategy();

      //Handler for all operations
      HandlerThread handlerThread = new HandlerThread("bg");
      handlerThread.start();
      Handler backgroundHandler = new Handler(handlerThread.getLooper());

      BatchManager batchManager = new BatchManager.Builder<>()
              .setBatchingStrategy(timeBatchingStrategy)
              .setSerializationStrategy(gsonSerializationStrategy)
              .setHandler(backgroundHandler)
              .setOnBatchReadyListener(new OnBatchReadyListener() {
                  @Override
                  public void onReady(BatchingStrategy causingStrategy, Batch batch) {
                      //Callback that the batch is ready
                  }
              }).build(this);

      //push in data to the library
      batchManager.addToBatch(Collections.singleton(new EventData()));
Clone this wiki locally