Use Scala function in Gatling scenarios.

48 views
Skip to first unread message

Олег Бройченко

unread,
Apr 23, 2018, 2:02:39 PM4/23/18
to Gatling User Group
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

Adam Artajew

unread,
Apr 24, 2018, 3:44:53 AM4/24/18
to Gatling User Group
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:

Cheers,
Adam

Олег Бройченко

unread,
Apr 24, 2018, 5:19:10 AM4/24/18
to Gatling User Group
That works just fine! Thanks!


вторник, 24 апреля 2018 г., 10:44:53 UTC+3 пользователь Adam Artajew написал:
Reply all
Reply to author
Forward
0 new messages