@@ -2,19 +2,19 @@ package net.manub.embeddedkafka
22
33import java .net .InetSocketAddress
44import java .util .Properties
5- import java .util .concurrent .{ TimeUnit , Executors }
5+ import java .util .concurrent .Executors
66
77import kafka .consumer .{Consumer , ConsumerConfig , Whitelist }
88import kafka .serializer .StringDecoder
99import kafka .server .{KafkaConfig , KafkaServer }
1010import org .apache .kafka .clients .producer .{KafkaProducer , ProducerConfig , ProducerRecord }
11- import org .apache .kafka .common .serialization .StringSerializer
11+ import org .apache .kafka .common .serialization .{ Serializer , StringSerializer }
1212import org .apache .zookeeper .server .{ServerCnxnFactory , ZooKeeperServer }
1313import org .scalatest .Suite
1414
1515import scala .collection .JavaConversions .mapAsJavaMap
16- import scala .concurrent .duration ._
1716import scala .concurrent ._
17+ import scala .concurrent .duration ._
1818import scala .language .postfixOps
1919import scala .reflect .io .Directory
2020import scala .util .Try
@@ -99,8 +99,8 @@ trait EmbeddedKafka {
9999 val messageStreams =
100100 consumer.createMessageStreamsByFilter(filter, keyDecoder = new StringDecoder , valueDecoder = new StringDecoder )
101101
102- val messageFuture = Future {
103- messageStreams.headOption .getOrElse(throw new KafkaSpecException (" Unable to find a message stream" )).iterator().next().message()
102+ val messageFuture = Future { messageStreams.headOption
103+ .getOrElse(throw new KafkaSpecException (" Unable to find a message stream" )).iterator().next().message()
104104 }
105105
106106 try {
@@ -110,6 +110,23 @@ trait EmbeddedKafka {
110110 }
111111 }
112112
113+ def aKafkaProducerThat (): KafkaProducerConfiguration = {
114+ new KafkaProducerConfiguration ()
115+ }
116+
117+ sealed class KafkaProducerConfiguration {
118+
119+ def serializesValuesWith [T <: Serializer [_]](serializer : Class [T ])(implicit config : EmbeddedKafkaConfig ) = {
120+ new KafkaProducer [String , T ](Map (
121+ ProducerConfig .BOOTSTRAP_SERVERS_CONFIG -> s " localhost: ${config.kafkaPort}" ,
122+ ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG -> classOf [StringSerializer ].getName,
123+ ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG -> serializer.getName,
124+ ProducerConfig .METADATA_FETCH_TIMEOUT_CONFIG -> 3000 .toString,
125+ ProducerConfig .RETRY_BACKOFF_MS_CONFIG -> 1000 .toString
126+ ))
127+ }
128+ }
129+
113130
114131 private def startZooKeeper (zooKeeperPort : Int ): ServerCnxnFactory = {
115132 val zkLogsDir = Directory .makeTemp(" zookeeper-logs" )
0 commit comments