pekko/akka-samples/akka-sample-rest-java/src/main/java/sample/rest/java/PersistentSimpleServiceImpl.java
Jonas Bonér e48572f32e Added TypedActor and TypedTransactor base classes.
Renamed ActiveObject factory object to TypedActor.
Improved network protocol for TypedActor.
Remote TypedActors now identified by UUID.
2010-07-26 20:03:33 +02:00

42 lines
No EOL
1.3 KiB
Java

/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package sample.rest.java;
import se.scalablesolutions.akka.actor.TypedTransactor;
import se.scalablesolutions.akka.persistence.common.PersistentMap;
import se.scalablesolutions.akka.persistence.cassandra.CassandraStorage;
import java.nio.ByteBuffer;
public class PersistentSimpleServiceImpl extends TypedTransactor implements PersistentSimpleService {
private String KEY = "COUNTER";
private boolean hasStartedTicking = false;
private PersistentMap<byte[], byte[]> storage;
public String count() {
if (storage == null) storage = CassandraStorage.newMap();
if (!hasStartedTicking) {
storage.put(KEY.getBytes(), ByteBuffer.allocate(4).putInt(0).array());
hasStartedTicking = true;
return "Tick: 0\n";
} else {
byte[] bytes = (byte[])storage.get(KEY.getBytes()).get();
int counter = ByteBuffer.wrap(bytes).getInt();
storage.put(KEY.getBytes(), ByteBuffer.allocate(4).putInt(counter + 1).array());
return "Tick: " + counter + "\n";
}
}
@Override
public void preRestart(Throwable cause) {
System.out.println("Prepare for restart by supervisor");
}
@Override
public void postRestart(Throwable cause) {
System.out.println("Reinitialize after restart by supervisor");
}
}