akka

Akka actorFor vs passing an ActorRef

空扰寡人 提交于 2020-01-01 07:41:09
问题 I'm learning Akka and I'm trying to figure out how to get actors talking to each other (let's call them A and B ). It's not a request / response scenario, A and B are sending each other messages at any time. At the moment I've got two sibling actors that pass messages in both directions to each other. They're both created directly on the ActorSystem . I had initially passed the ActorRef of A into the constructor of B . But I can't pass the ActorRef of B to the constructor of A because it

Consume TCP stream and redirect it to another Sink (with Akka Streams)

北城余情 提交于 2020-01-01 05:29:12
问题 I try to redirect/forward a TCP stream to another Sink with Akka 2.4.3. The program should open a server socket, listen for incoming connections and then consume the tcp stream. Our sender does not expect/accept replies from us so we never send back anything - we just consume the stream. After framing the tcp stream we need to transform the bytes into something more useful and send it to the Sink. I tried the following so far but i struggle especially with the part how to not sending tcp

Akka Actor ask and Type Safety

随声附和 提交于 2019-12-31 19:38:48
问题 How can I use Akka Actor ask and maintain type safety? or avoid using ask in favour of tells? When calling ? or ask on an Akka Actor, a Future[Any] is returned and I have to do an explicit cast via future.mapTo[MyType] . I don't like losing this type safety. If I use Futures directly (with no actors) I can explicitly return Future[MyType] and maintain type safety. My specific use case involves an actor delegating it's message to two child actors and then aggregating the results from those

Akka Logging outside Actor

泪湿孤枕 提交于 2019-12-31 08:07:11
问题 I have an Akka Actor that makes a call to MyObject.foo() . MyObject is not an Actor. How do I setup Logging in it? With an Actor it's simple, because I can just mixin ActorLogging. In MyObject, I don't have access to context.system. Do I create an akka.event.Logging with AkkaSystem() and then what for the LogSource implicit? 回答1: Actually I would redirect Akka logging to slf4j and use this API directly in all unrelated classes. First add this to your configuration: akka { event-handlers = [

Akka Logging outside Actor

痴心易碎 提交于 2019-12-31 08:06:07
问题 I have an Akka Actor that makes a call to MyObject.foo() . MyObject is not an Actor. How do I setup Logging in it? With an Actor it's simple, because I can just mixin ActorLogging. In MyObject, I don't have access to context.system. Do I create an akka.event.Logging with AkkaSystem() and then what for the LogSource implicit? 回答1: Actually I would redirect Akka logging to slf4j and use this API directly in all unrelated classes. First add this to your configuration: akka { event-handlers = [

Initializing an actor before being able to handle some other messages

流过昼夜 提交于 2019-12-30 08:33:11
问题 I have an actor which creates another one: class MyActor1 extends Actor { val a2 = system actorOf Props(new MyActor(123)) } The second actor must initialize (bootstrap) itself once it created and only after that it must be able to do other job. class MyActor2(a: Int) extends Actor { //initialized (bootstrapped) itself, potentially a long operation //how? val initValue = // get from a server //handle incoming messages def receive = { case "job1" => // do some job but after it's initialized

How do i specify spray Content-Type response header?

旧时模样 提交于 2019-12-30 08:19:29
问题 I understand that spray does that for me, but I still want to override it with my header, how can I override the header in the response? My response looks like this: case HttpRequest(GET, Uri.Path("/something"), _, _, _) => sender ! HttpResponse(entity = """{ "key": "value" }""" // here i want to specify also response header i would like to explicitly set it and not get it implicitly 回答1: If you still want to use spray can, then you have two options, based on that HttpResponse is a case class

Best practices with Akka in Scala and third-party Java libraries

狂风中的少年 提交于 2019-12-30 01:57:28
问题 I need to use memcached Java API in my Scala/Akka code. This API gives you both synchronous and asynchronous methods. The asynchronous ones return java.util.concurrent.Future. There was a question here about dealing with Java Futures in Scala here How do I wrap a java.util.concurrent.Future in an Akka Future?. However in my case I have two options: Using synchronous API and wrapping blocking code in future and mark blocking: Future { blocking { cache.get(key) //synchronous blocking call } }

Akka messaging mechanisms by example

谁都会走 提交于 2019-12-28 11:49:14
问题 I have a fair amount of Apache Camel (routing/mediation/orchestation engine; lightweight ESB) experience and am racking my brain trying to understand the difference between Akka: Dispatchers ( Dispatcher , PinnedDispatcher , CallingThreadDispatcher ) Routers Pools Groups Event Buses According to the docs: Dispatchers are: ...is what makes Akka Actors “tick”, it is the engine of the machine so to speak. But that doesn't really explain what a dispatcher is or what it's relationship to an actor

scala之Akka的Actor模型(下)

余生颓废 提交于 2019-12-26 09:07:02
原文地址:http://my.oschina.net/jingxing05/blog/287462 ActorSystem(“companyname”) 相当于注册一家公司一样,负责: 通用配置 如:dispatchers, deployments, remote capabilities and addresses 创建Actor和搜索actor 通常一个应用一个Actorsystem ActorRef actOf会异步的启动一个Actor,并返回这个Actor的ActorRef,作为消息目的地,ActorRef的特征: immutable 与Actor是一对一关系 可序列化和网络传输,以实现远程透明性 启动Actor actor_system.actorOf(Props(new HelloActor("jingxing05")), name = "helloactor") 如果一个Actor有多个属性,可以通过如下方式设置其值: 发送适当的消息 放到构造函数中 重写preStart方法 Actor交互消息 消息必须是immutable的 通过 ! 给Actor发送消息 Actor的消息处理方式 def receive = { // 接受消息时隐式的传入了 发送者的 ActorRef 名为 sender case "hello" => println("hello back to