-
Notifications
You must be signed in to change notification settings - Fork 11
OnBatchReadyListener
OnBatchReadyListener is an interface, with a method void onReady(BatchingStrategy<E, T> causingStrategy, T batch)
which gets called, whenever the batch is ready. It is a basic listener callback for the batch.
There are many variants of OnBatchReadyListener in the library, which we will discuss one by one.
-
TagBatchReadyListener : TagBatchReadyListener, which implements the OnBatchReadyListener of type TagBatch, has a hashmap to store the tag with its corresponding listener. Each tag will have its own listener, so whenever a particular tag batch is formed, its corresponding listener will get called, regardless of the other tags.
-
public void addListenerForTag(Tag tag, OnBatchReadyListener<E, TagBatchingStrategy.TagBatch<E>> listener)
: Used to add the tag and its corresponding listener to the hashmap. -
private OnBatchReadyListener<E, TagBatchingStrategy.TagBatch<E>> getListenerByTag(Tag tag)
: Used to get a listener for a particular tag. -
NOTE : This type of listener is only useful if TagBatchingStrategy is used.
-
-
PersistedBatchReadyListener : PersistedBatchReadyListener, which implements the OnBatchReadyListener persists the batch till there is a response from the server. It uses the Tape library from persisting the batch in a file. It also has a PersistedBatchCallback listener which will be discussed below.
There is a public method
finish(T Batch)
, which the client will call in the onReady whenever there is a success response from the server (2XX), or if the request is bad (400). This method removes that particular batch from the queue, and since it uses a FIFO queue, the next batch will be ready for syncing.-
public PersistedBatchReadyListener(String filePath, SerializationStrategy<E, T> serializationStrategy, Handler handler, @Nullable PersistedBatchCallback<T> listener)
: The constructor takes in the filePath for creating the file for persistence, and a PersistedBatchCallback listener, wherein its respective methods are called depending upon the response from the server. -
public void finish(final T batch)
: This method removes the batch provided from the queue.
All these operations will take place in the background handler thread.
-
-
TrimPersistedBatchReadyListener : TrimPersistedBatchReadyListener is an extension of PersistedBatchReadyListener, which takes in a TrimmedBatchCallback listener which gets called whenever the size of the queue file is greater than a certain limit specified.
-
public TrimPersistedBatchReadyListener(String filePath, SerializationStrategy<E, T> serializationStrategy, Handler handler, int maxQueueSize, int trimSize, int mode, PersistedBatchCallback<T> persistedBatchCallback, TrimmedBatchCallback trimmedBatchCallback)
: maxQueueSize is the maximum size of queue file permitted, and trimSize is the size to which the queue file will be trimmed whenever its size goes beyond a certain limit.
MODE : It is a bit operator, which specifies when the trimmming should take place
-
MODE_TRIM_AT_START
: Trimming is done at the start of the application. Eg. When the app starts, and if the size of the queue file is greater than a certain limit, it will get trimmed. -
MODE_TRIM_ON_READY
: Trimming is done when the onReady is called. -
MODE_TRIM_NONE
: Disables trimming.
Whenever trimming takes place, TrimmedBatchCallback calls the
onTrimmed(int oldSize, int newSize)
method, wherein it gives the size of old queue file, and the size to which it called trimmed, so that the client is aware of amount of events that got trimmed. -
-
NetworkPersistedBatchReadyListener : NetworkPersistedBatchReadyListener is a listener based on the response from the network, which has a VallueCallback, wherein the client will pass in the value(eg is the sync complete, or the error response), and this listener will be responsible for retrying the network, registering broadcast and etc, depending upon the network response received.
Network Responses and its solutions
-
2XX : Whenever there is 2XX response from the server, the finish method will get called, removing the batch from the queue file, and giving in the next batch for syncing.
-
5XX : Whenever there is 5XX response from the server, the library will retry the batch again depending upon certain criteria. The client will provide a defaultTimeOut and a defaultBackoffMultiplier, after which the library will try syncing the batch. The library will backoff exponentially
mCurrentTimeoutMs += (mCurrentTimeoutMs * defaultBackoffMultiplier)
. -
4XX : Whenever there is 4XX response from the server, the finish method should be called since its a bad request, and it can never be synced with the server.
-
No Network : The library registers a broadcast receiver, and when there is any change in the network connectivity, the onReceive method gets called which resumes the operations with the last batch.
This listener is very useful whenever you want a error handling criteria for the network call and its responses. This listener is not bound to any network library, thus providing the client the flexibility to use any library for making network response.
-