/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

package org.apache.kafka.connect.runtime;

import org.apache.kafka.common.annotation.InterfaceStability;
import org.apache.kafka.connect.runtime.rest.RestServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * This class ties together all the components of a Kafka Connect process (herder, worker,
 * storage, command interface), managing their lifecycle.
 */
@InterfaceStability.Unstable
public class Connect {
    private static final Logger log = LoggerFactory.getLogger(Connect.class);

    private final Worker worker;
    private final Herder herder;
    private final RestServer rest;
    private final CountDownLatch startLatch = new CountDownLatch(1);
    private final CountDownLatch stopLatch = new CountDownLatch(1);
    private final AtomicBoolean shutdown = new AtomicBoolean(false);
    private final ShutdownHook shutdownHook;

    public Connect(Worker worker, Herder herder, RestServer rest) {
        log.debug("Kafka Connect instance created");
        this.worker = worker;
        this.herder = herder;
        this.rest = rest;
        shutdownHook = new ShutdownHook();
    }

    public void start() {
        log.info("Kafka Connect starting");
        Runtime.getRuntime().addShutdownHook(shutdownHook);

        worker.start();
        herder.start();
        rest.start(herder);

        log.info("Kafka Connect started");

        startLatch.countDown();
    }

    public void stop() {
        boolean wasShuttingDown = shutdown.getAndSet(true);
        if (!wasShuttingDown) {
            log.info("Kafka Connect stopping");

            rest.stop();
            herder.stop();
            worker.stop();

            log.info("Kafka Connect stopped");
        }

        stopLatch.countDown();
    }

    public void awaitStop() {
        try {
            stopLatch.await();
        } catch (InterruptedException e) {
            log.error("Interrupted waiting for Kafka Connect to shutdown");
        }
    }

    private class ShutdownHook extends Thread {
        @Override
        public void run() {
            try {
                startLatch.await();
                Connect.this.stop();
            } catch (InterruptedException e) {
                log.error("Interrupted in shutdown hook while waiting for Kafka Connect startup to finish");
            }
        }
    }
}

@@ -2039,24 +2039,42 @@
 d start() %7B%0A
+        try %7B%0A    
         log.
@@ -2113,16 +2113,20 @@
         
+ 
+   
 Runtime.
@@ -2170,32 +2170,36 @@
 Hook);%0A%0A        
+    
 worker.start();%0A
@@ -2198,32 +2198,36 @@
 tart();%0A        
+   
+ 
 herder.start();%0A
@@ -2218,32 +2218,36 @@
 herder.start();%0A
+    
         rest.sta
@@ -2255,24 +2255,28 @@
 t(herder);%0A%0A
+    
         log.
@@ -2302,24 +2302,43 @@
  started%22);%0A
+        %7D finally %7B
 %0A        sta
@@ -2326,32 +2326,36 @@
 nally %7B%0A        
+    
 startLatch.count
@@ -2354,32 +2354,42 @@
 ch.countDown();%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -2396,32 +2396,50 @@
 c void stop() %7B%0A
+        try %7B%0A    
         boolean 
@@ -2490,16 +2490,20 @@
         
+    
 if (!was
@@ -2522,32 +2522,36 @@
 ) %7B%0A            
+ 
+   
 log.info(%22Kafka 
@@ -2579,24 +2579,28 @@
             
+    
 rest.stop();
@@ -2604,32 +2604,36 @@
 ();%0A            
+   
+ 
 herder.stop();%0A 
@@ -2623,32 +2623,36 @@
  herder.stop();%0A
+    
             work
@@ -2659,24 +2659,28 @@
 er.stop();%0A%0A
+    
             
@@ -2718,27 +2718,54 @@
 %22);%0A        
-%7D%0A%0A
+    %7D%0A        %7D finally %7B%0A    
         stop
@@ -2779,24 +2779,34 @@
 ountDown();%0A
+        %7D%0A
     %7D%0A%0A    p

Connect hangs on startup failure