Use Scala function in Gatling scenarios.

Hello everyone.
Let’s cut to the chase.

I have Scala function that just posts some messages to Kafka topic hence it returns Unit type.
Obviously I can’t use this function in scenario since exec expects Chainbuilder type.
Desperately trying to figure out how to make it return proper type but I can’t due to lack of Scala experience.
All tries led to failure.


def sendMessage(topic: String, request: Vector[Map[String, String]]) = {
    println("sending messages...")
    val generatedMessages = request.flatMap( { request => new KeyedMessage[String, String](topic, request._1, request._2) })
    producer.send(queueMessages: _*)


val messages = Vector(Map(“foo” → “bar”), Map(“foo1” → “bar1”))
val publishMessages = kafkaProducer.sendKeyedMessages(Config.Kafka.topic, messages)


Hoping for assistance…

Hi Oleg,

this is pretty straightforward. The trick is to return session object:


exec { session =>
// run your code
kafkaProducer.sendKeyedMessages(Config.Kafka.topic, messages)

// return the original session


For more info see:


That works just fine! Thanks!