18
18
import java .io .IOException ;
19
19
import java .io .PrintWriter ;
20
20
import java .io .StringWriter ;
21
+ import java .util .LinkedHashMap ;
22
+ import java .util .Map ;
21
23
import java .util .Properties ;
22
24
23
25
import org .apache .kafka .clients .producer .KafkaProducer ;
33
35
import org .apache .jmeter .samplers .SampleResult ;
34
36
import org .apache .jorphan .logging .LoggingManager ;
35
37
import org .apache .kafka .clients .producer .ProducerRecord ;
38
+ import org .apache .kafka .common .header .internals .RecordHeader ;
36
39
import org .apache .log .Logger ;
37
40
38
41
import co .signal .handlebars .CustomHandlebars ;
@@ -121,6 +124,11 @@ public class KafkaProducerSampler extends AbstractJavaSamplerClient {
121
124
*/
122
125
private static final String PARAMETER_KAFKA_PARTITION = "kafka_partition" ;
123
126
127
+ /**
128
+ * Parameter for setting the headers. It is optional.
129
+ */
130
+ private static final String PARAMETER_KAFKA_HEADERS = "kafka_headers" ;
131
+
124
132
// private Producer<Long, byte[]> producer;
125
133
126
134
private KafkaProducer <String , String > producer ;
@@ -178,6 +186,7 @@ public Arguments getDefaultParameters() {
178
186
defaultParameters .addArgument (PARAMETER_KAFKA_USE_SSL , "${PARAMETER_KAFKA_USE_SSL}" );
179
187
defaultParameters .addArgument (PARAMETER_KAFKA_COMPRESSION_TYPE , null );
180
188
defaultParameters .addArgument (PARAMETER_KAFKA_PARTITION , null );
189
+ defaultParameters .addArgument (PARAMETER_KAFKA_HEADERS , null );
181
190
return defaultParameters ;
182
191
}
183
192
@@ -197,6 +206,19 @@ public SampleResult runTest(JavaSamplerContext context) {
197
206
} catch (IOException e1 ) {
198
207
e1 .printStackTrace ();
199
208
}
209
+ Map <String , String > headers = new LinkedHashMap <String , String >();
210
+ try {
211
+ String headerString = handlebars .compileInline (context .getParameter (PARAMETER_KAFKA_HEADERS )).apply ("" );
212
+ if (!Strings .isNullOrEmpty (headerString )) {
213
+ String [] pairs = headerString .split ("&" );
214
+ for (String pair : pairs ) {
215
+ int idx = pair .indexOf ("=" );
216
+ headers .put (pair .substring (0 , idx ), pair .substring (idx + 1 ));
217
+ }
218
+ }
219
+ } catch (IOException e1 ) {
220
+ e1 .printStackTrace ();
221
+ }
200
222
sampleResultStart (result , message );
201
223
202
224
final ProducerRecord <String , String > producerRecord ;
@@ -207,6 +229,9 @@ public SampleResult runTest(JavaSamplerContext context) {
207
229
final int partitionNumber = Integer .parseInt (partitionString );
208
230
producerRecord = new ProducerRecord <String , String >(topic , partitionNumber , key , message );
209
231
}
232
+ for (Map .Entry <String , String > entry : headers .entrySet ()) {
233
+ producerRecord .headers ().add (new RecordHeader (entry .getKey (), entry .getValue ().getBytes ()));
234
+ }
210
235
211
236
try {
212
237
producer .send (producerRecord );
@@ -301,4 +326,4 @@ private String getStackTrace(Exception exception) {
301
326
exception .printStackTrace (new PrintWriter (stringWriter ));
302
327
return stringWriter .toString ();
303
328
}
304
- }
329
+ }
0 commit comments