Java Code Examples for org.apache.kafka.clients.producer.RecordMetadata#partition()

The following examples show how to use org.apache.kafka.clients.producer.RecordMetadata#partition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackupSourceTask.java    From kafka-backup with Apache License 2.0 5 votes vote down vote up
@Override
public void commitRecord(SourceRecord record, RecordMetadata metadata) {
    TopicPartition topicPartition = new TopicPartition(metadata.topic(), metadata.partition());
    long sourceOffset = (Long) record.sourceOffset().get(SOURCE_OFFSET_OFFSET);
    long targetOffset = metadata.offset();
    offsetSource.syncGroupForOffset(topicPartition, sourceOffset, targetOffset);
}
 
Example 2
Source File: KafkaFuture.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public KafkaSendResult get() throws InterruptedException, ExecutionException {
	RecordMetadata recordMetadata = this.m_recordMetadata.get();
	KafkaSendResult sendResult = new KafkaSendResult(recordMetadata.topic(), recordMetadata.partition(),
	      recordMetadata.offset());
	return sendResult;
}
 
Example 3
Source File: KafkaFuture.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public KafkaSendResult get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
	RecordMetadata recordMetadata = this.m_recordMetadata.get(timeout, unit);
	KafkaSendResult sendResult = new KafkaSendResult(recordMetadata.topic(), recordMetadata.partition(),
	      recordMetadata.offset());
	return sendResult;
}
 
Example 4
Source File: ProducedKafkaRecord.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Utility factory.
 * @param recordMetadata Metadata about the produced record.
 * @param producerRecord The original record that was produced.
 * @param <K> Type of key
 * @param <V> Type of message
 * @return A ProducedKafkaRecord that represents metadata about the original record, and the results of it being published.
 */
static <K,V> ProducedKafkaRecord<K,V> newInstance(
    final RecordMetadata recordMetadata,
    final ProducerRecord<K,V> producerRecord) {
    return new ProducedKafkaRecord<>(
        recordMetadata.topic(),
        recordMetadata.partition(),
        recordMetadata.offset(),
        producerRecord.key(),
        producerRecord.value()
    );
}
 
Example 5
Source File: KafkaService.java    From cerberus-source with GNU General Public License v3.0 4 votes vote down vote up
@Override
public AnswerItem<AppService> produceEvent(String topic, String key, String eventMessage,
        String bootstrapServers,
        List<AppServiceHeader> serviceHeader) throws InterruptedException, ExecutionException {

    MessageEvent message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE_PRODUCEKAFKA);;
    AnswerItem<AppService> result = new AnswerItem<>();
    AppService serviceREST = factoryAppService.create("", AppService.TYPE_KAFKA, AppService.METHOD_KAFKAPRODUCE, "", "", "", "", "", "", "", "", "", "", "",
            "", null, "", null, null);

    Properties props = new Properties();
    serviceHeader.add(factoryAppServiceHeader.create(null, "bootstrap.servers", bootstrapServers, "Y", 0, "", "", null, "", null));
    serviceHeader.add(factoryAppServiceHeader.create(null, "enable.idempotence", "true", "Y", 0, "", "", null, "", null));
    serviceHeader.add(factoryAppServiceHeader.create(null, "key.serializer", "org.apache.kafka.common.serialization.StringSerializer", "Y", 0, "", "", null, "", null));
    serviceHeader.add(factoryAppServiceHeader.create(null, "value.serializer", "org.apache.kafka.common.serialization.StringSerializer", "Y", 0, "", "", null, "", null));

    for (AppServiceHeader object : serviceHeader) {
        if (StringUtil.parseBoolean(object.getActive())) {
            props.put(object.getKey(), object.getValue());
        }
    }

    serviceREST.setServicePath(bootstrapServers);
    serviceREST.setKafkaTopic(topic);
    serviceREST.setKafkaKey(key);
    serviceREST.setServiceRequest(eventMessage);
    serviceREST.setHeaderList(serviceHeader);

    LOG.info("Open Producer : " + getKafkaConsumerKey(topic, bootstrapServers));
    KafkaProducer<String, String> producer = new KafkaProducer<>(props);
    int partition = -1;
    long offset = -1;
    try {
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, eventMessage);
        LOG.debug("Producing Kafka message - topic : " + topic + " key : " + key + " message : " + eventMessage);
        RecordMetadata metadata = producer.send(record).get(); //Wait for a responses
        partition = metadata.partition();
        offset = metadata.offset();
        LOG.debug("Produced Kafka message - topic : " + topic + " key : " + key + " partition : " + partition + " offset : " + offset);
        message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CALLSERVICE_PRODUCEKAFKA);
    } catch (Exception ex) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE_PRODUCEKAFKA);
        message.setDescription(message.getDescription().replace("%EX%", ex.toString()));
        LOG.debug(ex, ex);
    } finally {
        if (producer != null) {
            producer.flush();
            producer.close();
            LOG.info("Closed Producer : " + getKafkaConsumerKey(topic, bootstrapServers));
        } else {
            LOG.info("Producer not opened : " + getKafkaConsumerKey(topic, bootstrapServers));
        }
    }

    serviceREST.setKafkaResponseOffset(offset);
    serviceREST.setKafkaResponsePartition(partition);

    serviceREST.setResponseHTTPBodyContentType(appServiceService.guessContentType(serviceREST, AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON));

    result.setItem(serviceREST);
    message.setDescription(message.getDescription().replace("%SERVICEMETHOD%", AppService.METHOD_KAFKAPRODUCE));
    message.setDescription(message.getDescription().replace("%TOPIC%", topic));
    message.setDescription(message.getDescription().replace("%PART%", String.valueOf(partition)));
    message.setDescription(message.getDescription().replace("%OFFSET%", String.valueOf(offset)));
    result.setResultMessage(message);
    return result;
}