Advertisement
Zvezdochet

RequestDispatcher

Apr 17th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.17 KB | None | 0 0
  1. class RequestDispatcher extends Actor{
  2.  
  3.     self.faultHandler =  OneForOneStrategy(List(classOf[Exception]), 5, 5000)
  4.  
  5.     val sessions = collection.mutable.HashMap.empty[String, ActorRef]
  6.     protected def receive = {
  7.         case Request(ctx) => {
  8.             var userId = ctx.param("userId")
  9.             if(userId == null) userId = UUID.randomUUID.toString
  10.  
  11.  
  12.             val session = sessions.getOrElseUpdate(userId, {
  13.                 val a = Actor.actorOf(new UserSession(userId))
  14.                 self.startLink(a)
  15.                 a
  16.             })
  17.  
  18.             ctx.ctx.addListener(new AsyncListener(){
  19.                 def onStartAsync(p1: AsyncEvent) {}
  20.  
  21.                 def onError(p1: AsyncEvent) {
  22.                     session ! CloseRequest()
  23.                 }
  24.  
  25.                 def onTimeout(p1: AsyncEvent) {
  26.                 }
  27.  
  28.                 def onComplete(p1: AsyncEvent) {}
  29.             })
  30.  
  31.             session ! Request(ctx)
  32.  
  33.         }
  34.  
  35.         case CloseSession(userId) => {
  36.             for(s <- sessions.get(userId)) {
  37.                 self.unlink(s)
  38.                 s.stop()
  39.                 sessions -= userId
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement