question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using KafkaActor Producer to push Messages

See original GitHub issue

I can see some documentation on how to use the KafkaActor Consumer, but there is very little information on how to use the KafkaActor Producer. I want to push a message that is contained as a case class into a Topic. For this purpose (Just to see how it might work), I just copied one of the tests and tried to run it:

"KafkaProducerActor" should "write a given batch to Kafka 1" in {
  case class MyDummy(s: String, i: Int)
  val topic = randomString
  val probe = TestProbe()
  val producer = system.actorOf(KafkaProducerActor.props(producerConf))
  val batch: Seq[ProducerRecord[String, MyDummy]] = Seq(
    KafkaProducerRecord(topic, MyDummy("foo", 1)),
    KafkaProducerRecord(topic, "key", MyDummy("value", 2)),
    KafkaProducerRecord(topic, MyDummy("bar", 3)))
  val message = ProducerRecords(batch, Some('response))

  probe.send(producer, message)

  probe.expectMsg('response)

  val results = consumeFromTopic(topic, 3, 10000)

  results.head shouldEqual ((None, "foo"))
  results(1) shouldEqual ((Some("key"), "value"))
  results(2) shouldEqual ((None, "bar"))
}

So as it can be seen that I want to push the MyDummy into the Kafka topic, but I get the following errors:

Error:(60, 34) No TypeTag available for MyDummy
    val message = ProducerRecords(batch, Some('response))
Error:(60, 34) not enough arguments for method apply: (implicit evidence$13: reflect.runtime.universe.TypeTag[String], implicit evidence$14: reflect.runtime.universe.TypeTag[MyDummy])cakesolutions.kafka.akka.ProducerRecords[String,MyDummy] in object ProducerRecords.
Unspecified value parameter evidence$14.
    val message = ProducerRecords(batch, Some('response))

I’m just not sure how to get past the compiler. Any help?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jkplcommented, Jan 23, 2018

The TypeTaggedTrait is there to add the type tag derived from the extending class as a value. This allows preserving the type information in the runtime that would be otherwise lost due to type erasure. This is especially nice when combined with Akka actors where each message loses its type information. The problem that TypeTaggedTrait has is that it doesn’t deal very well with inheritance: the cast operation matches on exact type rather than being aware of the type hierarchy.

Here’s some additional reading on the subject, if you’re interested: https://www.cakesolutions.net/teamblogs/ways-to-pattern-match-generic-types-in-scala

0reactions
jkplcommented, Jan 24, 2018

It’s there so that the actors that receive those records (in this case the producer actor), can pattern match on only ProducerRecords[MyKey, MyString] and not on any other ProducerRecords with different type parameters.

The pattern match for the producer actor is effectively a function from Any => Option[ProducerRecords[MyKey, MyString] where MyKey and MyString are any custom type parameters. If the input is of type TypeTaggedTrait, then we know that it has a cast method. We can use that cast method to attempt to cast the value to ProducerRecords[MyKey, MyString], which will work if only if the value is of type ProducerRecords and it has the MyKey and MyString type parameters. Otherwise it will return None, i.e. it’s not a match. See TypeTaggedExtractor for the implementation details.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Messages from and to Kafka • Akka Projection - Documentation
A typical source for Projections is messages from Kafka. Akka Projections supports integration with Kafka using Alpakka Kafka. The KafkaSourceProvider ...
Read more >
Communicating with kafka using akka actors | dive-in-scala
we need a producer to place messages on our queue. we will use KafkaProducer which is a thin, lightweight wrapper for the Kafka...
Read more >
Section 7: Projection publishing to Kafka :: Akka Guide
On this page you will learn how to: send messages to a Kafka topic from a Projection. consume messages from a Kafka topic ......
Read more >
Documentation - Apache Kafka
Kafka comes with a command line client that will take input from a file or from standard input and send it out as...
Read more >
akka/alpakka-kafka - Gitter
If you send the message to the actor and commit, you get at-most-once, ... I'm using Alpakka AMQP source and I only ack...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found