A small type-safe Actor class implementation

Hey All,

Here is a small type-safe Actor class.

Akka is great but working on day to day stuff I sometimes need something small that will just process messages sequentially.

Hope it helps someone.

Cheers
Simer

1 Like

Does your actor differ significantly from e.g. https://github.com/scalaz/scalaz/blob/series/7.3.x/concurrent/src/main/scala/scalaz/concurrent/Actor.scala ?

Hmm just had a quick look and they seem to differ quite a bit. My implementation borrows ideas from akka’s API.

But the end goal is the same I guess - to process messages sequentially.

I am a beginner in Scala … always need to say that before I open my big mouth … :slight_smile:
But I am trying to understand this implementation and to understand what it is supposed to do. Also what is the underlying purpose of the code?
.

For an introduction to the concept of Actors, watch a couple Akka Tutorial videos on YouTube. (Akka is the standard Actor implementation in Scala.)

You may have an easier time understanding the code once you understand what is trying to achieve.

Brian Maso

1 Like

Hi sidhartha11,

Actor is a concurrency abstraction like the Futures. An Actor allows us to run some logic in a single threaded way in a multi-threaded environment (JVM) and makes it’s easier to manage concurrency as we don’t have to worry about locking & threads synchronisation.

It’s also very easy to overuse Actors, for example implementing something like a simple in-memory counter which can simply be achieved using a volatile variable or AtomicInteger. Using Actor for this would be overkill and I’ve actually seen these implementations in production systems.

I reckon having a play with it will give you a better idea. Here is a ping pong example.

I agree with Brian. There are a lot of very helpful tutorials available on Akka that explain the concept of Actors. Let us know if there is anything specific that you need explained.

Simer

Took a quick look at Akka Actors in Scala. Appears to be a type of distributed processing model at first look. Something that might be very useful in a microservices implementation. Didn’t read enough to know how it would fit in that environment. Also maybe could be useful in a Hadoop set up. I could imagine having thousands and thousands of Actors running on thousands of machines; all sending request to a Hadoop/Hive clustered environment. These actors could be supplying various types of BIG-DATA related information to users all over the world; All done using extreme parallel processing. I guess at the core level, maybe the individual JVM node level, one could reason about the concurrency aspects of the whole thing. Once I learn more about Scala, I will definitely move to Actors …

Hi Guys, just added initial support for remote Actors using RxNetty for remoting and backpressure. The
reason to build this is to support Actor like API and process incoming messages concurrently instead of using an Actor that will process messages sequentially which would be a lot slower. It took only a few lines of code to get this working, RxNetty does the heavy lifting. There is certainly a lot of room for improvement.

If anyone is familiar with RxNetty please do suggest improvements.

val server =
  TCP.server[String, String](port = 8000, writeRequest = _.getBytes(), readRequest = new String(_), writeResponse = _.getBytes()) {
    request: String =>
      //do something with the request and return response
      println(s"Request received: $request")
      s"response for $request"
  }.get

val client = TCP.client[String](host = "localhost", port = 8000, writeRequest = _.getBytes(), readResponse = new String(_)) {
  response: String =>
    println(s"Response received: $response")
}.get

client ! "some request"

Thread.sleep(1000)
server.shutdown()

If Akka is too big a hammer for what you sometimes need to do, you could have a look at vert.x. It has bindings for Scala and a large user basis, that is code tested well by many people.