Reputation: 2137
I am trying to work with PersistentActor in Akka.
I tried the basic example provided in the Akka documentation at https://doc.akka.io/docs/akka/current/persistence.html.
I am getting the following error at the starting of the actor:
Caused by: java.lang.IllegalArgumentException: Default journal plugin is not configured, see 'reference.conf' at akka.persistence.Persistence$.verifyPluginConfigIsDefined(Persistence.scala:193) at akka.persistence.Persistence.defaultJournalPluginId$lzycompute(Persistence.scala:228) at akka.persistence.Persistence.defaultJournalPluginId(Persistence.scala:226) at akka.persistence.Persistence.journalConfigFor(Persistence.scala:336) at akka.persistence.Eventsourced.$init$(Eventsourced.scala:97) at akka.persistence.AbstractPersistentActor.(PersistentActor.scala:455) at org.spituk.learning.akka.samples.ExamplePersistentActor.(ExamplePersistentActor.java:72)
The code I tried is like:
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.persistence.AbstractPersistentActor;
import akka.persistence.SnapshotOffer;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
class Cmd implements Serializable {
private static final long serialVersionUID = 1L;
private final String data;
public Cmd(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
class Evt implements Serializable {
private static final long serialVersionUID = 1L;
private final String data;
public Evt(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
class ExampleState implements Serializable {
private static final long serialVersionUID = 1L;
private final ArrayList<String> events;
public ExampleState() {
this(new ArrayList<>());
}
public ExampleState(ArrayList<String> events) {
this.events = events;
}
public ExampleState copy() {
return new ExampleState(new ArrayList<>(events));
}
public void update(Evt evt) {
events.add(evt.getData());
}
public int size() {
return events.size();
}
@Override
public String toString() {
return events.toString();
}
}
public class ExamplePersistentActor extends AbstractPersistentActor {
private int snapShotInterval = 1000;
private ExampleState state = new ExampleState();
public static Props props() {
return Props.create(ExamplePersistentActor.class);
}
public int getNumEvents() {
return state.size();
}
@Override
public String persistenceId() {
return "sample-id-1";
}
@Override
public Receive createReceiveRecover() {
return receiveBuilder()
.match(Evt.class, state::update)
.match(SnapshotOffer.class, ss -> state = (ExampleState) ss.snapshot())
.build();
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(
Cmd.class,
c -> {
final String data = c.getData();
final Evt evt = new Evt(data + "-" + getNumEvents());
System.out.println("Cmd received::" + c);
persist(
evt,
(Evt e) -> {
state.update(e);
getContext().getSystem().getEventStream().publish(e);
if (lastSequenceNr() % snapShotInterval == 0 && lastSequenceNr() != 0)
// IMPORTANT: create a copy of snapshot because ExampleState is mutable
saveSnapshot(state.copy());
});
})
.matchEquals("print", s -> System.out.println(state))
.build();
}
public static void main(String[] args) throws IOException {
ActorSystem persistentSystem = ActorSystem.create("persistent-system");
ActorRef persistentSystemActor = persistentSystem.actorOf(ExamplePersistentActor.props());
persistentSystemActor.tell(new Cmd("Hello"), ActorRef.noSender());
System.in.read();
persistentSystem.terminate();
}
}
I have not defined any configurations for the persistence intend to use the built-in default plugins. Can someone please help me with this?
Upvotes: 1
Views: 1558
Reputation: 2137
I had to add the following to the application.conf
file:
akka.persistence.journal.plugin = "akka.persistence.journal.leveldb"
akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local"
akka.persistence.journal.leveldb.dir = "target/example/journal"
akka.persistence.snapshot-store.local.dir = "target/example/snapshots"
# DO NOT USE THIS IN PRODUCTION !!!
akka.persistence.journal.leveldb.native = false
Upvotes: 2