Java Code Examples for org.apache.kafka.common.header.Headers#iterator()

The following examples show how to use org.apache.kafka.common.header.Headers#iterator() . 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: Record.java    From kafka-backup with Apache License 2.0 5 votes vote down vote up
private boolean headersEqualityByValue(Headers a, Headers b) {
    // This is an alternative implementation of ConnectHeaders::equals that use proper Header equality by value
    if (a == b) {
        return true;
    }
    // Note, similar to ConnectHeaders::equals, it requires headers to have the same order
    // (although, that is probably not what we want in most cases)
    Iterator<Header> aIter = a.iterator();
    Iterator<Header> bIter = b.iterator();
    while (aIter.hasNext() && bIter.hasNext()) {
        if (!headerEqualityByValue(aIter.next(), bIter.next()))
            return false;
    }
    return !aIter.hasNext() && !bIter.hasNext();
}
 
Example 2
Source File: KafkaEasyTransMsgConsumerImpl.java    From EasyTransaction with Apache License 2.0 5 votes vote down vote up
private void reconsumeLater(ConsumerRecord<String, byte[]> consumeRecord) throws InterruptedException, ExecutionException {

		// add all header to headList except RETRY_COUNT
		Headers headers = consumeRecord.headers();
		List<Header> headerList = new ArrayList<Header>(8);
		Iterator<Header> iterator = headers.iterator();
		Integer retryCount = -1;
		boolean hasOrignalHeader = false;
		while (iterator.hasNext()) {
			Header next = iterator.next();
			if (next.key().equals(RETRY_COUNT_KEY)) {
				retryCount = serializer.deserialize(next.value());
				continue;
			}
			
			if(next.key().equals(ORGINAL_TOPIC)){
				hasOrignalHeader = true;
			}
			headerList.add(next);
		}
		
		// add RETRY_COUNT to header
		retryCount++;
		headerList.add(new RecordHeader(RETRY_COUNT_KEY, serializer.serialization(retryCount)));
		
		if(!hasOrignalHeader){
			headerList.add(new RecordHeader(ORGINAL_TOPIC, serializer.serialization(consumeRecord.topic())));
		}

		// send message to corresponding queue according to retry times
		String retryTopic = calcRetryTopic(consumeRecord.topic(), retryCount);
		
		ProducerRecord<String, byte[]> record = new ProducerRecord<>(retryTopic,
				consumeRecord.partition() % retryQueuePartitionCount.get(retryTopic), null, consumeRecord.key(),
				consumeRecord.value(), headerList);
		Future<RecordMetadata> publishKafkaMessage = retryQueueMsgProducer.publishKafkaMessage(record);
		publishKafkaMessage.get();
	}
 
Example 3
Source File: KafkaTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
void clearTraceIdHeaders(Headers headers) {
  // Headers::remove creates and consumes an iterator each time. This does one loop instead.
  for (Iterator<Header> i = headers.iterator(); i.hasNext(); ) {
    Header next = i.next();
    if (traceIdHeaders.contains(next.key())) i.remove();
  }
}
 
Example 4
Source File: KafkaStreamsTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
void clearHeaders(Headers headers) {
  // Headers::remove creates and consumes an iterator each time. This does one loop instead.
  for (Iterator<Header> i = headers.iterator(); i.hasNext(); ) {
    Header next = i.next();
    if (propagationKeys.contains(next.key())) i.remove();
  }
}