diff --git a/akka-samples-chat/src/main/scala/ChatServer.scala b/akka-samples-chat/src/main/scala/ChatServer.scala index 039cece3fa..44c1da7ab4 100644 --- a/akka-samples-chat/src/main/scala/ChatServer.scala +++ b/akka-samples-chat/src/main/scala/ChatServer.scala @@ -1,4 +1,4 @@ -/** +/**ChatStorage * Copyright (C) 2009-2010 Scalable Solutions AB . */ @@ -67,9 +67,34 @@ class Session(user: String, storage: Actor) extends Actor { } /** - * Abstraction of Chat storage holding the chat log. + * Abstraction of chat storage holding the chat log. */ -trait Storage +trait ChatStorage extends Actor + +/** + * Redis-backed chat storage implementation. + */ +class RedisChatStorage extends ChatStorage { + lifeCycle = Some(LifeCycle(Permanent)) + + private val chatLog = RedisStorage.getVector("akka.chat.log") + + log.info("Redis-based chat storage is starting up...") + + def receive = { + case msg @ ChatMessage(from, message) => + log.debug("New chat message [%s]", message) + atomic { + chatLog + message.getBytes("UTF-8") + } + + case GetChatLog(_) => + val messageList = atomic { + chatLog.map(bytes => new String(bytes, "UTF-8")).toList + } + reply(ChatLog(messageList)) + } +} /** * Implements user session management. @@ -78,7 +103,7 @@ trait Storage */ trait SessionManagement { this: Actor => - val storage: Storage // needs someone to provide the Storage + val storage: ChatStorage // needs someone to provide the ChatStorage val sessions = new HashMap[String, Actor] protected def sessionManagement: PartialFunction[Any, Unit] = { @@ -113,6 +138,13 @@ trait ChatManagement { this: Actor => } } +/** + * Creates and links a RedisChatStorage. + */ +trait RedisChatStorageFactory { this: Actor => + val storage: ChatStorage = spawnLink(classOf[RedisChatStorage]) // starts and links ChatStorage +} + /** * Chat server. Manages sessions and redirects all other messages to the Session for the client. */ @@ -120,7 +152,7 @@ trait ChatServer extends Actor { faultHandler = Some(OneForOneStrategy(5, 5000)) trapExit = List(classOf[Exception]) - val storage: Storage = spawnLink(classOf[Storage]) // starts and links Storage + val storage: ChatStorage log.info("Chat service is starting up...") @@ -140,35 +172,14 @@ trait ChatServer extends Actor { } } -/** - * Redis-backed storage implementation. - */ -class RedisStorage extends Actor with Storage { - lifeCycle = Some(LifeCycle(Permanent)) - - private val chatLog = RedisStorage.getVector("akka.chat.log") - - log.info("Redis-based chat storage is starting up...") - - def receive = { - case msg @ ChatMessage(from, message) => - log.debug("New chat message [%s]", message) - atomic { - chatLog + message.getBytes("UTF-8") - } - - case GetChatLog(_) => - val messageList = atomic { - chatLog.map(bytes => new String(bytes, "UTF-8")).toList - } - reply(ChatLog(messageList)) - } -} - /** * Object encapsulating the full Chat Service. */ -object ChatService extends ChatServer with SessionManagement with ChatManagement +object ChatService extends + ChatServer with + SessionManagement with + ChatManagement with + RedisChatStorageFactory /** * Test runner emulating a chat session. diff --git a/pom.xml b/pom.xml index 74aac9e4a7..c59044e37a 100755 --- a/pom.xml +++ b/pom.xml @@ -273,6 +273,9 @@ **/*Test.java + + **/InMemNestedStateTest.java + akka.home