Simple Android Queue ( Useful for Async )
This is Android/Java library with simple Queue interface and it persistent implementation used build in Android SQLite. It help perform async exchange with remote servers in case of unstable network connection.
Originally saqua - a fancy alcohol drink from Endmond Hamilton's cosmic opera "The Star Kings". "A cursed good drink!" as main person say about it. So, why not.
Initializing
publicclassAppextendsApplication {
...
// Init DB Queue factory, must be performed before obtained first queue SimpleDBQueueFactory.initFactory(getApplicationContext());
...
}Produce messages
publicvoidstoreDocument(DocumentdocToStore) {
SimpleDBQueuebackupQueue = SimpleDBQueueFactory.getQueue("backup_documents");
backupQueue.push(docToStore, ImmutableMap.of("collection", backupCollection));
SimpleDBQueuedocsQueue = SimpleDBQueueFactory.getQueue("documents");
docsQueue.push(docToStore); }Consume messages
publicclassBackupDocumentsServiceextendsAsyncTask {
@OverrideprotectedObjectdoInBackground(Object[] params) {
SimpleDBQueuequeue = SimpleDBQueueFactory.getQueue("backup_documents");
while (!queue.isEmpty()) {
SimpleQueueMessagemessage = queue.pull();
StringbackupCollection = message.getHeaders().get("backup_collection");
if (backupCollection == null) {
queue.Ack(); // wrong record, skip
} else {
objectToStore = message.getBody();
if (RemoteService.storeBackupObject(backupCollection, objectToStore)) {
queue.Ack();
} else {
queue.NAck();
}
}
}
returnnull;
}
}