Skip to content

Instantly share code, notes, and snippets.

@lamoboos223
Created May 7, 2022 00:22
Show Gist options
  • Save lamoboos223/c6411c34b87ad3c8dde25d8ae1261928 to your computer and use it in GitHub Desktop.
Save lamoboos223/c6411c34b87ad3c8dde25d8ae1261928 to your computer and use it in GitHub Desktop.
import com.example.restaurant.avro.schema.OrderAvro;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
@Service
@Slf4j
public class AvroProducer {
@Value("${avro.topic.name}")
private String topic;
@Autowired
private KafkaTemplate<String, OrderAvro> kafkaTemplate;
public void publish(OrderAvro orderAvro){
ListenableFuture<SendResult<String, OrderAvro>> future = kafkaTemplate.send(topic, orderAvro);
future.addCallback(new ListenableFutureCallback<SendResult<String, OrderAvro>>() {
@Override
public void onFailure(Throwable ex) {
log.warn(String.format("Failed publishing Message %s to topic %s", orderAvro, topic));
log.error(ex.getMessage());
}
@Override
public void onSuccess(SendResult<String, OrderAvro> result) {
log.info(String.format("Produced Message -> %s to topic %s", orderAvro, topic));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment