final DatabaseEntry data = new DatabaseEntry();
queue.clear();
OperationStatus status;
if (batchSize > 1) {
Cursor cursor = database.openCursor(null, CursorConfig.DEFAULT);
try {
status = cursor.getFirst(key, data, null);
BatchEvent batch = new BatchEvent();
for (int i = 0; status == OperationStatus.SUCCESS && i < batchSize; ++i) {
SimpleEvent event = createEvent(data);
if (event != null) {
batch.addEvent(event);
}
status = cursor.getNext(key, data, null);
}
try {
manager.send(batch);
} catch (Exception ioe) {
LOGGER.error("Error sending events", ioe);
break;
}
cursor.close();
cursor = null;
Transaction txn = environment.beginTransaction(null, null);
try {
for (Event event : batch.getEvents()) {
try {
Map<String, String> headers = event.getHeaders();
key = new DatabaseEntry(headers.get(FlumeEvent.GUID).getBytes(UTF8));
database.delete(txn, key);
} catch (Exception ex) {
LOGGER.error("Error deleting key from database", ex);
}
}
txn.commit();
} catch (Exception ex) {
LOGGER.error("Unable to commit transaction", ex);
if (txn != null) {
txn.abort();
}
}
} catch (Exception ex) {
LOGGER.error("Error reading database", ex);
shutdown = true;
break;
} finally {
if (cursor != null) {
cursor.close();
}
}
} else {
Transaction txn = environment.beginTransaction(null, null);
Cursor cursor = database.openCursor(txn, null);
try {
status = cursor.getFirst(key, data, LockMode.RMW);
while (status == OperationStatus.SUCCESS) {
SimpleEvent event = createEvent(data);
if (event != null) {
try {
manager.doSend(event);
} catch (Exception ioe) {
errors = true;
LOGGER.error("Error sending event", ioe);
break;
}
if (!errors) {
try {
cursor.delete();
} catch (Exception ex) {
LOGGER.error("Unable to delete event", ex);
}
}
}
status = cursor.getNext(key, data, LockMode.RMW);
}
if (cursor != null) {
cursor.close();
cursor = null;
}
txn.commit();
txn = null;
} catch (Exception ex) {
LOGGER.error("Error reading or writing to database", ex);
shutdown = true;
break;
} finally {
if (cursor != null) {
cursor.close();
}
if (txn != null) {
txn.abort();
}
}