Created
July 1, 2014 03:12
-
-
Save mather/d0152f2e0b3d6ff6f079 to your computer and use it in GitHub Desktop.
Configuration for Akka application ref: http://qiita.com/mather314/items/a3cd9dd1f3c4cfea6fbf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.{ExtendedActorSystem, Extension, ExtensionId} | |
class SampleExtentionImpl(system: ExtendedActorSystem) extends Extension { | |
val hoge = "HOGE" | |
} | |
object SampleExtension extends ExtensionId[SampleExtensionImpl] { | |
def createExtension(system: ExtendedActorSystem) = new SampleExtensionImpl(system) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.Actor | |
class SampleActor extends Actor { | |
val ext = SampleExtension(system) | |
def hoge = ext.hoge //=> "HOGE" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.typesafe.config.Config | |
import akka.actor.ActorSystem | |
// configを定義するtrait | |
trait ConfigSupplier { | |
val config: Config | |
} | |
// configから読み込む設定情報の実装 | |
trait DatabaseSetting { this: ConfigSupplier => | |
import scala.util.Try | |
val dbHost = Try(config.getString("sample.db.host")).getOrElse("localhost") | |
val dbPort = Try(config.getInt("sample.db.port")).getOrElse(5432) | |
} | |
// 設定情報を利用するクラス | |
class SampleApplication { this: DatabaseSetting => | |
val connection = getConnection(dbHost, dbPort) | |
} | |
object Main extends App { | |
val system = ActorSystem() | |
// インスタンス化のときに配線と注入 | |
val app = new SampleApplication with DatabaseSetting with ConfigSupplier { | |
val config = system.settings.config | |
} | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment