How to manually control the offset commit with camel-kafka?

后端 未结 3 1378
小鲜肉
小鲜肉 2021-01-16 05:59

I\'m using the camel kafka component and I\'m unclear what is happening under the hood with committing the offsets. As can be seen below, I\'m aggregating records and I thin

3条回答
  •  不思量自难忘°
    2021-01-16 06:52

    You can control manual offset commit even in multi threaded route (using aggregator for exemple) by using offset repository (Camel Documentation)

    @Override
    public void configure() throws Exception {
          // The route
          from(kafkaEndpoint())
                .routeId(ROUTE_ID)
                // Some processors...
                // Commit kafka offset
                .process(MyRoute::commitKafka)
                // Continue or not...
                .to(someEndpoint());
    }
    
    private String kafkaEndpoint() {
        return new StringBuilder("kafka:")
                .append(kafkaConfiguration.getTopicName())
                .append("?brokers=")
                .append(kafkaConfiguration.getBootstrapServers())
                .append("&groupId=")
                .append(kafkaConfiguration.getGroupId())
                .append("&clientId=")
                .append(kafkaConfiguration.getClientId())
                .append("&autoCommitEnable=")
                .append(false)
                .append("&allowManualCommit=")
                .append(true)
                .append("&autoOffsetReset=")
                .append("earliest")
                .append("&offsetRepository=")
                .append("#fileStore")
                .toString();
    
    }
    
    @Bean(name = "fileStore", initMethod = "start", destroyMethod = "stop")
    private FileStateRepository fileStore() {
        FileStateRepository fileStateRepository = 
        FileStateRepository.fileStateRepository(new File(kafkaConfiguration.getOffsetFilePath()));
        fileStateRepository.setMaxFileStoreSize(10485760); // 10MB max
    
        return fileStateRepository;
    }
    
    private static void commitKafka(Exchange exchange) {
        KafkaManualCommit manual = exchange.getIn().getHeader(KafkaConstants.MANUAL_COMMIT, KafkaManualCommit.class);
        manual.commitSync();
    }
    

提交回复
热议问题