4

I am currently in the process of writing my own plugin for the Akka SyncWriteJournal API to implement the connection with HSQLDB.

The problem is that I do no understand the requirements for the method doAsyncReplayMessages. It states that it needs to return a future and that all the messages should be called by replayCallback.

Let's say that I have a query which returns a list of messages: List<Message> messages. Can anyone provide a minimal example (with explanation) of how to use replayCallback, and Future to implement the method correctly by using that list? How would replayCallback and Future work together and what should be returned by the method doAsyncReplayMessages?

Thanks!

-Edit-

With the help of some comments I've whipped up an implementation which is not complete but incorporates the idea proposed:

public Future<Void> doAsyncReplayMessages(final String persistenceId, long fromSequenceNr, long toSequenceNr, long max,
        final Procedure<PersistentRepr> replayCallback) {
    final ExecutionContext ec = context().system().dispatcher();

    final Future<Void> future = Futures.future(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            final List<Message> messages = getMessages();
            for (int i = 0; i < feedbackList.size(); i++) {
                replayCallback.apply(
                        new PersistentImpl(messages.get(i), i, persistenceId, false, null, null));
            }
            return null;
        }
    }, ec);

    return future;
}

As you might see it missed a few key concepts I'm still missing. The PersistentImpl needs one argument Seq<String> confirm which is null still. And perhaps more importantly I'm return null since the future expects Void as return type and I'm not sure how implement that. It currently throws a NPE:

[ERROR] [08/28/2014 12:31:19.582] [akkaSystem-akka.actor.default-dispatcher-7] [akka://akkaSystem/system/journal] null
java.lang.NullPointerException
    at akka.persistence.journal.japi.AsyncRecovery.asyncReadHighestSequenceNr(AsyncRecovery.scala:26)
    at akka.persistence.journal.SyncWriteJournal$$anonfun$receive$1.applyOrElse(SyncWriteJournal.scala:53)
    at akka.actor.Actor$class.aroundReceive(Actor.scala:465)
    at akka.persistence.journal.japi.SyncWriteJournal.aroundReceive(SyncWriteJournal.scala:16)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
    at akka.actor.ActorCell.invoke(ActorCell.scala:487)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
    at akka.dispatch.Mailbox.run(Mailbox.scala:220)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
user3354890
  • 367
  • 1
  • 3
  • 10

1 Answers1

2

You can simply wrap your blocking operation in the Future, such as: Future { fetchStuff() }.

You can refer to dnvriend/akka-persistence-jdbc: JdbcSyncWriteJournal for a full blown implementation of a synchronous journal.

Konrad 'ktoso' Malawski
  • 13,102
  • 3
  • 47
  • 52
  • I'm sorry, I cannot read scala (my implementation is in Java). Can you elaborate a bit more? – user3354890 Aug 26 '14 at 12:39
  • Here are the docs for using Futures from Java: http://doc.akka.io/docs/akka/2.3.5/java/futures.html#Use_Directly You can call Futures.future(callable, executionContext) where the callable would fetch the List, call the replyCallback on each of them and then finish. That callable must have a return type of Void to match the expected return type Future. – Endre Varga Aug 27 '14 at 12:49