当前位置: 首页 > news >正文

免费可商用网站正规轻电商网站模板

免费可商用网站,正规轻电商网站模板,南昌开发公司,wordpress熊掌号自动提交在前两部分#xff08; 一 #xff0c; 二 #xff09;中#xff0c;我们简要讨论了Actor以及消息传递的工作方式。 在这一部分中#xff0c;让我们看一下如何修复并记录我们的TeacherActor 。 概括 这就是我们上一部分中的Actor的样子#xff1a; class TeacherActor … 在前两部分 一 二 中我们简要讨论了Actor以及消息传递的工作方式。 在这一部分中让我们看一下如何修复并记录我们的TeacherActor 。 概括 这就是我们上一部分中的Actor的样子 class TeacherActor extends Actor {val quotes List(Moderation is for cowards,Anything worth doing is worth overdoing,The trouble is you think you have time,You never gonna know if you never even try)def receive {case QuoteRequest {import util.Random//Get a random Quote from the list and construct a responseval quoteResponseQuoteResponse(quotes(Random.nextInt(quotes.size)))println (quoteResponse)}} }使用SLF4J记录Akka 您会注意到在代码中我们正在将quoteResponse打印到标准输出您显然会认为这是一个坏主意。 让我们通过启用SLF4J Facade日志进行修复。 1.修复类以使用日志记录 Akka提供了一个很好的小特征ActorLogging来实现它。 让我们混合 class TeacherLogActor extends Actor with ActorLogging {val quotes List(Moderation is for cowards,Anything worth doing is worth overdoing,The trouble is you think you have time,You never gonna know if you never even try)def receive {case QuoteRequest {import util.Random//get a random element (for now)val quoteResponseQuoteResponse(quotes(Random.nextInt(quotes.size)))log.info(quoteResponse.toString())}}//Well cover the purpose of this method in the Testing sectiondef quoteListquotes} 绕道走一圈 在内部当我们记录一条消息时ActorLogging中的记录方法最终将日志消息发布到EventStream 。 是的我确实说过publish 。 那么EventStream实际上是什么 EventStream和记录 EventStream行为就像我们可以向其发布和接收消息的消息代理一样。 与常规MOM的一个细微区别是EventStream的订阅者只能是Actor。 在记录消息的情况下所有日志消息都将发布到EventStream。 默认情况下订阅这些消息的Actor是DefaultLogger 它仅将消息打印到标准输出中。 class DefaultLogger extends Actor with StdOutLogger { override def receive: Receive {...case event: LogEvent ⇒ print(event)} } 因此这就是我们尝试启动StudentSimulatorApp的原因我们看到了写入控制台的日志消息。 也就是说Eve​​ntStream不仅适合于记录。 它是VM内ActorWorld内部可用的通用发布订阅机制稍后会详细介绍。 返回SLF4J设置 2.将Akka配置为使用SLF4J akka{ loggers [akka.event.slf4j.Slf4jLogger]loglevel DEBUGlogging-filter akka.event.slf4j.Slf4jLoggingFilter } 我们将此信息存储在一个名为application.conf的文件中该文件应该在您的类路径中。 在我们的sbt文件夹结构中我们会将其放在您的main/resources目录中。 从配置中我们可以得出 loggers属性指示将要订阅日志事件的Actor。 Slf4jLogger所做的只是简单地使用日志消息并将其委托给SLF4J Logger外观。 loglevel属性仅指示记录日志时应考虑的最低级别。 logging-filter比较当前配置的日志loglevel和传入的日志消息级别并在发布到EventStream之前剔除配置的日志级别以下的任何日志消息。 但是为什么前面的示例没有提供application.conf 仅仅是因为Akka提供了一些合理的默认值所以我们在开始使用它之前不需要构建配置文件。 为了自定义各种内容我们将在这里经常重新访问该文件。 您可以在application.conf内部使用一堆很棒的参数来单独记录日志。 它们在这里详细解释。 3.放入logback.xml 现在我们将配置一个由logback支持的SLF4J记录器。 ?xml version1.0 encodingUTF-8? configuration appender nameFILEclassch.qos.logback.core.rolling.RollingFileAppenderencoderpattern%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n/pattern/encoderrollingPolicy classch.qos.logback.core.rolling.TimeBasedRollingPolicyfileNamePatternlogs\akka.%d{yyyy-MM-dd}.%i.log/fileNamePatterntimeBasedFileNamingAndTriggeringPolicy classch.qos.logback.core.rolling.SizeAndTimeBasedFNATPmaxFileSize50MB/maxFileSize/timeBasedFileNamingAndTriggeringPolicy/rollingPolicy/appenderroot levelDEBUGappender-ref refFILE //root /configuration 我也将它与application.conf一起放入了main/resources文件夹中。 请确保main/resources现在位于Eclipse或其他IDE的类路径中。 还应将logback和slf4j-api包含到build.sbt中 。 而当我们揭开序幕我们StudentSimulatorApp和发送邮件到我们的新TeacherLogActor 该akkaxxxxx.log文件我们配置看起来像这样。 测试Akka 请注意这绝不是Test Akka的详尽介绍。 我们将在以下各部分中相应标题下的“测试”的更多功能上构建测试。 这些测试用例旨在覆盖我们之前编写的Actor。 当StudentSimulatorApp我们的需求时您将同意应将其从测试用例中删除。 为了减轻测试的痛苦Akka提出了一个了不起的测试工具包使用它我们可以做一些神奇的事情例如直接探查Actor实现的内部。 聊够了让我们看看测试用例。 首先让我们尝试将StudentSimulatorApp映射到测试用例。 现在让我们单独看一下声明。 class TeacherPreTest extends TestKit(ActorSystem(UniversityMessageSystem)) with WordSpecLikewith MustMatcherswith BeforeAndAfterAll { 因此从TestCase类的定义中我们看到 TestKit特性接受一个ActorSystem通过它我们可以创建Actor。 在内部TestKit装饰ActorSystem并替换默认的调度程序。 我们使用WordSpec 这是使用ScalaTest编写测试用例的许多有趣方式之一。 MustMatchers提供方便的方法来使测试用例看起来像自然语言 在测试用例完成之后我们混入BeforeAndAfterAll以关闭ActorSystem。 特质提供的afterAll方法更像我们在JUnit中的tearDown 1、2 –向演员发送消息 第一个测试用例只是向PrintActor发送一条消息。 它没有断言 第二种情况将消息发送到Log actor后者使用ActorLogging的log字段将消息发布到EventStream。 这也没有断言 //1. Sends message to the Print Actor. Not even a testcase actuallyA teacher must {print a quote when a QuoteRequest message is sent in {val teacherRef TestActorRef[TeacherActor]teacherRef ! QuoteRequest}}//2. Sends message to the Log Actor. Again, not a testcase per seA teacher with ActorLogging must {log a quote when a QuoteRequest message is sent in {val teacherRef TestActorRef[TeacherLogActor]teacherRef ! QuoteRequest}3 –维护参与者的内部状态 第三种情况使用TestActorRef的underlyingActor quoteList方法并调用TeacherActor的quoteList方法。 quoteList方法返回引号列表。 我们使用此列表来声明其大小。 如果对quoteList引用使您退缩请参考上面列出的TeacherLogActor代码并查找 //From TeacherLogActor //Well cover the purpose of this method in the Testing sectiondef quoteListquotes//3. Asserts the internal State of the Log Actor. have a quote list of size 4 in {val teacherRef TestActorRef[TeacherLogActor]teacherRef.underlyingActor.quoteList must have size (4)teacherRef.underlyingActor.quoteList must have size (4)}4 –声明日志消息 正如我们前面在EventStream和Logging部分上文中所讨论的所有日志消息都发送到EventStream 并且SLF4JLogger订阅并使用其附加程序将其写入日志文件/控制台等。直接在我们的测试用例中使用EventStream并断言日志消息本身的存在 看起来我们也可以做到。 这涉及两个步骤 您需要像这样向您的TestKit添加额外的配置 class TeacherTest extends TestKit(ActorSystem(UniversityMessageSystem, ConfigFactory.parseString(akka.loggers [akka.testkit.TestEventListener]))) with WordSpecLikewith MustMatcherswith BeforeAndAfterAll { 现在我们已经订阅了EventStream我们可以从测试用例中将其声明为 //4. Verifying log messages from eventStreambe verifiable via EventFilter in response to a QuoteRequest that is sent in {val teacherRef TestActorRef[TeacherLogActor]EventFilter.info(pattern QuoteResponse*, occurrences 1) intercept {teacherRef ! QuoteRequest}} EventFilter.info块仅拦截1条以QuoteResponse patternQuoteResponse* 开头的日志消息。 您也可以通过使用startQuoteResponse 。如果没有消息发送给TeacherLogActor则该测试用例将失败。 5 –使用构造函数参数测试Actor 请注意我们在测试用例中创建Actor的方式是通过TestActorRef[TeacherLogActor]而不是通过system.actorOf 。 这只是为了使我们可以通过TeacherActorRef中的underlyingActor Actor方法访问Actor的内部。 我们将无法通过在常规运行时可以访问的ActorRef来实现此ActorRef 。 这并没有给我们在生产中使用TestActorRef的任何借口。您会被追捕的。 如果Actor接受参数那么我们创建TestActorRef的方式将是 val teacherRef TestActorRef(new TeacherLogParameterActor(quotes)) 整个测试用例将如下所示 //5. have a quote list of the same size as the input parameter have a quote list of the same size as the input parameter in {val quotes List(Moderation is for cowards,Anything worth doing is worth overdoing,The trouble is you think you have time,You never gonna know if you never even try)val teacherRef TestActorRef(new TeacherLogParameterActor(quotes))//val teacherRef TestActorRef(Props(new TeacherLogParameterActor(quotes)))teacherRef.underlyingActor.quoteList must have size (4)EventFilter.info(pattern QuoteResponse*, occurrences 1) intercept {teacherRef ! QuoteRequest}}关闭ActorSystem 最后 afterAll生命周期方法 override def afterAll() { super.afterAll()system.shutdown()}码 与往常一样可以从github这里下载整个项目。 翻译自: https://www.javacodegeeks.com/2014/10/akka-notes-actor-logging-and-testing.html
http://www.zqtcl.cn/news/387954/

相关文章:

  • 深圳外贸商城网站建设wordpress 空搜索
  • 做微信的网站有哪些shop商城系统
  • 网站落地页如何做优化大师免费下载安装
  • 本地计算机做网站服务器做算命网站
  • 广州网站建设公司万齐网络科技做围棋题网站
  • 运动服装商城网站建设引流推广
  • 武进区城乡建设局网站聊城商城网站建设
  • 做网站开发赚钱吗网站建设电子书资料
  • wordpress 回收站在哪个文件夹建站之星模板好吗
  • 怎么用dw做博客网站天使投资平台官网
  • 淮安市网站建设crm网站
  • 门户网站主要特点和功能深圳地铁优化
  • 银川网站推广方式湖南建工交通建设有限公司网站
  • 知道网站域名怎么联系怎么创建自己的公司网站
  • 淘宝网站开发多少金额网站优化 福州
  • 百度推广让我先做虚拟网站后进一步优化落实
  • 好的网站建设启示汕头网页设计网站方案
  • 深圳网站制作开发免费精准客户软件
  • 网站超链接用什么南宁行业平台开发公司
  • 注册门户网站襄樊seo快速排名
  • 优秀的手机网站iis 设置此网站的访问权限
  • 用nat123做自己的网站深圳市建设工程质量检测中心官网
  • 网上做衣服的网站废旧网站哪个做的最好
  • 网站开发设置网页端口wordpress 知识库
  • 网站建设的方法有四种开发一款新闻app需要多少钱
  • 成都网站建站公司2023年防疫新政策
  • 17做网店一样的网站十大互联网培训机构
  • 中企网络科技建站施工企业oa办公系统
  • 成都网站推广公司排名淘宝商家网站建设
  • 平台网站建设报价网站建设企业蛋糕