refactoring of persistence implementation and its api

This commit is contained in:
Jonas Bonér 2009-12-05 20:59:15 +01:00
parent 7ad879d86f
commit 5edc93315d
19 changed files with 647 additions and 663 deletions

View file

@ -4,8 +4,8 @@
package sample.scala
import se.scalablesolutions.akka.state.{PersistentState, TransactionalState, CassandraStorageConfig}
import se.scalablesolutions.akka.actor.{SupervisorFactory, Actor}
import se.scalablesolutions.akka.state.{CassandraStorage, TransactionalState}
import se.scalablesolutions.akka.config.ScalaConfig._
import se.scalablesolutions.akka.util.Logging
@ -16,6 +16,7 @@ import javax.ws.rs.{GET, POST, Path, Produces, WebApplicationException, Consumes
import org.atmosphere.core.annotation.{Broadcast, Suspend}
import org.atmosphere.util.XSSHtmlFilter
import org.atmosphere.cpr.BroadcastFilter
import java.nio.ByteBuffer
class Boot {
val factory = SupervisorFactory(
@ -81,7 +82,7 @@ class PersistentSimpleService extends Actor {
case object Tick
private val KEY = "COUNTER"
private var hasStartedTicking = false
private val storage = PersistentState.newMap(CassandraStorageConfig())
private val storage = CassandraStorage.newMap
@GET
@Produces(Array("text/html"))
@ -89,11 +90,12 @@ class PersistentSimpleService extends Actor {
def receive = {
case Tick => if (hasStartedTicking) {
val counter = storage.get(KEY).get.asInstanceOf[Integer].intValue
storage.put(KEY, new Integer(counter + 1))
val bytes = storage.get(KEY.getBytes).get
val counter = ByteBuffer.wrap(bytes).getInt
storage.put(KEY.getBytes, ByteBuffer.allocate(4).putInt(counter + 1).array)
reply(<success>Tick:{counter + 1}</success>)
} else {
storage.put(KEY, new Integer(0))
storage.put(KEY.getBytes, Array(0.toByte))
hasStartedTicking = true
reply(<success>Tick: 0</success>)
}