I have this producer:
public class ProducerDemo {
public static void main(String[] args) {
String inputFile = "C:\\Users\\path\\to\\binary\\file";
String bootstrapServers = "127.0.0.1:9092";
try (
InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outputFile);
) {
long fileSize = new File(inputFile).length();
byte[] allBytes = new byte[(int) fileSize];
inputStream.read(allBytes);
// create producer properties
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName());
properties.setProperty(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, "com.cme.pe.kms.EncryptionInterceptor");
// create the producer
KafkaProducer<String, byte[]> producer = new KafkaProducer<String, byte[]>(properties);
// create a producer record
ProducerRecord<String, byte[]> record = new ProducerRecord<String, byte[]>("foo", allBytes);
// send asynchronously
producer.send(record);
// flush and close the producer
//producer.flush();
producer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
I also have a Kafka Intereptor
with this method:
public ProducerRecord<String, byte[]> onSend(final ProducerRecord<String, byte[]> record) {
try {
MasterKeyProvider<?> key_provider = keySet.keyProvider(record.topic());
Toppar toppar = new Toppar(record.topic(), record.partition(), 0L, 0);
inboundDecoders.get(record.topic()+record.partition()).decodeMsg(record.value(), toppar);
byte[] buf = encrypt(key_provider, record.value());
return new ProducerRecord<>(record.topic(), record.partition(), record.key(), buf);
} catch (Exception e) {
LOGGER.error("unable to encrypt message", e);
return new ProducerRecord<>(record.topic(), record.partition(), record.key(), new byte[0]);
}
}
When I run my application I get this error:
java.lang.NullPointerException at com.my.app.MyInterceptor.onSend(MyInterceptor.java:44) .... at com.my.app.ProducerDemo.main(ProducerDemo.java:50)
Line 44 is Toppar toppar = new Toppar(record.topic(), record.partition(), 0L, 0);
. I think the issue is that record.partition()
is null. I'm running Kafka as a single instance locally. I thought the partition would be 0. Am I seeing correct functionality or how do I resolve this?