actor

Akka Actor “ask” and “Await” with TimeoutException

依然范特西╮ 提交于 2019-12-04 06:49:20
I'm modeling a simple P2P with Scala and Akka: class Node() extends Peer with Actor { var peers: List[ActorRef] = List() def receive = { case _register(peer: ActorRef, p: Option[Int]) => { println("registering [" + peer + "] for [" + this + "]") peers = peer :: peers } } } sealed case class _register(val peer: ActorRef, var p: Option[Int] = None) and then a simple network: class Network() extends Actor { def this(name: String) = { this() val system = ActorSystem(name) val s1 = system.actorOf(Props(new Node()), name = "s1") val s2 = system.actorOf(Props(new Node()), name = "s2") val c1 = system

Incremental processing in an akka actor

二次信任 提交于 2019-12-04 05:30:57
I have actors that need to do very long-running and computationally expensive work, but the computation itself can be done incrementally. So while the complete computation itself takes hours to complete, the intermediate results are actually extremely useful, and I'd like to be able to respond to any requests of them. This is the pseudo code of what I want to do: var intermediateResult = ... loop { while (mailbox.isEmpty && computationNotFinished) intermediateResult = computationStep(intermediateResult) receive { case GetCurrentResult => sender ! intermediateResult ...other messages... } } I

React for futures

我是研究僧i 提交于 2019-12-04 04:27:09
I am trying to use a divide-and-conquer (aka fork/join) approach for a number crunching problem. Here is the code: import scala.actors.Futures.future private def compute( input: Input ):Result = { if( pairs.size < SIZE_LIMIT ) { computeSequential() } else { val (input1,input2) = input.split val f1 = future( compute(input1) ) val f2 = future( compute(input2) ) val result1 = f1() val result2 = f2() merge(result1,result2) } } It runs (with a nice speed-up) but the the future apply method seems to block a thread and the thread pool increases tremendously. And when too many threads are created, the

Advantages of actors over futures

一笑奈何 提交于 2019-12-04 03:56:57
I currently program in Futures, and I'm rather curious about actors. I'd like to hear from an experienced voice: What are the advantages of actors over futures? When should I use one instead of other? As far as I've read, actors hold state and futures doesn't, is this the only difference? So if I have true immutability I shouldn't care about actors? Please enlighten me :-) One important difference is that actors typically have internal state, and therefore theoretically, they are not composable; see this and this blog post for having some issues elaborated. However, in practice, they usually

call methods on akka actors in scala

╄→гoц情女王★ 提交于 2019-12-04 03:23:22
I have a an actor defined as so: class nodeActor(ID: String) extends Actor which contains a method, which is used to set up the actor before it is started: def addRef(actor:ActorRef) I instantiate this actor as so: val node1 = system.actorOf(Props(new nodeActor("node1")), name="node1") which returns me an ActorRef. The compiler doesn't let me call "addRef" on an ActorRef since it's a member of the subtype. So I cast the node using: node1.asInstanceOf[nodeActor].addRef(link1) Which keeps the compiler happy. Then at runtime I get java.lang.ClassCastException: akka.actor.LocalActorRef cannot be

Dependency injection with Akka

折月煮酒 提交于 2019-12-04 03:01:36
I use Guice in my application quite a lot. Recently i start to learn akka actors and felt like refactoring my application with it. However upfront i am already wondering how all my guice will work with actors. I went on searching on google and it is kinda a bit messy. The most up to date docs that i have found on the subject are theses: http://letitcrash.com/post/55958814293/akka-dependency-injection http://eng.42go.com/tag/guice/ which do not advocate same thing. I must confess i still need to read a lot, i am at the beginning of learning akka. I Did few examples and red few things, but i don

Scala: Why are Actors lightweight?

有些话、适合烂在心里 提交于 2019-12-04 02:33:53
What makes actors so lightweight? I'm not even sure how they work. Aren't they separate threads? ChaosPandion When they say lightweight they mean that each actor is not mapped to a single thread. JVM offers shared memory threads with locks as the primary form of concurrency abstractions. But shared memory threads are quite heavyweight and incur severe performance penalties from context switching overheads. For an actor implementation based on a one-to-one mapping with JVM threads, the process payload per Scala actor will not be as lightweight that we can spawn a million instances of an actor

scala实例

拈花ヽ惹草 提交于 2019-12-04 01:33:59
scala的wordcount实例 package com.wondersgroup.myscala import scala.actors.{Actor, Future} import scala.collection.mutable.ListBuffer import scala.io.Source //首先统计每个文本中出现的频率=》汇总 case class SubmitTask(f:String) case object StopTask //统计一个文本中单词出现的次数 class ActorTest3 extends Actor{ override def act() :Unit = { while (true) { receive{ case SubmitTask(f) => { //把文件的一行内容作为一个元素存入list val lines = Source.fromFile(f).getLines().toList //文件中的每一个单词作为一个元素存入list val words = lines.flatMap(_.split(" ")) print("----------"+words) println("================"+words.map((_,1))) //得到一个map ,当前文本的单词,以及相应单词出现的次数 println("

using guice injection with actor throws null pointer

本小妞迷上赌 提交于 2019-12-04 01:24:30
问题 I'm getting null pointer exception on the field injection of a server which is started as an akka actor. Schedular part: private ActorRef myActor = Akka.system().actorOf( new Props(Retreiver.class)); @Override public void onStart(Application app) { log.info("Starting schedular.....!"); Akka.system() .scheduler() .schedule(Duration.create(0, TimeUnit.MILLISECONDS), Duration.create(30, TimeUnit.MINUTES), myActor, "tick", Akka.system().dispatcher()); } Retreiver class part: public class

How can I host multiple Service Fabric Actor Types inside a single service?

和自甴很熟 提交于 2019-12-04 00:36:13
问题 I've read here that is should be possible to host tightly coupled ActorTypes within the same service but I can't seem to find any documentation on exactly how to do it. I thought it might I need to create my own instance of the ActorService and pass the context into it but I don't seen to be able to find the right API's from the document. Does anyone have an example they could share ? 回答1: Sort of but not really. You can have multiple actor types in the same application . It looks like they