{"id":19224831,"url":"https://github.com/flipkart-incubator/batchman","last_synced_at":"2025-04-21T00:31:32.084Z","repository":{"id":17453273,"uuid":"20227177","full_name":"flipkart-incubator/batchman","owner":"flipkart-incubator","description":"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","archived":false,"fork":false,"pushed_at":"2023-07-06T05:58:58.000Z","size":3834,"stargazers_count":55,"open_issues_count":4,"forks_count":11,"subscribers_count":127,"default_branch":"develop","last_synced_at":"2025-04-01T07:12:20.985Z","etag":null,"topics":["analytics","android-library","batch-manager","batch-processing","batching","java","job-scheduler","networking","persistence","persistence-strategies","retry","serialization","strategies"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flipkart-incubator.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-27T16:35:48.000Z","updated_at":"2024-03-31T14:14:52.000Z","dependencies_parsed_at":"2022-09-04T10:52:47.728Z","dependency_job_id":null,"html_url":"https://github.com/flipkart-incubator/batchman","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipkart-incubator%2Fbatchman","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipkart-incubator%2Fbatchman/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipkart-incubator%2Fbatchman/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flipkart-incubator%2Fbatchman/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flipkart-incubator","download_url":"https://codeload.github.com/flipkart-incubator/batchman/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249979949,"owners_count":21355325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","android-library","batch-manager","batch-processing","batching","java","job-scheduler","networking","persistence","persistence-strategies","retry","serialization","strategies"],"created_at":"2024-11-09T15:12:58.566Z","updated_at":"2025-04-21T00:31:31.730Z","avatar_url":"https://github.com/flipkart-incubator.png","language":"Java","funding_links":[],"categories":["任务调度"],"sub_categories":[],"readme":"# BatchMan [![](https://jitpack.io/v/flipkart-incubator/batchman.svg)](https://jitpack.io/#flipkart-incubator/batchman)\n\n| Branch | Build Status |\n|--------|--------------|\n| master | [![Build Status](https://travis-ci.org/flipkart-incubator/batchman.svg?branch=master)](https://travis-ci.org/flipkart-incubator/batchman) |\n| develop    | [![Build Status](https://travis-ci.org/flipkart-incubator/batchman.svg?branch=develop)](https://travis-ci.org/flipkart-incubator/batchman) |\n\nBatchMan (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.\n\nThe library has been written in a more flexible way, so that the client can plugin his own implementations for batching.\n* \u003cb\u003eBatchManager\u003c/b\u003e : 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.\n\n* \u003cb\u003eBatchingStrategy\u003c/b\u003e : 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.\n\n* \u003cb\u003ePersistenceStrategy\u003c/b\u003e : 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) \n\n* \u003cb\u003eOnBatchReadyListener\u003c/b\u003e : 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.\n\n* \u003cb\u003eData\u003c/b\u003e : It is an abstract class, wherein the client will need to extend this class for his events.\n\n\n![Diagram](Batchman.png?raw=true \"High level diagram\")\n\nGet BatchMan\n------------\n\nAdd it in your root build.gradle at the end of repositories :\n\n````java\n\tallprojects {\n\t\trepositories {\n\t\t\t...\n\t\t\tmaven { url \"https://jitpack.io\" }\n\t\t}\n\t}\n````\t\n\nAdd the dependencies :\n\n* \u003cb\u003eLibrary\u003c/b\u003e :\n\n````java\n\tdependencies {\n\t        compile 'com.github.flipkart-incubator.batchman:batching:1.3.9'\n\t}\n````\n\n* \u003cb\u003eGSON Serialization\u003c/b\u003e :\n\n````java\n\n\tdependencies {\n\t        compile 'com.github.flipkart-incubator.batchman:batching-gson:1.3.9'\n\t}\n````\n\nHow to use\n----------\n\n### Step 1 :\n\nInitialize persistence strategy, batching strategy will take persistence strategy as one of it's parameters.\n\n````java\n\n// Using inMemoryPersistenceStrategy\nPersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy();\n\n````\n\n### Step 2 :\n\nInitialize batching strategy with a max batch size and persistence strategy.\n\n````java\n\nint MAX_BATCH_SIZE = 5;\n\n// Using SizeBatchingStrategy. Whenever the number of events is 5, a batch is formed\nSizeBatchingStrategy sizeBatchingStrategy = new SizeBatchingStrategy(MAX_BATCH_SIZE, persistenceStrategy);\n\n````\n\n### Step 3 :\n\nInitialize 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.\n\n````java\n\n// Initialize serialization strategy\nSerializationStrategy gsonSerializationStrategy = new GsonSerializationStrategy();\n\n// Handler for doing heavy operations like read/write from disk\nHandlerThread handlerThread = new HandlerThread(\"bg\");\nhandlerThread.start();\nHandler backgroundHandler = new Handler(handlerThread.getLooper());\n\n````\n\n### Step 4 :\n\nBuild 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.\n\n````java\n\n// Initialize batch manager\nBatchManager batchManager = new BatchManager.Builder\u003c\u003e()\n       .setBatchingStrategy(sizeBatchingStrategy)\n       .setSerializationStrategy(gsonSerializationStrategy)\n       .setHandler(backgroundHandler)\n       //to enable logging while debug\n       .enableLogging()\n       .setOnBatchReadyListener(new OnBatchReadyListener() {\n           @Override\n           public void onReady(BatchingStrategy causingStrategy, Batch batch) {\n               //Callback with batch when it's ready\n           }\n       }).build(this);\n\n````\n\n### Step 5 :\n\nUse addToBatch() for adding events to batch manager.\n\n````java\n\n// Push data to batch manager\nbatchManager.addToBatch(Collections.singleton(new EventData()));\n\n````\n\n### Typical usage\nThis library can also be used for just batching events. \nAt Flipkart, this library is used for pushing analytics events to an in-house backend. For this usecase, you can make create an instance of `NetworkPersistedBatchReadyListener` and pass it to `setOnBatchReadyListener`. \nDont forget to pass an instance of `NetworkBatchListener` by implementing `performNetworkRequest` method. \nDetails of this class is in the comments section below.\n\n```java\npublic static abstract class NetworkBatchListener\u003cE extends Data, T extends Batch\u003cE\u003e\u003e {\n\n        /**\n         * Implement this method and make your network request here. Once request is complete, call the {@link ValueCallback#onReceiveValue(Object)} method.\n         * This method will be called once the batch has been persisted. The batch will be removed or retried once you invoke the networkBatchListener.\n         * While invoking the networkBatchListener, pass a {@link NetworkRequestResponse} object with the following data.\n         * If the network response was successfully received, set complete to true, and set httpErrorCode to the status code from server. If status code is 5XX, this batch will be retried. If status code is 200 or 4XX the batch will be discarded and next batch will be processed.\n         * If the network response was not received (timeout or not connected or any other network error), set complete to false. This will cause a retry until max retries are reached.\n         * \u003cp\u003e\n         * Note: If there is a network redirect, do not call the networkBatchListener, and wait for the final redirected response and pass that one.\n         *\n         * @param batch    batch of data\n         * @param callback callback\n         */\n        public abstract void performNetworkRequest(final T batch, final ValueCallback\u003cNetworkRequestResponse\u003e callback);\n\n        /**\n         * @return true if network is connected\n         */\n        public boolean isNetworkConnected(Context context) {\n            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);\n            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();\n            return null != networkInfo \u0026\u0026 networkInfo.isConnected();\n        }\n    }\n```\n\nGetting Started\n---------------\n\n### [Wiki](https://github.com/Flipkart/fk-android-batchnetworking/wiki)\n\n\nDependencies\n------------\n\n* For Testing : [JUnit](http://junit.org/), [Roboelectric](http://robolectric.org/), [Mockito](http://mockito.org/)\n* For Persistence : [Tape by Square](https://github.com/square/tape)\n* For Serialization/Deserialization : [GSON](https://github.com/google/gson)\n\n\nLicense\n-------\n\n    The Apache License\n    \n    Copyright (c) 2017 Flipkart Internet Pvt. Ltd.\n    \n    Licensed under the Apache License, Version 2.0 (the \"License\"); \n    you may not use this file except in compliance with the License.\n    \n    You may obtain a copy of the License at\n\n       https://www.apache.org/licenses/LICENSE-2.0 \n       \n    Unless required by applicable law or agreed to in writing, software \n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    \n    See the License for the specific language governing permissions and \n    limitations under the License.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflipkart-incubator%2Fbatchman","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflipkart-incubator%2Fbatchman","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflipkart-incubator%2Fbatchman/lists"}