@@ -8,7 +8,7 @@ import kafka.consumer.{Consumer, ConsumerConfig, Whitelist}
88import kafka .serializer .{Decoder , StringDecoder }
99import kafka .server .{KafkaConfig , KafkaServer }
1010import org .apache .kafka .clients .producer .{KafkaProducer , ProducerConfig , ProducerRecord }
11- import org .apache .kafka .common .serialization .{Deserializer , Serializer , StringSerializer }
11+ import org .apache .kafka .common .serialization .{Serializer , StringSerializer }
1212import org .apache .zookeeper .server .{ServerCnxnFactory , ZooKeeperServer }
1313import org .scalatest .Suite
1414
@@ -17,7 +17,6 @@ import scala.concurrent._
1717import scala .concurrent .duration ._
1818import scala .language .{higherKinds , postfixOps }
1919import scala .reflect .io .Directory
20- import scala .reflect .runtime .universe ._
2120import scala .util .Try
2221
2322trait EmbeddedKafka {
@@ -46,28 +45,42 @@ trait EmbeddedKafka {
4645 }
4746 }
4847
48+
49+ /**
50+ * Publishes synchronously a message of type [[String ]] to the running Kafka broker.
51+ *
52+ * @see [[EmbeddedKafka#publishToKafka ]]
53+ * @param topic the topic to which publish the message (it will be auto-created)
54+ * @param message the [[String ]] message to publish
55+ * @param config an implicit [[EmbeddedKafkaConfig ]]
56+ * @throws KafkaUnavailableException if unable to connect to Kafka
57+ */
4958 def publishStringMessageToKafka (topic : String , message : String )(implicit config : EmbeddedKafkaConfig ): Unit =
5059 publishToKafka(topic, message)(config, new StringSerializer )
5160
5261 /**
53- * Publishes asynchronously a message to the running Kafka broker.
62+ * Publishes synchronously a message to the running Kafka broker.
5463 *
5564 * @param topic the topic to which publish the message (it will be auto-created)
56- * @param message the message to publish
65+ * @param message the message of type [[ T ]] to publish
5766 * @param config an implicit [[EmbeddedKafkaConfig ]]
67+ * @param serializer an implicit [[Serializer ]] for the type [[T ]]
5868 * @throws KafkaUnavailableException if unable to connect to Kafka
5969 */
6070 @ throws(classOf [KafkaUnavailableException ])
61- def publishToKafka [T ](topic : String , message : T )(implicit config : EmbeddedKafkaConfig , serializer : Serializer [T ]): Unit = {
71+ def publishToKafka [T ](topic : String , message : T )
72+ (implicit config : EmbeddedKafkaConfig , serializer : Serializer [T ]): Unit = {
6273
6374 val kafkaProducer = new KafkaProducer (Map (
64- ProducerConfig .BOOTSTRAP_SERVERS_CONFIG -> s " localhost: ${config.kafkaPort}" ,
65- ProducerConfig .METADATA_FETCH_TIMEOUT_CONFIG -> 3000 .toString,
66- ProducerConfig .RETRY_BACKOFF_MS_CONFIG -> 1000 .toString
75+ ProducerConfig .BOOTSTRAP_SERVERS_CONFIG -> s " localhost: ${config.kafkaPort}" ,
76+ ProducerConfig .METADATA_FETCH_TIMEOUT_CONFIG -> 3000 .toString,
77+ ProducerConfig .RETRY_BACKOFF_MS_CONFIG -> 1000 .toString
6778 ), new StringSerializer , serializer)
6879
6980 val sendFuture = kafkaProducer.send(new ProducerRecord (topic, message))
70- val sendResult = Try { sendFuture.get(3 , SECONDS ) }
81+ val sendResult = Try {
82+ sendFuture.get(3 , SECONDS )
83+ }
7184
7285 kafkaProducer.close()
7386
@@ -83,7 +96,8 @@ trait EmbeddedKafka {
8396 *
8497 * @param topic the topic to consume a message from
8598 * @param config an implicit [[EmbeddedKafkaConfig ]]
86- * @return the first message consumed from the given topic
99+ * @param decoder an implicit [[Decoder ]] for the type [[T ]]
100+ * @return the first message consumed from the given topic, with a type [[T ]]
87101 * @throws TimeoutException if unable to consume a message within 3 seconds
88102 * @throws KafkaUnavailableException if unable to connect to Kafka
89103 */
@@ -100,12 +114,12 @@ trait EmbeddedKafka {
100114 Consumer .create(new ConsumerConfig (props))
101115 }.getOrElse(throw new KafkaUnavailableException )
102116
103- val filter = Whitelist (topic)
104117 val messageStreams =
105- consumer.createMessageStreamsByFilter(filter , keyDecoder = new StringDecoder , valueDecoder = decoder)
118+ consumer.createMessageStreamsByFilter(Whitelist (topic) , keyDecoder = new StringDecoder , valueDecoder = decoder)
106119
107- val messageFuture = Future { messageStreams.headOption
108- .getOrElse(throw new KafkaSpecException (" Unable to find a message stream" )).iterator().next().message()
120+ val messageFuture = Future {
121+ messageStreams.headOption
122+ .getOrElse(throw new KafkaSpecException (" Unable to find a message stream" )).iterator().next().message()
109123 }
110124
111125 try {
@@ -117,22 +131,19 @@ trait EmbeddedKafka {
117131
118132 object aKafkaProducer {
119133 def thatSerializesValuesWith [V ](serializer : Class [_ <: Serializer [V ]])(implicit config : EmbeddedKafkaConfig ) = {
120- new KafkaProducer [String , V ]( basicKafkaConfig(config) + (
134+ new KafkaProducer [String , V ](basicKafkaConfig(config) + (
121135 ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG -> classOf [StringSerializer ].getName,
122- ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG -> serializer.getName
123- ))
136+ ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG -> serializer.getName))
124137 }
125-
138+
126139 def apply [V ](implicit valueSerializer : Serializer [V ], config : EmbeddedKafkaConfig ) =
127140 new KafkaProducer [String , V ](basicKafkaConfig(config), new StringSerializer , valueSerializer)
128141
129- def basicKafkaConfig [V ](config : EmbeddedKafkaConfig ): Map [String , String ] = {
130- Map (
131- ProducerConfig .BOOTSTRAP_SERVERS_CONFIG -> s " localhost: ${config.kafkaPort}" ,
132- ProducerConfig .METADATA_FETCH_TIMEOUT_CONFIG -> 3000 .toString,
133- ProducerConfig .RETRY_BACKOFF_MS_CONFIG -> 1000 .toString
134- )
135- }
142+ def basicKafkaConfig [V ](config : EmbeddedKafkaConfig ): Map [String , String ] = Map (
143+ ProducerConfig .BOOTSTRAP_SERVERS_CONFIG -> s " localhost: ${config.kafkaPort}" ,
144+ ProducerConfig .METADATA_FETCH_TIMEOUT_CONFIG -> 3000 .toString,
145+ ProducerConfig .RETRY_BACKOFF_MS_CONFIG -> 1000 .toString
146+ )
136147 }
137148
138149 private def startZooKeeper (zooKeeperPort : Int ): ServerCnxnFactory = {
0 commit comments