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.

Function:


def sendMessage(topic: String, request: Vector[Map[String, String]]) = {
    println("sending messages...")
    val generatedMessages = request.flatMap(_.map { 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…
Regards,
Oleg

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
session
}

`

For more info see:
https://gatling.io/docs/2.3/general/scenario/#exec

Cheers,
Adam

That works just fine! Thanks!