Skip to content

Instantly share code, notes, and snippets.

@mather
Created July 1, 2014 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mather/d0152f2e0b3d6ff6f079 to your computer and use it in GitHub Desktop.
Save mather/d0152f2e0b3d6ff6f079 to your computer and use it in GitHub Desktop.
Configuration for Akka application ref: http://qiita.com/mather314/items/a3cd9dd1f3c4cfea6fbf
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)
}
import akka.actor.Actor
class SampleActor extends Actor {
val ext = SampleExtension(system)
def hoge = ext.hoge //=> "HOGE"
}
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