Search in sources :

Example 1 with ActorPersistenceObject

use of actor4j.core.persistence.ActorPersistenceObject in project actor4j-core by relvaner.

the class ActorCell method saveSnapshot.

@SuppressWarnings("unchecked")
public <S extends ActorPersistenceObject> void saveSnapshot(Consumer<S> onSuccess, Consumer<Exception> onFailure, S state) {
    if (system.persistenceMode && state != null) {
        state.persistenceId = persistenceId();
        List<ActorPersistenceObject> list = new ArrayList<>();
        list.add(state);
        PersistenceTuple tuple = new PersistenceTuple((Consumer<ActorPersistenceObject>) onSuccess, onFailure, list);
        try {
            system.messageDispatcher.postPersistence(new ActorMessage<String>(new ObjectMapper().writeValueAsString(state), PersistenceServiceActor.PERSIST_STATE, id, null));
            persistenceTuples.offer(tuple);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            onFailure.accept(e);
        }
    }
}
Also used : ActorPersistenceObject(actor4j.core.persistence.ActorPersistenceObject) ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ActorPersistenceObject

use of actor4j.core.persistence.ActorPersistenceObject in project actor4j-core by relvaner.

the class ActorCell method persist.

@SuppressWarnings("unchecked")
public <E extends ActorPersistenceObject> void persist(Consumer<E> onSuccess, Consumer<Exception> onFailure, E... events) {
    if (system.persistenceMode && events != null) {
        List<ActorPersistenceObject> list = new ArrayList<>(Arrays.asList(events));
        for (ActorPersistenceObject obj : list) obj.persistenceId = persistenceId();
        PersistenceTuple tuple = new PersistenceTuple((Consumer<ActorPersistenceObject>) onSuccess, onFailure, list);
        try {
            system.messageDispatcher.postPersistence(new ActorMessage<String>(new ObjectMapper().writeValueAsString(events), PersistenceServiceActor.PERSIST_EVENTS, id, null));
            persistenceTuples.offer(tuple);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            onFailure.accept(e);
        }
    }
}
Also used : ActorPersistenceObject(actor4j.core.persistence.ActorPersistenceObject) ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ActorPersistenceObject (actor4j.core.persistence.ActorPersistenceObject)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayList (java.util.ArrayList)2