import akka.persistence.journal.japi.AsyncWriteJournal;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.Uninterruptibles;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.concurrent.Future;
private static final Map<String, CountDownLatch> blockReadMessagesLatches = new ConcurrentHashMap<>();
+ private static Object deserialize(Object data) {
+ return data instanceof byte[] ? SerializationUtils.deserialize((byte[])data) : data;
+ }
+
public static void addEntry(String persistenceId, long sequenceNr, Object data) {
Map<Long, Object> journal = journals.get(persistenceId);
if(journal == null) {
}
synchronized (journal) {
- journal.put(sequenceNr, data);
+ journal.put(sequenceNr, data instanceof Serializable ?
+ SerializationUtils.serialize((Serializable) data) : data);
}
}
synchronized (journalMap) {
List<T> journal = new ArrayList<>(journalMap.size());
for(Object entry: journalMap.values()) {
- if(type.isInstance(entry)) {
- journal.add((T) entry);
+ Object data = deserialize(entry);
+ if(type.isInstance(data)) {
+ journal.add((T) data);
}
}
@Override
public Future<Void> doAsyncReplayMessages(final String persistenceId, final long fromSequenceNr,
- final long toSequenceNr, long max, final Procedure<PersistentRepr> replayCallback) {
+ final long toSequenceNr, final long max, final Procedure<PersistentRepr> replayCallback) {
return Futures.future(new Callable<Void>() {
@Override
public Void call() throws Exception {
}
synchronized (journal) {
+ int count = 0;
for (Map.Entry<Long,Object> entry : journal.entrySet()) {
- if (entry.getKey() >= fromSequenceNr && entry.getKey() <= toSequenceNr) {
+ if (++count <= max && entry.getKey() >= fromSequenceNr && entry.getKey() <= toSequenceNr) {
PersistentRepr persistentMessage =
- new PersistentImpl(entry.getValue(), entry.getKey(), persistenceId,
+ new PersistentImpl(deserialize(entry.getValue()), entry.getKey(), persistenceId,
false, null, null);
replayCallback.apply(persistentMessage);
}
@Override
public Void call() throws Exception {
for (PersistentRepr repr : messages) {
- Map<Long, Object> journal = journals.get(repr.persistenceId());
- if(journal == null) {
- journal = Maps.newLinkedHashMap();
- journals.put(repr.persistenceId(), journal);
- }
+ LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
+ repr.sequenceNr(), repr.payload());
- synchronized (journal) {
- LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
- repr.sequenceNr(), repr.payload());
- journal.put(repr.sequenceNr(), repr.payload());
- }
+ addEntry(repr.persistenceId(), repr.sequenceNr(), repr.payload());
WriteMessagesComplete complete = writeMessagesComplete.get(repr.persistenceId());
if(complete != null) {