* @throws InvalidRecordLocationException
* @throws IllegalStateException
*/
private void recover() throws IllegalStateException, InvalidRecordLocationException, IOException, JMSException {
RecordLocation pos = null;
int transactionCounter = 0;
log.info("Journal Recovery Started.");
// While we have records in the journal.
while ((pos = journal.getNextRecordLocation(pos)) != null) {
byte[] data = journal.read(pos);
// Read the destination and packate from the record.
String destination = null;
Packet packet = null;
DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
try {
byte type = is.readByte();
switch (type) {
case PACKET_RECORD_TYPE:
// Is the current packet part of the destination?
destination = is.readUTF();
packet = wireFormat.readPacket(is);
// Try to replay the packet.
JournalMessageStore store = (JournalMessageStore) createQueueMessageStore(destination);
if (packet instanceof ActiveMQMessage) {
ActiveMQMessage msg = (ActiveMQMessage) packet;
try {
store.getLongTermStore().addMessage(msg);
transactionCounter++;
}
catch (Throwable e) {
log.error("Recovery Failure: Could not add message: " + msg.getJMSMessageIdentity().getMessageID() + ", reason: " + e, e);
}
}
else if (packet instanceof MessageAck) {
MessageAck ack = (MessageAck) packet;
try {
store.getLongTermStore().removeMessage(ack.getMessageIdentity(), ack);
transactionCounter++;
}
catch (Throwable e) {
log.error("Recovery Failure: Could not remove message: " + ack.getMessageIdentity().getMessageID() + ", reason: " + e, e);
}
}
else {
log.error("Unknown type of packet in transaction log which will be discarded: " + packet);
}
break;
case COMMAND_RECORD_TYPE:
break;
default:
log.error("Unknown type of record in transaction log which will be discarded: " + type);
break;
}
}
finally {
is.close();
}
}
RecordLocation location = writeCommand("RECOVERED", true);
journal.setMark(location, true);
log.info("Journal Recovered: " + transactionCounter + " message(s) in transactions recovered.");
}