use of org.apache.kafka.clients.producer.Callback in project logging-log4j2 by apache.
the class KafkaManager method send.
public void send(final byte[] msg) throws ExecutionException, InterruptedException, TimeoutException {
if (producer != null) {
ProducerRecord<byte[], byte[]> newRecord = new ProducerRecord<>(topic, msg);
if (syncSend) {
Future<RecordMetadata> response = producer.send(newRecord);
response.get(timeoutMillis, TimeUnit.MILLISECONDS);
} else {
producer.send(newRecord, new Callback() {
public void onCompletion(RecordMetadata metadata, Exception e) {
if (e != null) {
LOGGER.error("Unable to write to Kafka in appender [" + getName() + "]", e);
}
}
});
}
}
}
use of org.apache.kafka.clients.producer.Callback in project camel by apache.
the class KafkaProducerTest method processAsyncSendsMessageWithException.
@Test
public void processAsyncSendsMessageWithException() throws Exception {
endpoint.getConfiguration().setTopic("sometopic");
Mockito.when(exchange.getIn()).thenReturn(in);
Mockito.when(exchange.getOut()).thenReturn(out);
// setup the exception here
org.apache.kafka.clients.producer.KafkaProducer kp = producer.getKafkaProducer();
Mockito.when(kp.send(Matchers.any(ProducerRecord.class), Matchers.any(Callback.class))).thenThrow(new ApiException());
in.setHeader(KafkaConstants.PARTITION_KEY, 4);
producer.process(exchange, callback);
ArgumentCaptor<Callback> callBackCaptor = ArgumentCaptor.forClass(Callback.class);
Mockito.verify(producer.getKafkaProducer()).send(Matchers.any(ProducerRecord.class), callBackCaptor.capture());
Mockito.verify(exchange).setException(Matchers.isA(ApiException.class));
Mockito.verify(callback).done(Matchers.eq(true));
Callback kafkaCallback = callBackCaptor.getValue();
kafkaCallback.onCompletion(new RecordMetadata(null, 1, 1), null);
assertRecordMetadataExists();
}
use of org.apache.kafka.clients.producer.Callback in project cruise-control by linkedin.
the class CruiseControlMetricsReporter method sendCruiseControlMetric.
/**
* Send a CruiseControlMetric to the Kafka topic.
* @param ccm the Cruise Control metric to send.
*/
public void sendCruiseControlMetric(CruiseControlMetric ccm) {
// Use topic name as key if existing so that the same sampler will be able to collect all the information
// of a topic.
String key = ccm.metricClassId() == CruiseControlMetric.MetricClassId.TOPIC_METRIC ? ((TopicMetric) ccm).topic() : Integer.toString(ccm.brokerId());
ProducerRecord<String, CruiseControlMetric> producerRecord = new ProducerRecord<>(_cruiseControlMetricsTopic, null, ccm.time(), key, ccm);
LOG.debug("Sending Cruise Control metric {}.", ccm);
_producer.send(producerRecord, new Callback() {
@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
if (e != null) {
LOG.warn("Failed to send Cruise Control metric {}", ccm);
_numMetricSendFailure++;
}
}
});
}
use of org.apache.kafka.clients.producer.Callback in project cruise-control by linkedin.
the class KafkaSampleStore method storeSamples.
@Override
public void storeSamples(MetricSampler.Samples samples) {
final AtomicInteger metricSampleCount = new AtomicInteger(0);
for (PartitionMetricSample sample : samples.partitionMetricSamples()) {
_producer.send(new ProducerRecord<>(_partitionMetricSampleStoreTopic, null, sample.sampleTime(), null, sample.toBytes()), new Callback() {
@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
if (e == null) {
metricSampleCount.incrementAndGet();
} else {
LOG.error("Failed to produce partition metric sample for {} of timestamp {} due to exception", sample.entity().tp(), sample.sampleTime(), e);
}
}
});
}
final AtomicInteger brokerMetricSampleCount = new AtomicInteger(0);
for (BrokerMetricSample sample : samples.brokerMetricSamples()) {
_producer.send(new ProducerRecord<>(_brokerMetricSampleStoreTopic, sample.toBytes()), new Callback() {
@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
if (e == null) {
brokerMetricSampleCount.incrementAndGet();
} else {
LOG.error("Failed to produce model training sample due to exception", e);
}
}
});
}
_producer.flush();
LOG.debug("Stored {} partition metric samples and {} broker metric samples to Kafka", metricSampleCount.get(), brokerMetricSampleCount.get());
}
use of org.apache.kafka.clients.producer.Callback in project uavstack by uavorg.
the class DoTestkafkaHookProxy method kafkaAsycnSendTest.
private static void kafkaAsycnSendTest() {
Producer<String, String> producer = createProducer();
for (int i = 0; i <= 3; i++) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String testMsg = "this is producer send test msg,date:" + simpleDateFormat.format(new Date());
String key = "key_" + i;
String topic = "test";
if (i % 3 == 0) {
topic = "test1";
}
producer.send(new ProducerRecord<String, String>(topic, key, testMsg), new Callback() {
public void onCompletion(RecordMetadata metadata, Exception exception) {
if (exception != null) {
exception.printStackTrace();
System.out.println("find send exception:" + exception);
}
System.out.println("send to partition(" + metadata.partition() + ")," + "offset(" + metadata.offset() + ")");
}
});
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
}
System.out.println("send message over.");
producer.close(100, TimeUnit.MILLISECONDS);
}
Aggregations