zoukankan      html  css  js  c++  java
  • [翻译] AKKA笔记- ACTORSYSTEM (配置CONFIGURATION 与调度SCHEDULING)

    原文在http://rerun.me/2014/10/06/akka-notes-actorsystem-in-progress/

    像我们前面看到的,我们可以用ActorSystemactorof方法来创建Actor。其实你可以用ActorSystem做更多事。我们可以先看下Configuration和Scheduling。

    让我们先看下ActorSystem 的方法。

    1. 配置管理

    还记得前一篇我们用application.conf文件来配置我们的日志级别吗?这个文件跟java里用的.properties文件很像。我们马上会看到我们如何用这个配置文件来自定义我们的dispatchers(分发器), mailboxes(邮箱)等。(我还没好好介绍typesafe config的神奇, 请自己去看一些例子来领略吧)

    所以,当我们不指定任何配置用ActorSystem对象的apply方法创建ActorSystem时,他会自动在classpath的根路径上加载application.conf,application.jsonapplication.properties

    所以,

    val system=ActorSystem("UniversityMessagingSystem")
    
    

    等价于

    val system=ActorSystem("UniversityMessagingSystem", ConfigFactory.load())
    

    想验证这个参数, 只要看下ActorSystem.scala的apply方法

      def apply(name: String, config: Option[Config] = None, classLoader: Option[ClassLoader] = None, defaultExecutionContext: Option[ExecutionContext] = None): ActorSystem = {
        val cl = classLoader.getOrElse(findClassLoader())
        val appConfig = config.getOrElse(ConfigFactory.load(cl))
        new ActorSystemImpl(name, appConfig, cl, defaultExecutionContext).start()
      }
    

    A. 覆盖缺省的配置

    如果你不想使用application.conf(例如在testcase中)或者你想有你自己的自定义配置文件(例如在测试不同的配置文件或部署在不同的环境),你可以用传入你自己的配置文件来覆盖那个在classpath上的配置文件。

    ConfigFactory.parseString是个选择

    val actorSystem=ActorSystem("UniversityMessageSystem", ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]"""))  
    

    或者
    简单的在testcase中写

    class TeacherTestLogListener extends TestKit(ActorSystem("UniversityMessageSystem", ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]""")))  
      with WordSpecLike
      with MustMatchers
      with BeforeAndAfterAll {
    

    还有一个ConfigFactory.load方式

    val system = ActorSystem("UniversityMessageSystem", ConfigFactory.load("uat-application.conf"))  
    

    如果你需要在runtime时访问你自己的配置文件, 你可以这样做:

    val system=ActorSystem("UniversityMessageSystem", ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]"""))  
    println (system.settings.config.getValue("akka.loggers")) // Results in > SimpleConfigList(["akka.testkit.TestEventListener"])
    

    B. 扩展缺省配置

    不同于覆盖,你还可以用扩展的方式来扩展缺省配置文件,只要用ConfigwithFallback方法。

    假如你的application.conf是这样的:

    akka{  
        loggers = ["akka.event.slf4j.Slf4jLogger"]
        loglevel = DEBUG
        arun="hello"
    }
    

    并且你打算这样覆盖你的akka.loggers属性:

       val config=ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]""")
       val system=ActorSystem("UniversityMessageSystem", config.withFallback(ConfigFactory.load()))
    

    最终merge过得配置文件是这样的的

       val config=ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"]""")
       val system=ActorSystem("UniversityMessageSystem", config.withFallback(ConfigFactory.load()))
        ```
    为什么我要说这个配置的事?因为我们的*ActorSystem*是加载并提供存取所有配置信息的点。
    
    ---
    ###重要笔记:
    请注意下falling back的顺序 - 哪一个是缺省,哪一个是扩展配置。 请记住,你需要(fall back)回滚到缺省配置。所以,
    

    config.withFallback(ConfigFactory.load())

    可以工作
    但是
    

    ConfigFactory.load().withFallback(config)

    得不到你想要的结果。
    
      [1]: /img/bVryIX
    
    ---
    文章来自微信平台「麦芽面包」,微信号「darkjune_think」。转载请注明。
  • 相关阅读:
    如何用js得到当前页面的url信息方法(JS获取当前网址信息)
    可拖动大小div案例
    div内div水平垂直居中
    div设置absolute情况下填充剩余宽度
    最近很不顺
    [转载]什么是对象序列化,为什么要使用
    mac下安装eclipse以及python
    Myeclipse 10 for mac 破解版下载安装及破解方法
    IOS7学习之路一(新UI之自定义UITableViewCell)
    Xcode5和ObjC新特性
  • 原文地址:https://www.cnblogs.com/zhukunrong/p/5079717.html
Copyright © 2011-2022 走看看