Code Monkey home page Code Monkey logo

myyhc / batchman Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flipkart-incubator/batchman

0.0 1.0 0.0 819 KB

This library for Android will take any set of events and batch them up before sending it to the server. It also supports persisting the events on disk so that no event gets lost because of an app crash. Typically used for developing any in-house analytics sdk where you have to make a single api call to push events to the server but you want to optimize the calls so that the api call happens only once per x events, or say once per x minutes. It also supports exponential backoff in case of network failures

Java 100.00%

batchman's Introduction

BatchMan

Branch Build Status
master Build Status
develop Build Status

BatchMan (short for batch manager) is an android library implementation responsible for batching of events based on the configurations done by the client, and giving the batch back to the client.

The library has been written in a more flexible way, so that the client can plugin his own implementations for batching.

  • BatchManager : It is the entry point to the library, where in the client will use the instance of the batch manager to push in data to the library for batching.

  • BatchingStrategy : It is an interface, where all the batching logic comes in. The library has 4 batching strategies on its own, or the client can implement the interface, and provide his/her own logic for batching.

  • PersistenceStrategy : It is an interface, where all the persistence logic comes in. The library has 3 persistence strategies on its own, or the client can provide his/her own persistence layer to persist the events, just to make sure that there is no loss of events (in case of app crash)

  • OnBatchReadyListener : It is a interface, which gives a callback, whenever the batch is ready. The client can consume the batch, and can make a network call to the server. There are various types of OnBatchReadyListener which will be discussed later.

  • Data : It is an abstract class, wherein the client will need to extend this class for his events.

Get BatchMan

Add it in your root build.gradle at the end of repositories :

	allprojects {
		repositories {
			...
			maven { url "https://jitpack.io" }
		}
	}

Add the dependencies :

  • Library :
	dependencies {
	        compile 'com.github.flipkart-incubator.batchman:batching:1.3.6'
	}
  • GSON Serialization :
	dependencies {
	        compile 'com.github.flipkart-incubator.batchman:batching-gson:1.3.6'
	}

How to use

Step 1 :

Initialize persistence strategy, batching strategy will take persistence strategy as one of it's parameters.

// Using inMemoryPersistenceStrategy
PersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy();

Step 2 :

Initialize batching strategy with a max batch size and persistence strategy.

int MAX_BATCH_SIZE = 5;

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

Step 3 :

Initialize serialization strategy and background handler thread. To include GsonSerializationStrategy, you must have its dependency in your gradle file. To get dependency, look into the Getting Started section.

// Initialize serialization strategy
SerializationStrategy gsonSerializationStrategy = new GsonSerializationStrategy();

// Handler for doing heavy operations like read/write from disk
HandlerThread handlerThread = new HandlerThread("bg");
handlerThread.start();
Handler backgroundHandler = new Handler(handlerThread.getLooper());

Step 4 :

Build batch manager with all the strategies and handler thread we initialized in previous steps. Batch manger will also take a listener for giving callbacks when a batch is ready.

// Initialize batch manager
BatchManager batchManager = new BatchManager.Builder<>()
       .setBatchingStrategy(sizeBatchingStrategy)
       .setSerializationStrategy(gsonSerializationStrategy)
       .setHandler(backgroundHandler)
       //to enable logging while debug
       .enableLogging()
       .setOnBatchReadyListener(new OnBatchReadyListener() {
           @Override
           public void onReady(BatchingStrategy causingStrategy, Batch batch) {
               //Callback with batch when it's ready
           }
       }).build(this);

Step 5 :

Use addToBatch() for adding events to batch manager.

// Push data to batch manager
batchManager.addToBatch(Collections.singleton(new EventData()));

Getting Started

Dependencies

License

The MIT License (MIT)

Copyright (c) 2017 Flipkart Internet Pvt. Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

batchman's People

Contributors

anirudhramanan avatar kushalsharma avatar yasirmhd avatar thekirankumar avatar guptasourabh04 avatar anshulikaprasad avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.