akka 2.3.11 实例

原创 2015年07月09日 14:29:14
package test
import java.io.File
import scala.concurrent.duration._
import akka.actor.Actor
import akka.actor.ActorIdentity
import akka.actor.ActorRef
import akka.actor.Identify
import akka.actor.ReceiveTimeout
import akka.actor.Terminated
import akka.actor.Props
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory

object Sender {
  def main(args: Array[String]): Unit = {
    val file =new File("D:/calculator.conf");
    System.out.println(file);
    val system = ActorSystem("Sys", ConfigFactory.parseFile(file))

    val remoteHostPort = if (args.length >= 1) args(0) else "127.0.0.1:2553"
    val remotePath = s"akka.tcp://Sys@$remoteHostPort/user/rcv"
    val totalMessages = if (args.length >= 2) args(1).toInt else 500000
    val burstSize = if (args.length >= 3) args(2).toInt else 5000
    val payloadSize = if (args.length >= 4) args(3).toInt else 100

    system.actorOf(Sender.props(remotePath, totalMessages, burstSize, payloadSize), "snd")
  }

  def props(path: String, totalMessages: Int, burstSize: Int, payloadSize: Int): Props =
    Props(new Sender(path, totalMessages, burstSize, payloadSize))

  private case object Warmup
  case object Shutdown
  sealed trait Echo
  case object Start extends Echo
  case object Done extends Echo
  case class Continue(remaining: Int, startTime: Long, burstStartTime: Long, n: Int)
    extends Echo
}

class Sender(path: String, totalMessages: Int, burstSize: Int, payloadSize: Int) extends Actor {
  import Sender._

  val payload: Array[Byte] = Vector.fill(payloadSize)("a").mkString.getBytes
  var startTime = 0L
  var maxRoundTripMillis = 0L

  context.setReceiveTimeout(3.seconds)
  sendIdentifyRequest()

  def sendIdentifyRequest(): Unit =
    context.actorSelection(path) ! Identify(path)

  def receive = identifying

  def identifying: Receive = {
    case ActorIdentity(`path`, Some(actor)) =>
      context.watch(actor)
      context.become(active(actor))
      context.setReceiveTimeout(Duration.Undefined)
      self ! Warmup
    case ActorIdentity(`path`, None) => println(s"Remote actor not available: $path")
    case ReceiveTimeout              => sendIdentifyRequest()
  }

  def active(actor: ActorRef): Receive = {
    case Warmup =>
      sendBatch(actor, burstSize)
      actor ! Start

    case Start =>
      println(s"Starting benchmark of $totalMessages messages with burst size $burstSize and payload size $payloadSize")
      startTime = System.nanoTime
      val remaining = sendBatch(actor, totalMessages)
      if (remaining == 0)
        actor ! Done
      else
        actor ! Continue(remaining, startTime, startTime, burstSize)

    case c @ Continue(remaining, t0, t1, n) =>
      val now = System.nanoTime()
      val duration = (now - t0).nanos.toMillis
      val roundTripMillis = (now - t1).nanos.toMillis
      maxRoundTripMillis = math.max(maxRoundTripMillis, roundTripMillis)
      if (duration >= 500) {
        val throughtput = (n * 1000.0 / duration).toInt
        println(s"It took $duration ms to deliver $n messages, throughtput $throughtput msg/s, " +
          s"latest round-trip $roundTripMillis ms, remaining $remaining of $totalMessages")
      }

      val nextRemaining = sendBatch(actor, remaining)
      if (nextRemaining == 0)
        actor ! Done
      else if (duration >= 500)
        actor ! Continue(nextRemaining, now, now, burstSize)
      else
        actor ! c.copy(remaining = nextRemaining, burstStartTime = now, n = n + burstSize)

    case Done =>
      val took = (System.nanoTime - startTime).nanos.toMillis
      val throughtput = (totalMessages * 1000.0 / took).toInt
      println(s"== It took $took ms to deliver $totalMessages messages, throughtput $throughtput msg/s, " +
        s"max round-trip $maxRoundTripMillis ms, burst size $burstSize, " +
        s"payload size $payloadSize")
      actor ! Shutdown

    case Terminated(`actor`) =>
      println("Receiver terminated")
  }

  /**
   * @return remaining messages after sending the batch
   */
  def sendBatch(actor: ActorRef, remaining: Int): Int = {
    val batchSize = math.min(remaining, burstSize)
    (1 to batchSize) foreach { x => actor ! payload }
    remaining - batchSize
  }
}
package test
import java.io.File
import akka.actor.Actor
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import akka.actor.Props

object Receiver {
  def main(args: Array[String]): Unit = {
    val file =new File("D:/remotelookup.conf");
    System.out.println(file);
    val system = ActorSystem("Sys", ConfigFactory.parseFile(file))
    system.actorOf(Props[Receiver], "rcv")
  }
}

class Receiver extends Actor {
  import Sender._

  def receive = {
    case m: Echo  => sender() ! m
    case _        =>
  }
}

calculator.conf

include "common"

akka {
  # LISTEN on tcp port 2552
  remote.netty.tcp.port = 2552
}

common.conf

akka {

  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }

  remote {
    netty.tcp {
      hostname = "127.0.0.1"
    }
  }

}

remotelookup.conf

include "common"

akka {
  remote.netty.tcp.port = 2553
}

全部从官网同一个akka版本里的
akka-actor_2.10-2.3.11.jar
akka-remote_2.10-2.3.11.jar
config-1.2.1.jar
netty-3.8.0.Final.jar
protobuf-java-2.5.0.jar

版权声明:本文为博主原创文章,未经博主允许不得转载。

akka实例参考

  • 2015年12月23日 22:04
  • 11KB
  • 下载

Akka和ProtoBuf的简单实例

Akka是什么? 可扩展的实时事务处理 我们相信编写出正确的具有容错性和可扩展性的并发程序太困难了。这多数是因为我们使用了错误的工具和错误的抽象级别。Akka就是为了改变这种状况而...

akka remote实例

Akka的设计目标就是为分布式准备的,因此 所有Actor之间的交互都是通过消息,且所有动作都是异步的 。这种做法就是为了确保Akka的所有功能无论是在单独的JVM,还是包含了成百上千机器的Clust...
  • weipanp
  • weipanp
  • 2015年08月04日 16:23
  • 802

Android基础入门教程——2.3.11 Date & Time组件(上)

Android基础入门教程——2.3.11 Date & Time组件(上) 本节引言: 1.TextClock(文本时钟) 2.AnalogClock(模拟时钟) 3.Chronometer(计时器...

freemarker中实现自定义标签(2.3.11版本以后的方式)

FreeMarker 2.3.11版本以后提供了新的自定义标签接口TemplateDirectiveModel 以替代TemplateTransformModel, 下面是一个转换自定义标签的内...
  • snihcel
  • snihcel
  • 2014年05月21日 11:22
  • 623

时间插件-bootstrap-datetimepicker2.3.11

时间插件之结束时间不能小于开始时间changeDate: function (starttime,stoptime) { //判断时间的大小弹窗提示用户 ...

openvpn-install-2.3.11-I601-x86_64.exe

  • 2017年11月30日 16:02
  • 1.75MB
  • 下载

cxf-2.3.11源码

  • 2015年01月07日 17:50
  • 11.88MB
  • 下载

【Akka】Akka入门编程实例

引言 这篇文章主要是第一次学习Akka编程,先试试水,探探坑,对Akka和SBT的使用有一个直观的了解,以几个简单的akka编程实例来说明akka的使用。希望在日后的学习和编程中,能有更多自己的体会...
  • ichsonx
  • ichsonx
  • 2016年08月04日 14:36
  • 228

actionmailer-2.3.11

  • 2014年05月20日 09:09
  • 268KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:akka 2.3.11 实例
举报原因:
原因补充:

(最多只允许输入30个字)