Search in sources :

Example 6 with Callback

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);
                    }
                }
            });
        }
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with Callback

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();
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) AsyncCallback(org.apache.camel.AsyncCallback) Callback(org.apache.kafka.clients.producer.Callback) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ApiException(org.apache.kafka.common.errors.ApiException) Test(org.junit.Test)

Example 8 with Callback

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++;
            }
        }
    });
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) CruiseControlMetric(com.linkedin.kafka.cruisecontrol.metricsreporter.metric.CruiseControlMetric) Callback(org.apache.kafka.clients.producer.Callback) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) InterruptException(org.apache.kafka.common.errors.InterruptException)

Example 9 with Callback

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());
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnknownVersionException(com.linkedin.kafka.cruisecontrol.metricsreporter.exception.UnknownVersionException) KafkaException(org.apache.kafka.common.KafkaException)

Example 10 with Callback

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);
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Callback (org.apache.kafka.clients.producer.Callback)81 Test (org.junit.Test)47 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)39 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)37 KafkaException (org.apache.kafka.common.KafkaException)21 Future (java.util.concurrent.Future)18 TimeoutException (org.apache.kafka.common.errors.TimeoutException)18 ExecutionException (java.util.concurrent.ExecutionException)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 MockProducer (org.apache.kafka.clients.producer.MockProducer)13 HashMap (java.util.HashMap)12 Properties (java.util.Properties)12 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)12 TopicPartition (org.apache.kafka.common.TopicPartition)12 Schema (org.apache.kafka.connect.data.Schema)12 Struct (org.apache.kafka.connect.data.Struct)12 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)11 StreamsException (org.apache.kafka.streams.errors.StreamsException)11