Example #1
0
  /** @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() */
  @Override
  public void doStart() throws Exception {
    _connectorServer.start();
    ShutdownThread.register(0, this);

    LOG.info("JMX Remote URL: {}", _connectorServer.getAddress().toString());
  }
  /* ------------------------------------------------------------ */
  @Override
  protected void doStart() throws Exception {
    if (getStopAtShutdown()) ShutdownThread.register(this);

    LOG.info("jetty-" + __version);
    HttpGenerator.setServerVersion(__version);
    MultiException mex = new MultiException();

    if (_threadPool == null) setThreadPool(new QueuedThreadPool());

    try {
      super.doStart();
    } catch (Throwable e) {
      mex.add(e);
    }

    if (_connectors != null) {
      for (int i = 0; i < _connectors.length; i++) {
        try {
          _connectors[i].start();
        } catch (Throwable e) {
          mex.add(e);
        }
      }
    }

    if (isDumpAfterStart()) dumpStdErr();

    mex.ifExceptionThrow();
  }
  /* ------------------------------------------------------------ */
  @Override
  protected void doStop() throws Exception {
    if (isDumpBeforeStop()) dumpStdErr();

    MultiException mex = new MultiException();

    if (_graceful > 0) {
      if (_connectors != null) {
        for (int i = _connectors.length; i-- > 0; ) {
          LOG.info("Graceful shutdown {}", _connectors[i]);
          try {
            _connectors[i].close();
          } catch (Throwable e) {
            mex.add(e);
          }
        }
      }

      Handler[] contexts = getChildHandlersByClass(Graceful.class);
      for (int c = 0; c < contexts.length; c++) {
        Graceful context = (Graceful) contexts[c];
        LOG.info("Graceful shutdown {}", context);
        context.setShutdown(true);
      }
      Thread.sleep(_graceful);
    }

    if (_connectors != null) {
      for (int i = _connectors.length; i-- > 0; )
        try {
          _connectors[i].stop();
        } catch (Throwable e) {
          mex.add(e);
        }
    }

    try {
      super.doStop();
    } catch (Throwable e) {
      mex.add(e);
    }

    mex.ifExceptionThrow();

    if (getStopAtShutdown()) ShutdownThread.deregister(this);
  }
Example #4
0
 /** @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop() */
 @Override
 public void doStop() throws Exception {
   ShutdownThread.deregister(this);
   _connectorServer.stop();
   stopRegistry();
 }
    @Override
    public void run() {
      if (serverSocket == null) {
        return;
      }

      while (serverSocket != null) {
        Socket socket = null;
        try {
          socket = serverSocket.accept();

          LineNumberReader lin =
              new LineNumberReader(new InputStreamReader(socket.getInputStream()));
          String receivedKey = lin.readLine();
          if (!key.equals(receivedKey)) {
            System.err.println("Ignoring command with incorrect key");
            continue;
          }

          OutputStream out = socket.getOutputStream();

          String cmd = lin.readLine();
          debug("command=%s", cmd);
          if ("stop".equalsIgnoreCase(cmd)) // historic, for backward compatibility
          {
            // Stop the lifecycles, only if they are registered with the ShutdownThread, only
            // destroying if vm is exiting
            debug("Issuing stop...");

            for (LifeCycle l : _lifeCycles) {
              try {
                if (l.isStarted() && ShutdownThread.isRegistered(l)) {
                  l.stop();
                }

                if ((l instanceof Destroyable) && exitVm) ((Destroyable) l).destroy();
              } catch (Exception e) {
                debug(e);
              }
            }

            // Stop accepting any more commands
            stopInput(socket);

            // Reply to client
            debug("Informing client that we are stopped.");
            informClient(out, "Stopped\r\n");

            // Stop the output and close the monitor socket
            stopOutput(socket);

            if (exitVm) {
              // Kill JVM
              debug("Killing JVM");
              System.exit(0);
            }
          } else if ("forcestop".equalsIgnoreCase(cmd)) {
            debug("Issuing force stop...");

            // Ensure that objects are stopped, destroyed only if vm is forcibly exiting
            stopLifeCycles(exitVm);

            // Stop accepting any more commands
            stopInput(socket);

            // Reply to client
            debug("Informing client that we are stopped.");
            informClient(out, "Stopped\r\n");

            // Stop the output and close the monitor socket
            stopOutput(socket);

            // Honour any pre-setup config to stop the jvm when this command is given
            if (exitVm) {
              // Kill JVM
              debug("Killing JVM");
              System.exit(0);
            }
          } else if ("stopexit".equalsIgnoreCase(cmd)) {
            debug("Issuing stop and exit...");
            // Make sure that objects registered with the shutdown thread will be stopped
            stopLifeCycles(true);

            // Stop accepting any more input
            stopInput(socket);

            // Reply to client
            debug("Informing client that we are stopped.");
            informClient(out, "Stopped\r\n");

            // Stop the output and close the monitor socket
            stopOutput(socket);

            debug("Killing JVM");
            System.exit(0);
          } else if ("exit".equalsIgnoreCase(cmd)) {
            debug("Killing JVM");
            System.exit(0);
          } else if ("status".equalsIgnoreCase(cmd)) {
            // Reply to client
            informClient(out, "OK\r\n");
          }
        } catch (Exception e) {
          debug(e);
          System.err.println(e.toString());
        } finally {
          close(socket);
          socket = null;
        }
      }
    }
 /* ------------------------------------------------------------ */
 public void setStopAtShutdown(boolean stop) {
   _stopAtShutdown = stop;
   if (stop) ShutdownThread.register(this);
   else ShutdownThread.deregister(this);
 }