if( journal == null )
throw new IllegalStateException("Journal is closed.");
// Do the checkpoint asynchronously?
Latch latch=null;
if( sync ) {
latch = new Latch();
checkpointRequests.put(latch);
} else {
checkpointRequests.put(Boolean.TRUE);
}
checkpointExecutor.execute(new Runnable() {
public void run() {
ArrayList listners = new ArrayList();
try {
// Avoid running a checkpoint too many times in a row.
// Consume any queued up checkpoint requests.
try {
boolean requested = false;
Object t;
while ((t=checkpointRequests.poll(0)) != null) {
if( t.getClass()==Latch.class )
listners.add(t);
requested = true;
}
if (!requested) {
return;
}
}
catch (InterruptedException e1) {
return;
}
log.debug("Checkpoint started.");
RecordLocation newMark = null;
Iterator iterator = messageStores.values().iterator();
while (iterator.hasNext()) {
try {
JournalMessageStore ms = (JournalMessageStore) iterator.next();
RecordLocation mark = ms.checkpoint();
if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
newMark = mark;
}
}
catch (Exception e) {
log.error("Failed to checkpoint a message store: " + e, e);
}
}
iterator = topicMessageStores.values().iterator();
while (iterator.hasNext()) {
try {
JournalTopicMessageStore ms = (JournalTopicMessageStore) iterator.next();
RecordLocation mark = ms.checkpoint();
if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
newMark = mark;
}
}
catch (Exception e) {
log.error("Failed to checkpoint a message store: " + e, e);
}
}
try {
if (newMark != null) {
if( log.isDebugEnabled() )
log.debug("Marking journal: "+newMark);
journal.setMark(newMark, true);
}
}
catch (Exception e) {
log.error("Failed to mark the Journal: " + e, e);
}
// Clean up the DB if it's a JDBC store.
if( longTermPersistence instanceof JDBCPersistenceAdapter ) {
// Disabled periodic clean up as it deadlocks with the checkpoint operations.
((JDBCPersistenceAdapter)longTermPersistence).cleanup();
}
log.debug("Checkpoint done.");
} finally {
for (Iterator iter = listners.iterator(); iter.hasNext();) {
Latch latch = (Latch) iter.next();
latch.release();
}
}
}
});
if( sync ) {
latch.acquire();
}
}
catch (InterruptedException e) {
log.warn("Request to start checkpoint failed: " + e, e);
}