-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKafkaKotlinExample.kt
72 lines (65 loc) · 3.07 KB
/
KafkaKotlinExample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import io.gatling.javaapi.core.*
import io.gatling.javaapi.core.CoreDsl.*
import io.github.amerousful.kafka.javaapi.*
import io.github.amerousful.kafka.javaapi.KafkaDsl.*
import io.github.amerousful.kafka.javaapi.KafkaMessageMatcher
import io.github.amerousful.kafka.protocol.SaslMechanism
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.clients.producer.ProducerRecord
import java.util.*
class KafkaKotlinExample {
private val customMatcher: KafkaMessageMatcher = object : KafkaMessageMatcher {
override fun requestMatchId(msg: ProducerRecord<String, *>): String {
return msg.key()
}
override fun responseMatchId(msg: ConsumerRecord<String, *>): String {
return msg.key()
}
}
val kafkaProtocol = kafka
.broker(KafkaBroker("kafka.us-east-1.amazonaws.com", 9096))
.brokers(
KafkaBroker("kafka.us-east-2.amazonaws.com", 9096),
KafkaBroker("kafka.us-east-3.amazonaws.com", 9096)
)
.acks("1")
.producerIdenticalSerializer("org.apache.kafka.common.serialization.StringSerializer")
.consumerIdenticalDeserializer("org.apache.kafka.common.serialization.StringDeserializer")
.addProducerProperty("retries", "3")
.addConsumerProperty("heartbeat.interval.ms", "3000")
.credentials("admin", "password", true, SaslMechanism.plain())
.replyTimeout(10)
.matchByKey()
.matchByValue()
.messageMatcher(customMatcher)
.replyConsumerName("gatling-test-consumer")
//#simple
fun checkRecordValue(record: ConsumerRecord<String?, *>): Boolean {
return record.value() == "myValue"
}
val scn: ScenarioBuilder = scenario("scenario")
.exec(
kafka("Kafka: fire and forget")
.send()
.topic("input_topic")
.payload("#{payload}")
.key("#{key}")
.header("k1", "v1")
.headers(Collections.singletonMap("key", "value"))
)
.exec(
kafka("Kafka: request with reply")
.requestReply()
.topic("input_topic")
.payload("message")
.replyTopic("output_topic")
.key("#{key}")
.check(CoreDsl.jsonPath("$.m").`is`("#{payload}_1"))
.checkIf("#{bool}")
.then(CoreDsl.jsonPath("$..foo"))
.checkIf { message: ConsumerRecord<String?, *>?, session: Session? -> true }
.then(CoreDsl.jsonPath("$").`is`("hello"))
.check(KafkaDsl.header("header1").`in`("value1"))
.check(KafkaDsl.simpleCheck { record: ConsumerRecord<String?, *> -> this.checkRecordValue(record) })
)
}