Al A
Al A

Reputation: 225

Akka Actor running infinite loop after initialization message

New to Akka and Actors - I need to start a few actors that will basically spend their lives reading from Kafka topics and writing to an Ignite cache. I configured my dispatcher like this:

kafka-dispatcher {
  executor  = "thread-pool-executor"
  type      = PinnedDispatcher
}

My actors are created with .withDispatcher("kafka-dispatcher"), and my assumption is that each actor will be assigned an individual thread.

These actors basically spend their lives like this:

override def receive: Receive = LoggingReceive {
  case InitWorker => {
    initialize()
    pollTopic() // This never returns
  }
}

In other words, they receive an initialization message and then call the pollTopic() method, which never returns - it runs a loop reading (which will block until there is data) and then writing the data.

My questions:

  1. Is this kosher?
  2. Is there a better, i.e. more idiomatic, way to do this? Note that the read call inside pollTopic() blocks.

Upvotes: 1

Views: 676

Answers (1)

manub
manub

Reputation: 4100

Answering to your point 2 and from the description of what you're trying to do, maybe you want to consider using Akka streams with the reactive-kafka library. Akka streams uses actors under the hood but manages all of this for you, so you can focus only on implementing reusable small components that do exactly one thing.

You will then be able to write data processing pipelines, using Kafka as a Source for your data flow. I don't know much about Ignite cache, but chances are you either will write a Sink for it or - if you're talking about a blocking API, mapAsync will be your friend.

Upvotes: 3

Related Questions