akka | Build highly concurrent , distributed , and resilient message
kandi X-RAY | akka Summary
kandi X-RAY | akka Summary
Akka [Latest version] Status(
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of akka
akka Key Features
akka Examples and Code Snippets
public WebSocket akkaStreamsSocket() {
return WebSocket.Json.accept(
request -> {
Sink in = Sink.foreach(System.out::println);
MessageDTO messageDTO = new MessageDTO("1", "1", "Title", "Test Body");
Community Discussions
Trending Discussions on akka
QUESTION
What is the proper way to handle Futures from inside an Akka (typed) Actor?
For example, assume there is an Actor OrderActor
that receives Commands to place orders... which it does by making an http call to an external service. Since these are http calls to an external service, Future
s are involved. So, what is the right way to handle that Future
from within the Actor.
I read something about the pipeTo pattern. Is that what needs to happen here or something else?
...ANSWER
Answered 2022-Mar-28 at 02:34It's generally best to avoid doing Future
transformations (map
, flatMap
, foreach
, etc.) inside an actor. There's a distinct risk that some mutable state within the actor isn't what you expect it to be when the transformation runs. In Akka Classic, perhaps the most pernicious form of this would result in sending a reply to the wrong actor.
Akka Typed (especially in the functional API) reduces a lot of the mutable state which could cause trouble, but it's still generally a good idea to pipe the Future
as a message to the actor.
So if orderFacade.placeOrder
results in a Future[OrderResponse]
, you might add subclasses of OrderCommand
like this
QUESTION
Is it possible to handle multiple message types from a single actor.
For example, assume there is an actor called TradingStrategy
that trades stocks. It takes in pricing data pumped in from another Actor. When it decides to place a trade, it sends a message to an another Actor, call it OrderActor
, to place a trade. But the TradingStrategy
Actor is interested in knowing whether or not the order placed was successful or rejected, etc... because it may change its strategy based on the results from the place order action. In this example, it seems the TradingStrategy
needs to handle messages for pricing updates AND order updates. Is this possible with Akka typed? Are there ways to handle this situation?
Here is a code example:
IEXData
is the data message.
ANSWER
Answered 2022-Mar-27 at 13:20TradingStrategy
's protocol would have to include messages indicating order updates. A TradingStrategy
actor can register a message adapter which will translate the messages sent by the order actor into TradingStrategy
's protocol: the TradingStrategy
can present the "virtual" ActorRef
returned by the message adapter to the order actor without the order actor knowing that its a TradingStrategy
.
QUESTION
I'm testing a deployment of the Eclipse IoT Cloud2Edge package and have followed the instructions here https://www.eclipse.org/packages/packages/cloud2edge/tour/ to test. After creating the new tenant and device, and configuring the connection between Hono and Ditto, I can send telemetry to the new device via the Hono http adapter as shown here:
...ANSWER
Answered 2022-Feb-14 at 07:56What you configured is the Connection source enforcement which makes sure that a Hono device (identified via the AMQP header device_id
) may only updates the twin with the same "thing id" in Ditto.
That enforcement fails as your thingId you set in the Ditto Protocol JSON is my-tenant:org.acme:my-device-1
- the topic
's first segment is the namespace, the second segment the name - combined those 2 segments become the "thing ID", see also: Protocol topic specification.
So you probably want to send the following message instead:
QUESTION
I'm trying to execute certain suspend function multiple times, in such a way that never more than N of these are being executed at the same time.
For those acquainted with Akka and Scala Streaming libraries, something like mapAsync
.
I did my own implementation using one input channel (as in kotlin channels) and N output channels. But it seems cumbersome and not very efficient.
The code I'm currently using is somewhat like this:
...ANSWER
Answered 2022-Feb-07 at 15:51You can use the limitedParallelism
-function on a Dispatcher
(experimental in v1.6.0), and use the returned dispatcher to call your asynchronous functions. The function returns a view over the original dispatcher which limits the parallelism to a limit you provide. You can use it like this:
QUESTION
I'm calling a C# API which uses overloads and optional parameters.
Unfortunately, one of the overloads is a params object[]
and F# selects it over a more specific overload which I intend to call. How do I make F# select the overload I want?
ANSWER
Answered 2021-Dec-07 at 00:45To call the expression version with two arguments, you need:
QUESTION
As I understand Akka parallelism, to handle each incoming message Actor use one thread. And this thread contains one state. As is it so, sequential messages does't share this states.
But Actor may have an ExecutorContext for execute callbacks from Future. And this is the point, where I stop understanding parallelism clearly.
For example we have the following actor:
...ANSWER
Answered 2021-Nov-27 at 18:23Broadly, actors run on an dispatcher which selects a thread from a pool and runs that actor's Receive
for some number of messages from the mailbox. There is no guarantee in general that an actor will run on a given thread (ignoring vacuous examples like a pool with a single thread, or a dispatcher which always runs a given actor in a specific thread).
That dispatcher is also a Scala ExecutionContext
which allows arbitrary tasks to be scheduled for execution on its thread pool; such tasks include Future
callbacks.
So in your actor, what happens when a messageA
is received?
- The actor calls
createApi()
and saves it - It calls the
callA
method onapi
- It closes
api
- It arranges to forward the result of
callA
when it's available to the sender - It is now ready to process another message and may or may not actually process another message
What this actually means depends on what callA
does. If callA
schedules a task on the execution context, it will return the future as soon as the task is scheduled and the callbacks have been arranged; there is no guarantee that the task or callbacks have been executed when the future is returned. As soon as the future is returned, your actor closes api
(so this might happen at any point in the task's or callbacks' execution).
In short, depending on how api
is implemented (and you might not have control over how it's implemented) and on the implementation details, the following ordering is possible
- Thread1 (processing
messageA
) sets up tasks in the dispatcher - Thread1 closes
api
and arranges for the result to be piped - Thread2 starts executing task
- Thread1 moves on to processing some other message
- Thread2's task fails because
api
has been closed
In short, when mixing Future
s and actors, the "single-threaded illusion" in Akka can be broken: it becomes possible for arbitrarily many threads to manipulate the actor's state.
In this example, because the only shared state between Future
land and actorland is local to the processing of a single message, it's not that bad: the general rule in force here is:
- As soon as you hand mutable (e.g. closeable) state from an actor to a future (this includes, unless you can be absolutely sure what's happening, calling a method on that stateful object which returns a future), it's best for the actor to forget about the existence of that object
How then to close api
?
Well, assuming that callA
isn't doing anything funky with api
(like saving the instance in some pool of instances), after messageA
is done processing and the future is completed, nothing has access to api
. So the simplest, and likely most correct, thing to do is arrange for api
to be closed after the future has completed, along these lines
QUESTION
I connected to my websocket service using this sample code client, but currently it just connects and then shutsdown.
How can I keep this connection open and never close it?
Once I make a connection, I want it to remain open until I shutdown the application.
...ANSWER
Answered 2021-Oct-19 at 10:28The Akka docs call out your situation:
The Akka HTTP WebSocket API does not support half-closed connections which means that if either stream completes the entire connection is closed (after a “Closing Handshake” has been exchanged or a timeout of 3 seconds has passed).
In your case, outgoing
(being a Source.single
) completes as soon as it has emitted the TextMessage
. The webSocketFlow
receives the completion message and then tears down the connection.
The solution is to delay when outgoing
completes, perhaps even delaying it forever (or at least until the application is killed).
Two standard sources are potentially useful for delaying completion in the scenario where you don't want to send messages through the websocket.
Source.maybe
materializes as aPromise
which you can complete with an optional terminating message. It will not complete unless and until the promise is completed.Source.never
never completes. You could achieve this by just not completingSource.maybe
, but this is less overhead than that.
So what would it look like in code?
QUESTION
I have two actors - childActor and parentActor
...ANSWER
Answered 2021-Oct-09 at 10:06IActorRefFactory
is an interface responsible for determining a parent and in case of Akka.FSharp it's implemented by ActorSystem
and Actor<_>
as well. So in your case just use:
QUESTION
I have written an API that takes couple of minutes to create a response. This apparently results in connection resets:
...ANSWER
Answered 2021-Aug-17 at 16:19There were two mistakes I found, fixing which solved the problem.
Mistake 1
I was forgetting to pass appConf
as an argument while creating an instance of ActorSystem
, without which ActorSystem
was always getting created with default akka configs. Hence always make sure to pass appConf
while creating an instance of ActorSystem
otherwise your akka configurations will never take effect.
HelloWorld.Scala
QUESTION
I'm running the sample project from How to setup akka persistence project
: https://developer.lightbend.com/start/?group=akka&project=akka-samples-persistence-dc-java
application.conf:
...ANSWER
Answered 2021-Jul-28 at 17:42Note that you're using the web interface to connect. The YCQL api is available on the 9042
port.
Also note that akka will use the default Cassandra driver. And it's best to use the YugabyteDB fork: https://github.com/yugabyte/cassandra-java-driver
Then you can also see how to setup a cluster in multiple regions https://docs.yugabyte.com/latest/deploy/multi-dc/
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install akka
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page