为什么 Scala 无法实例化伴随对象?

发布于 2024-12-02 03:35:57 字数 12124 浏览 1 评论 0原文

我是 Scala 和 Akka 的新手,所以如果这是一个新手问题,请原谅我,但我在其他地方找不到答案......

根据记录,我使用 Scala 2.9.0-1 和 Akka 1.1.3 并包含我的 SBT 0.10.1 设置也是如此。

我已经编写了这条消息后面的代码作为 Akka 中的实验;它是用户数据库和注册设施的玩具版本。基本思想是有一个 UserServer Actor 的 ActorPool,每个 Actor 都有一个 MemoryUserDatabase 实例,它使用 STM 与用户电子邮件地址的用户映射进行交互 - 非常简单,对吧?

可以通过编译文件并在两个单独的控制台中运行以下命令来重现该问题:

控制台#1:

导入玩具.service.user._; ServiceRunner.run

控制台#2:

导入玩具.service.user._; ClientRunner.run

这是服务器控制台的输出 (#1)

Aug 31, 2011 5:21:29 PM org.multiverse.api.GlobalStmInstance <clinit>
INFO: Initializing GlobalStmInstance using factoryMethod 'org.multiverse.stms.alpha.AlphaStm.createFast'.
Aug 31, 2011 5:21:29 PM org.multiverse.stms.alpha.AlphaStm <init>
INFO: Created a new AlphaStm instance
Aug 31, 2011 5:21:29 PM org.multiverse.api.GlobalStmInstance <clinit>
INFO: Successfully initialized GlobalStmInstance using factoryMethod 'org.multiverse.stms.alpha.AlphaStm.createFast'.
[ERROR]   [8/31/11 5:21 PM] [akka:event-driven:dispatcher:global-3] [LocalActorRef] Availability(foo)
java.lang.NoClassDefFoundError: Could not initialize class toy.service.user.memory.MemoryUserDatabase$
    at toy.service.user.memory.MemoryUserDatabase$$anonfun$getUser$1.apply(Registration.scala:96)
    at toy.service.user.memory.MemoryUserDatabase$$anonfun$getUser$1.apply(Registration.scala:96)
    at toy.service.user.memory.MemoryUserDatabase$$anonfun$getUser$2.apply(Registration.scala:96)
    at toy.service.user.memory.MemoryUserDatabase$$anonfun$getUser$2.apply(Registration.scala:96)
    at akka.stm.Stm$$anon$1.call(Stm.scala:51)
    at org.multiverse.templates.TransactionBoilerplate.executeWithTransaction(TransactionBoilerplate.java:279)
    at org.multiverse.templates.TransactionBoilerplate.executeChecked(TransactionBoilerplate.java:218)
    at org.multiverse.templates.TransactionBoilerplate.execute(TransactionBoilerplate.java:169)
    at akka.stm.Stm$class.atomic(Stm.scala:50)
    at akka.stm.package$.atomic(package.scala:10)
    at akka.stm.Stm$class.atomic(Stm.scala:47)
    at akka.stm.package$.atomic(package.scala:10)
    at toy.service.user.memory.MemoryUserDatabase.getUser(Registration.scala:95)
    at toy.service.user.UserDatabase$class.available(Registration.scala:88)
    at toy.service.user.memory.MemoryUserDatabase.available(Registration.scala:92)
    at toy.service.user.UserServer$$anonfun$receive$1.apply(Registration.scala:77)
    at toy.service.user.UserServer$$anonfun$receive$1.apply(Registration.scala:76)
    at akka.actor.Actor$class.apply(Actor.scala:478)
    at toy.service.user.UserServer.apply(Registration.scala:74)
    at akka.actor.LocalActorRef.invoke(ActorRef.scala:860)
    at akka.dispatch.MessageInvocation.invoke(MessageHandling.scala:26)
    at akka.dispatch.ExecutableMailbox$class.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:214)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$4.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:120)
    at akka.dispatch.ExecutableMailbox$class.run(ExecutorBasedEventDrivenDispatcher.scala:186)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$4.run(ExecutorBasedEventDrivenDispatcher.scala:120)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
    at akka.dispatch.MonitorableThread.run(ThreadPoolBuilder.scala:181)

使用 Scala 2.9.1.final 时,该错误稍微更有趣:

Aug 31, 2011 5:38:35 PM org.multiverse.api.GlobalStmInstance <clinit>
INFO: Initializing GlobalStmInstance using factoryMethod 'org.multiverse.stms.alpha.AlphaStm.createFast'.
Aug 31, 2011 5:38:35 PM org.multiverse.api.GlobalStmInstance getMethod
INFO: Failed to initialize GlobalStmInstance through System property 'org.multiverse.api.GlobalStmInstance.factoryMethod' with value 'org.multiverse.stms.alpha.AlphaStm'.'org.multiverse.stms.alpha.AlphaStm.createFast' is not an existing class (it can't be found using the Thread.currentThread.getContextClassLoader).
[ERROR]   [8/31/11 5:38 PM] [akka:event-driven:dispatcher:global-3] [LocalActorRef] Availability(foo)
java.lang.ExceptionInInitializerError
    at akka.stm.TransactionFactory.<init>(TransactionFactory.scala:172)
    at akka.stm.TransactionFactory$.apply(TransactionFactory.scala:122)
    at akka.stm.Stm$class.$init$(Stm.scala:44)
    at akka.stm.package$.<init>(package.scala:10)
    at akka.stm.package$.<clinit>(package.scala)
    at toy.service.user.memory.MemoryUserDatabase.getUser(Registration.scala:95)
    at toy.service.user.UserDatabase$class.available(Registration.scala:88)
    at toy.service.user.memory.MemoryUserDatabase.available(Registration.scala:92)
    at toy.service.user.UserServer$$anonfun$receive$1.apply(Registration.scala:77)
    at toy.service.user.UserServer$$anonfun$receive$1.apply(Registration.scala:76)
    at akka.actor.Actor$class.apply(Actor.scala:478)
    at toy.service.user.UserServer.apply(Registration.scala:74)
    at akka.actor.LocalActorRef.invoke(ActorRef.scala:860)
    at akka.dispatch.MessageInvocation.invoke(MessageHandling.scala:26)
    at akka.dispatch.ExecutableMailbox$class.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:214)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$4.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:120)
    at akka.dispatch.ExecutableMailbox$class.run(ExecutorBasedEventDrivenDispatcher.scala:186)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$4.run(ExecutorBasedEventDrivenDispatcher.scala:120)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
    at akka.dispatch.MonitorableThread.run(ThreadPoolBuilder.scala:181)
Caused by: java.lang.IllegalArgumentException: Failed to initialize GlobalStmInstance through System property 'org.multiverse.api.GlobalStmInstance.factoryMethod' with value 'org.multiverse.stms.alpha.AlphaStm'.'org.multiverse.stms.alpha.AlphaStm.createFast' is not an existing class (it can't be found using the Thread.currentThread.getContextClassLoader).
    at org.multiverse.api.GlobalStmInstance.getMethod(GlobalStmInstance.java:82)
    at org.multiverse.api.GlobalStmInstance.<clinit>(GlobalStmInstance.java:38)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: org.multiverse.stms.alpha.AlphaStm
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.multiverse.api.GlobalStmInstance.getMethod(GlobalStmInstance.java:76)
    ... 23 more

代码的此操作部分是:

class MemoryUserDatabase extends UserDatabase {
  import MemoryUserDatabase._

  def getUser(email: String) = atomic {
    users.get.get(email)
  }
  def register(user: User) = atomic {
    getUser(user.email) match {
      case None =>
        users alter (_ + (user.email -> user))
        true
      case Some(found) => false
    }
  }
}
object MemoryUserDatabase {
  import scala.collection.mutable.{ Map => MutMap }
  private val users = Ref(MutMap[String, User]())
}

我不明白为什么不能使用伴随对象已初始化。最奇怪的部分是你可以做同样的事情,但是如果在服务器控制台(#1)中你首先访问伴随对象:

内存用户数据库

在运行 ServerRunner 之前它会初始化得很好,随后其他一切都很好。

谁能解释这是为什么吗?

谢谢! 伊丹

PS.这是我的第一个 Scala 代码,所以尽量不要笑得太厉害......欢迎任何其他建议(风格、哲学、神学......)。

package toy.service.user

import scala.collection.mutable.HashMap

import akka.actor.{ Actor, ActorRef }
import akka.config.Supervision.{ OneForOneStrategy, Permanent }
import Actor._
import akka.routing._
import akka.stm._
import akka.actor.TypedActor
import akka.event.EventHandler

class User(var email: String,
           var password: String) extends Serializable

/** Registration message types.
 */
sealed trait RegistrationMessage
case class Availability(email: String) extends RegistrationMessage
case class GetUser(email: String) extends RegistrationMessage
case class Register(user: User) extends RegistrationMessage

// Client ---------------------------------------
class UserClient(defaultTimeout: Int = 1000) {
  val userService = Actor.remote.actorFor(UserService.USER_SERVICE_ID, "localhost", UserService.USER_SERVICE_PORT)
  EventHandler.info(this, "remote UserService: id(" + userService.id + "), uuid(" + userService.uuid + ")")

  def getUser(email: String, timeout: Int = defaultTimeout): Option[User] = (userService !! (GetUser(email), timeout)).as[User]

  def available(email: String, timeout: Int = defaultTimeout): Boolean =
    (userService !! (Availability(email), timeout)).as[Boolean].getOrElse(throw new RuntimeException("Oi!"))

  def register(user: User, timeout: Int = defaultTimeout): Boolean =
    (userService !! (Register(user), timeout)).as[Boolean].getOrElse(throw new RuntimeException("Got bogus (None) response from " + UserService.USER_SERVICE_ID))
}

// Service Pool ---------------------------------
object UserService {
  val USER_SERVICE_ID = "user:service"
  val USER_SERVICE_PORT = 2662
  val host = "localhost"
}

class UserService extends Actor
  with DefaultActorPool
  with BoundedCapacityStrategy
  with MailboxPressureCapacitor
  with SmallestMailboxSelector
  with BasicFilter {

  import toy.service.user.memory._

  def receive = _route // DefaultActorPool's receive
  def lowerBound = 1
  def upperBound = 5
  def pressureThreshold = 1
  def partialFill = true // never send duplicate messages to same actor (only meaningful if selectionCount > 1)
  def selectionCount = 1 // How many in pool should receive each message
  def rampupRate = 0.1 // increase by 10% capacity (# num actors)
  def backoffRate = 0.50 // halve capacity once backoffThreshold is reached
  def backoffThreshold = 0.50
  def instance = actorOf(new UserServer(new MemoryUserDatabase))

  override def preStart() {
    import UserService.{ host, USER_SERVICE_ID, USER_SERVICE_PORT }

    remote.start(host, UserService.USER_SERVICE_PORT);
    remote.register(UserService.USER_SERVICE_ID, self) //Register the actorPool with the specified service id
    EventHandler.info(this, "Prestart: Started UserService(" + self.uuid + ") on %s:%s".format(host, UserService.USER_SERVICE_PORT.toString()))
  }
}

// Service --------------------------------------
class UserServer(db: UserDatabase) extends Actor {

  def receive = {
    case Availability(email) => self.reply(db.available(email))
    case GetUser(email)      => self.reply(db.getUser(email))
    case Register(user)      => self.reply(db.register(user))
  }
}

// Database -------------------------------------
trait UserDatabase {
  def getUser(email: String): Option[User]
  def register(user: User): Boolean

  def available(email: String): Boolean = getUser(email) == None
}

package memory {
  class MemoryUserDatabase extends UserDatabase {
    import MemoryUserDatabase._

    def getUser(email: String) = atomic {
      users.get.get(email)
    }

    def register(user: User) = atomic {
      getUser(user.email) match {
        case None =>
          users alter (_ + (user.email -> user))
          true
        case Some(found) => false
      }
    }
  }

  object MemoryUserDatabase {
    import scala.collection.mutable.{ Map => MutMap }

    private val users = Ref(MutMap[String, User]())
  }
}

object ServerRunner {
  def run() {
    actorOf[UserService].start()
  }
}

object ClientRunner {
  def run {
    val client = new UserClient
    EventHandler.info(this, client.available("foo"))
  }
}

I am new to Scala and Akka so forgive me if this is a newb question, but I can't find the answer anywhere else...

For the record I am using Scala 2.9.0-1 and Akka 1.1.3 and have included my SBT 0.10.1 setup as well.

I have written the code that follows this message as an experiment in Akka; it is a toy version of a user database and registration facilities. The basic idea is there is an ActorPool of UserServer actors each of which have an instance of a MemoryUserDatabase which uses STM to interact with a Map of Users keyed off the users' email addresses - pretty simple, right?

The problem can be reproduced by compiling the file and running the following in two separate consoles:

Console #1:

import toy.service.user._;
ServiceRunner.run

Console #2:

import toy.service.user._;
ClientRunner.run

This is the output from the server console (#1)

Aug 31, 2011 5:21:29 PM org.multiverse.api.GlobalStmInstance <clinit>
INFO: Initializing GlobalStmInstance using factoryMethod 'org.multiverse.stms.alpha.AlphaStm.createFast'.
Aug 31, 2011 5:21:29 PM org.multiverse.stms.alpha.AlphaStm <init>
INFO: Created a new AlphaStm instance
Aug 31, 2011 5:21:29 PM org.multiverse.api.GlobalStmInstance <clinit>
INFO: Successfully initialized GlobalStmInstance using factoryMethod 'org.multiverse.stms.alpha.AlphaStm.createFast'.
[ERROR]   [8/31/11 5:21 PM] [akka:event-driven:dispatcher:global-3] [LocalActorRef] Availability(foo)
java.lang.NoClassDefFoundError: Could not initialize class toy.service.user.memory.MemoryUserDatabase$
    at toy.service.user.memory.MemoryUserDatabase$anonfun$getUser$1.apply(Registration.scala:96)
    at toy.service.user.memory.MemoryUserDatabase$anonfun$getUser$1.apply(Registration.scala:96)
    at toy.service.user.memory.MemoryUserDatabase$anonfun$getUser$2.apply(Registration.scala:96)
    at toy.service.user.memory.MemoryUserDatabase$anonfun$getUser$2.apply(Registration.scala:96)
    at akka.stm.Stm$anon$1.call(Stm.scala:51)
    at org.multiverse.templates.TransactionBoilerplate.executeWithTransaction(TransactionBoilerplate.java:279)
    at org.multiverse.templates.TransactionBoilerplate.executeChecked(TransactionBoilerplate.java:218)
    at org.multiverse.templates.TransactionBoilerplate.execute(TransactionBoilerplate.java:169)
    at akka.stm.Stm$class.atomic(Stm.scala:50)
    at akka.stm.package$.atomic(package.scala:10)
    at akka.stm.Stm$class.atomic(Stm.scala:47)
    at akka.stm.package$.atomic(package.scala:10)
    at toy.service.user.memory.MemoryUserDatabase.getUser(Registration.scala:95)
    at toy.service.user.UserDatabase$class.available(Registration.scala:88)
    at toy.service.user.memory.MemoryUserDatabase.available(Registration.scala:92)
    at toy.service.user.UserServer$anonfun$receive$1.apply(Registration.scala:77)
    at toy.service.user.UserServer$anonfun$receive$1.apply(Registration.scala:76)
    at akka.actor.Actor$class.apply(Actor.scala:478)
    at toy.service.user.UserServer.apply(Registration.scala:74)
    at akka.actor.LocalActorRef.invoke(ActorRef.scala:860)
    at akka.dispatch.MessageInvocation.invoke(MessageHandling.scala:26)
    at akka.dispatch.ExecutableMailbox$class.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:214)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$anon$4.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:120)
    at akka.dispatch.ExecutableMailbox$class.run(ExecutorBasedEventDrivenDispatcher.scala:186)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$anon$4.run(ExecutorBasedEventDrivenDispatcher.scala:120)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
    at akka.dispatch.MonitorableThread.run(ThreadPoolBuilder.scala:181)

The error is slightly more interesting using Scala 2.9.1.final:

Aug 31, 2011 5:38:35 PM org.multiverse.api.GlobalStmInstance <clinit>
INFO: Initializing GlobalStmInstance using factoryMethod 'org.multiverse.stms.alpha.AlphaStm.createFast'.
Aug 31, 2011 5:38:35 PM org.multiverse.api.GlobalStmInstance getMethod
INFO: Failed to initialize GlobalStmInstance through System property 'org.multiverse.api.GlobalStmInstance.factoryMethod' with value 'org.multiverse.stms.alpha.AlphaStm'.'org.multiverse.stms.alpha.AlphaStm.createFast' is not an existing class (it can't be found using the Thread.currentThread.getContextClassLoader).
[ERROR]   [8/31/11 5:38 PM] [akka:event-driven:dispatcher:global-3] [LocalActorRef] Availability(foo)
java.lang.ExceptionInInitializerError
    at akka.stm.TransactionFactory.<init>(TransactionFactory.scala:172)
    at akka.stm.TransactionFactory$.apply(TransactionFactory.scala:122)
    at akka.stm.Stm$class.$init$(Stm.scala:44)
    at akka.stm.package$.<init>(package.scala:10)
    at akka.stm.package$.<clinit>(package.scala)
    at toy.service.user.memory.MemoryUserDatabase.getUser(Registration.scala:95)
    at toy.service.user.UserDatabase$class.available(Registration.scala:88)
    at toy.service.user.memory.MemoryUserDatabase.available(Registration.scala:92)
    at toy.service.user.UserServer$anonfun$receive$1.apply(Registration.scala:77)
    at toy.service.user.UserServer$anonfun$receive$1.apply(Registration.scala:76)
    at akka.actor.Actor$class.apply(Actor.scala:478)
    at toy.service.user.UserServer.apply(Registration.scala:74)
    at akka.actor.LocalActorRef.invoke(ActorRef.scala:860)
    at akka.dispatch.MessageInvocation.invoke(MessageHandling.scala:26)
    at akka.dispatch.ExecutableMailbox$class.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:214)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$anon$4.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:120)
    at akka.dispatch.ExecutableMailbox$class.run(ExecutorBasedEventDrivenDispatcher.scala:186)
    at akka.dispatch.ExecutorBasedEventDrivenDispatcher$anon$4.run(ExecutorBasedEventDrivenDispatcher.scala:120)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
    at akka.dispatch.MonitorableThread.run(ThreadPoolBuilder.scala:181)
Caused by: java.lang.IllegalArgumentException: Failed to initialize GlobalStmInstance through System property 'org.multiverse.api.GlobalStmInstance.factoryMethod' with value 'org.multiverse.stms.alpha.AlphaStm'.'org.multiverse.stms.alpha.AlphaStm.createFast' is not an existing class (it can't be found using the Thread.currentThread.getContextClassLoader).
    at org.multiverse.api.GlobalStmInstance.getMethod(GlobalStmInstance.java:82)
    at org.multiverse.api.GlobalStmInstance.<clinit>(GlobalStmInstance.java:38)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: org.multiverse.stms.alpha.AlphaStm
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.multiverse.api.GlobalStmInstance.getMethod(GlobalStmInstance.java:76)
    ... 23 more

This operative part of the code is:

class MemoryUserDatabase extends UserDatabase {
  import MemoryUserDatabase._

  def getUser(email: String) = atomic {
    users.get.get(email)
  }
  def register(user: User) = atomic {
    getUser(user.email) match {
      case None =>
        users alter (_ + (user.email -> user))
        true
      case Some(found) => false
    }
  }
}
object MemoryUserDatabase {
  import scala.collection.mutable.{ Map => MutMap }
  private val users = Ref(MutMap[String, User]())
}

I don't understand why the Companion Object can't be initialized. The strangest part is that you can do the same thing, but if in the server console (#1) you first access the Companion Object:

MemoryUserDatabase

before you run the ServerRunner it initializes just fine and subsequently everything else is just dandy.

Can anyone explain why this is?

Thanks!
Idan

PS. This is my first Scala code so try not to laugh too hard... and any other suggestions (stylistic, philisophical, theological, ...) are welcome.

package toy.service.user

import scala.collection.mutable.HashMap

import akka.actor.{ Actor, ActorRef }
import akka.config.Supervision.{ OneForOneStrategy, Permanent }
import Actor._
import akka.routing._
import akka.stm._
import akka.actor.TypedActor
import akka.event.EventHandler

class User(var email: String,
           var password: String) extends Serializable

/** Registration message types.
 */
sealed trait RegistrationMessage
case class Availability(email: String) extends RegistrationMessage
case class GetUser(email: String) extends RegistrationMessage
case class Register(user: User) extends RegistrationMessage

// Client ---------------------------------------
class UserClient(defaultTimeout: Int = 1000) {
  val userService = Actor.remote.actorFor(UserService.USER_SERVICE_ID, "localhost", UserService.USER_SERVICE_PORT)
  EventHandler.info(this, "remote UserService: id(" + userService.id + "), uuid(" + userService.uuid + ")")

  def getUser(email: String, timeout: Int = defaultTimeout): Option[User] = (userService !! (GetUser(email), timeout)).as[User]

  def available(email: String, timeout: Int = defaultTimeout): Boolean =
    (userService !! (Availability(email), timeout)).as[Boolean].getOrElse(throw new RuntimeException("Oi!"))

  def register(user: User, timeout: Int = defaultTimeout): Boolean =
    (userService !! (Register(user), timeout)).as[Boolean].getOrElse(throw new RuntimeException("Got bogus (None) response from " + UserService.USER_SERVICE_ID))
}

// Service Pool ---------------------------------
object UserService {
  val USER_SERVICE_ID = "user:service"
  val USER_SERVICE_PORT = 2662
  val host = "localhost"
}

class UserService extends Actor
  with DefaultActorPool
  with BoundedCapacityStrategy
  with MailboxPressureCapacitor
  with SmallestMailboxSelector
  with BasicFilter {

  import toy.service.user.memory._

  def receive = _route // DefaultActorPool's receive
  def lowerBound = 1
  def upperBound = 5
  def pressureThreshold = 1
  def partialFill = true // never send duplicate messages to same actor (only meaningful if selectionCount > 1)
  def selectionCount = 1 // How many in pool should receive each message
  def rampupRate = 0.1 // increase by 10% capacity (# num actors)
  def backoffRate = 0.50 // halve capacity once backoffThreshold is reached
  def backoffThreshold = 0.50
  def instance = actorOf(new UserServer(new MemoryUserDatabase))

  override def preStart() {
    import UserService.{ host, USER_SERVICE_ID, USER_SERVICE_PORT }

    remote.start(host, UserService.USER_SERVICE_PORT);
    remote.register(UserService.USER_SERVICE_ID, self) //Register the actorPool with the specified service id
    EventHandler.info(this, "Prestart: Started UserService(" + self.uuid + ") on %s:%s".format(host, UserService.USER_SERVICE_PORT.toString()))
  }
}

// Service --------------------------------------
class UserServer(db: UserDatabase) extends Actor {

  def receive = {
    case Availability(email) => self.reply(db.available(email))
    case GetUser(email)      => self.reply(db.getUser(email))
    case Register(user)      => self.reply(db.register(user))
  }
}

// Database -------------------------------------
trait UserDatabase {
  def getUser(email: String): Option[User]
  def register(user: User): Boolean

  def available(email: String): Boolean = getUser(email) == None
}

package memory {
  class MemoryUserDatabase extends UserDatabase {
    import MemoryUserDatabase._

    def getUser(email: String) = atomic {
      users.get.get(email)
    }

    def register(user: User) = atomic {
      getUser(user.email) match {
        case None =>
          users alter (_ + (user.email -> user))
          true
        case Some(found) => false
      }
    }
  }

  object MemoryUserDatabase {
    import scala.collection.mutable.{ Map => MutMap }

    private val users = Ref(MutMap[String, User]())
  }
}

object ServerRunner {
  def run() {
    actorOf[UserService].start()
  }
}

object ClientRunner {
  def run {
    val client = new UserClient
    EventHandler.info(this, client.available("foo"))
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年 2024-12-09 03:35:57

当我在应用程序启动期间创建的对象的构造函数中初始化 TMap 时,我遇到了类似的问题。不知何故,我的对象是在 STM 静态初始化程序运行之前创建的。我使对象的字段变得懒惰,它解决了问题。

...实际上它只是将问题推迟到程序的后期。咕噜咕噜。

I had a similar problem when I was initializing a TMap in a constructor of an object created during the booting of my app. Somehow my object was being created before the STM static initializers ran. I made my object's field lazy and it fixed the problem.

... actually it just delayed the problem to later in the program. Grrrr.

十秒萌定你 2024-12-09 03:35:57

我遇到了同样的问题。添加依赖项“org.multiverse.multiverse-alpha-unborn”为我解决了这个问题。

I ran into the same problem. Adding the dependency 'org.multiverse.multiverse-alpha-unborn' solved the issue for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文