Fixing case 334

This commit is contained in:
Viktor Klang 2010-07-19 21:47:28 +02:00
parent 1310a982a4
commit 96533ee381
5 changed files with 65 additions and 41 deletions

View file

@ -1,35 +1,24 @@
package sample.lift
import se.scalablesolutions.akka.actor.{Transactor, Actor}
import se.scalablesolutions.akka.actor._
import se.scalablesolutions.akka.actor.Actor._
import se.scalablesolutions.akka.config.ScalaConfig._
import se.scalablesolutions.akka.stm.TransactionalMap
import se.scalablesolutions.akka.persistence.cassandra.CassandraStorage
import Actor._
import scala.xml.Node
import java.lang.Integer
import javax.ws.rs.{GET, Path, Produces}
import java.nio.ByteBuffer
import net.liftweb.http._
import net.liftweb.http.rest._
/**
* Try service out by invoking (multiple times):
* <pre>
* curl http://localhost:9998/liftcount
* </pre>
* Or browse to the URL from a web browser.
*/
@Path("/liftcount")
class SimpleService extends Transactor {
case object Tick
class SimpleServiceActor extends Transactor {
private val KEY = "COUNTER"
private var hasStartedTicking = false
private lazy val storage = TransactionalMap[String, Integer]()
@GET
@Produces(Array("text/html"))
def count = (self !! Tick).getOrElse(<h1>Error in counter</h1>)
def receive = {
case Tick => if (hasStartedTicking) {
case "Tick" => if (hasStartedTicking) {
val counter = storage.get(KEY).get.asInstanceOf[Integer].intValue
storage.put(KEY, new Integer(counter + 1))
self.reply(<h1>Tick: {counter + 1}</h1>)
@ -41,27 +30,14 @@ class SimpleService extends Transactor {
}
}
/**
* Try service out by invoking (multiple times):
* <pre>
* curl http://localhost:9998/persistentliftcount
* </pre>
* Or browse to the URL from a web browser.
*/
@Path("/persistentliftcount")
class PersistentSimpleService extends Transactor {
class PersistentServiceActor extends Transactor {
case object Tick
private val KEY = "COUNTER"
private var hasStartedTicking = false
private lazy val storage = CassandraStorage.newMap
@GET
@Produces(Array("text/html"))
def count = (self !! Tick).getOrElse(<h1>Error in counter</h1>)
def receive = {
case Tick => if (hasStartedTicking) {
case "Tick" => if (hasStartedTicking) {
val bytes = storage.get(KEY.getBytes).get
val counter = ByteBuffer.wrap(bytes).getInt
storage.put(KEY.getBytes, ByteBuffer.allocate(4).putInt(counter + 1).array)
@ -73,3 +49,46 @@ class PersistentSimpleService extends Transactor {
}
}
}
/**
* Try service out by invoking (multiple times):
* <pre>
* curl http://localhost:8080/liftcount
* </pre>
* Or browse to the URL from a web browser.
*/
object SimpleRestService extends RestHelper {
serve {
case Get("liftcount" :: _, req) =>
//Fetch the first actor of type SimpleServiceActor
//Send it the "Tick" message and expect a Node back
val result = for( a <- ActorRegistry.actorsFor(classOf[SimpleServiceActor]).headOption;
r <- (a !! "Tick").as[Node] ) yield r
//Return either the resulting NodeSeq or a default one
(result getOrElse <h1>Error in counter</h1>).asInstanceOf[Node]
}
}
/**
* Try service out by invoking (multiple times):
* <pre>
* curl http://localhost:8080/persistentliftcount
* </pre>
* Or browse to the URL from a web browser.
*/
object PersistentRestService extends RestHelper {
serve {
case Get("persistentliftcount" :: _, req) =>
//Fetch the first actor of type SimpleServiceActor
//Send it the "Tick" message and expect a Node back
val result = for( a <- ActorRegistry.actorsFor(classOf[PersistentServiceActor]).headOption;
r <- (a !! "Tick").as[Node] ) yield r
//Return either the resulting NodeSeq or a default one
(result getOrElse <h1>Error in counter</h1>).asInstanceOf[Node]
}
}

View file

@ -13,7 +13,7 @@ import se.scalablesolutions.akka.actor.Actor._
import se.scalablesolutions.akka.config.ScalaConfig._
import se.scalablesolutions.akka.util.Logging
import sample.lift.{PersistentSimpleService, SimpleService}
import sample.lift._
/**
* A class that's instantiated early and run. It allows the application
@ -35,6 +35,8 @@ class Boot extends Logging {
true
}
}
LiftRules.statelessDispatchTable.append(SimpleRestService)
LiftRules.statelessDispatchTable.append(PersistentRestService)
LiftRules.passNotFoundToChain = true
@ -42,10 +44,10 @@ class Boot extends Logging {
SupervisorConfig(
RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
Supervise(
actorOf[SimpleService],
actorOf[SimpleServiceActor],
LifeCycle(Permanent)) ::
Supervise(
actorOf[PersistentSimpleService],
actorOf[PersistentServiceActor],
LifeCycle(Permanent)) ::
Nil))
factory.newInstance.start

View file

@ -13,7 +13,7 @@
</filter-mapping>
<servlet>
<servlet-name>AkkaServlet</servlet-name>
<servlet-class>se.scalablesolutions.akka.rest.AkkaServlet</servlet-class>
<servlet-class>se.scalablesolutions.akka.comet.AkkaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AkkaServlet</servlet-name>

View file

@ -57,7 +57,9 @@ akka {
hostname = "localhost"
port = 9998
filters = ["se.scalablesolutions.akka.security.AkkaSecurityFilterFactory"] # List with all jersey filters to use
resource_packages = ["sample.rest.scala","sample.rest.java","sample.security"] # List with all resource packages for your Jersey services
resource_packages = ["sample.rest.scala",
"sample.rest.java",
"sample.security"] # List with all resource packages for your Jersey services
authenticator = "sample.security.BasicAuthenticationService" # The authentication service to use. Need to be overridden (uses sample now)
#maxInactiveActivity = 60000 #Atmosphere CometSupport maxInactiveActivity
#IF you are using a KerberosAuthenticationActor

View file

@ -310,13 +310,14 @@ class AkkaParent(info: ProjectInfo) extends DefaultProject(info) {
class AkkaSampleChatProject(info: ProjectInfo) extends AkkaDefaultProject(info, deployPath) with CodeFellowPlugin
class AkkaSamplePubSubProject(info: ProjectInfo) extends AkkaDefaultProject(info, deployPath) with CodeFellowPlugin
class AkkaSampleLiftProject(info: ProjectInfo) extends AkkaDefaultProject(info, deployPath) with CodeFellowPlugin {
class AkkaSampleLiftProject(info: ProjectInfo) extends DefaultWebProject(info) with DeployProject with CodeFellowPlugin {
def deployPath = AkkaParent.this.deployPath
val commons_logging = "commons-logging" % "commons-logging" % "1.1.1" % "compile"
val lift = "net.liftweb" % "lift-webkit" % LIFT_VERSION % "compile"
val lift_util = "net.liftweb" % "lift-util" % LIFT_VERSION % "compile"
val servlet = "javax.servlet" % "servlet-api" % "2.5" % "compile"
// testing
val jetty = "org.mortbay.jetty" % "jetty" % "6.1.22" % "test"
val jettyServer = "org.mortbay.jetty" % "jetty" % "6.1.22" % "test"
val junit = "junit" % "junit" % "4.5" % "test"
}
@ -403,7 +404,7 @@ class AkkaParent(info: ProjectInfo) extends DefaultProject(info) {
// ------------------------------------------------------------
class AkkaDefaultProject(info: ProjectInfo, val deployPath: Path) extends DefaultProject(info) with DeployProject
trait DeployProject extends DefaultProject {
trait DeployProject { self: Project =>
// defines where the deployTask copies jars to
def deployPath: Path