美文网首页编程语言-Java系列
java柔性关闭模板代码

java柔性关闭模板代码

作者: jerrik | 来源:发表于2020-05-22 17:08 被阅读0次

    调用close方法后,此时是有可能存在未处理完的消息的;然后代码会运行到下一个while死循环, 如果客户端没有手动设置强制关闭, 则需要继续处理未处理后的消息; 如果设置了强制关闭, 则表示客户端丢弃未处理完的消息。

     /**
         * The main run loop for the sender thread
         */
        public void run() {
            log.debug("Starting Kafka producer I/O thread.");
    
            // main loop, runs until close is called
            while (running) {
                try {
                    run(time.milliseconds());
                } catch (Exception e) {
                    log.error("Uncaught error in kafka producer I/O thread: ", e);
                }
            }
    
            log.debug("Beginning shutdown of Kafka producer I/O thread, sending remaining records.");
    
            // okay we stopped accepting requests but there may still be
            // requests in the accumulator or waiting for acknowledgment,
            // wait until these are completed.
            while (!forceClose && (this.accumulator.hasUndrained() || this.client.inFlightRequestCount() > 0)) {
                try {
                    run(time.milliseconds());
                } catch (Exception e) {
                    log.error("Uncaught error in kafka producer I/O thread: ", e);
                }
            }
            if (forceClose) {
                // We need to fail all the incomplete batches and wake up the threads waiting on
                // the futures.
                log.debug("Aborting incomplete batches due to forced shutdown");
                this.accumulator.abortIncompleteBatches();
            }
            try {
                this.client.close();
            } catch (Exception e) {
                log.error("Failed to close network client", e);
            }
    
            log.debug("Shutdown of Kafka producer I/O thread has completed.");
        }
    

    相关文章

      网友评论

        本文标题:java柔性关闭模板代码

        本文链接:https://www.haomeiwen.com/subject/fgqcahtx.html