Using KafkaTemplate already answered by Deadpool there is another approach to just searlized object to byte array and send whole object.
This is not best approach as its overruled Kafka best practice to distribute and parallelize as much as possible . So in general distribute message and let producer to use pool buffer and partitions to parallelize.
But some time we might need to use specific use........
//You can use any list of object i am just using String but could be enhance to any Object after all String is nothing but object..If you are using Pojo can be convert to JSON string and pass as List of String of json.
public byte[] searlizedByteArray(List<String> listObject) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
byte[] inByteArray = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(listObject);
out.flush();
inByteArray = bos.toByteArray();
} finally {
if (bos != null)
bos.close();
}
return inByteArray;
}
De-searlize byte[] array to List of object
public List<String> desearlizedByteArray(byte[] byteArray) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
ObjectInput in = null;
List<String> returnList=null;
try {
in = new ObjectInputStream(bis);
List<String> o = (List<String>) in.readObject();
for (String string : o) {
System.out.println("==="+o);
}
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
return returnList;
}
Please note we are using VALUE_SERIALIZER_CLASS_CONFIG as ByteArraySerializer
public void publishMessage() throws Exception {
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:8080");
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName());
//You might to increase buffer memory and request size in case of large size,
properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, "");
properties.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, "");
Producer<String, byte[]> producer = new org.apache.kafka.clients.producer.KafkaProducer<String, byte[]>(
properties);
try {
List asl=new ArrayList<String>();
asl.add("test1");
asl.add("test2");
byte[] byteArray = searlizedByteArray(asl);
ProducerRecord producerRecord = new ProducerRecord<String, byte[]>("testlist", null,
byteArray);
producer.send(producerRecord).get();
} finally {
if (producer != null) {
producer.flush();
producer.close();
}
}
}
Finally in consumer ByteArrayDeserializer will work
public void consumeMessage() {
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9091");
properties.setProperty("key.deserializer", StringDeserializer.class.getName());
properties.setProperty("value.deserializer", ByteArrayDeserializer.class.getName());
properties.setProperty("group.id", "grp_consumer");
properties.put("auto.offset.reset", "earliest");
KafkaConsumer<String, byte[]> consumer = new KafkaConsumer<String, byte[]>(properties);
consumer.subscribe(Arrays.asList("testlist"));
while (true) {
ConsumerRecords<String, byte[]> records = consumer.poll(100);
for (ConsumerRecord<String, byte[]> record : records) {
}
}
}