Package com.commander4j.service

Source Code of com.commander4j.service.Interface4j$AddShutdownHookSample

package com.commander4j.service;

import com.commander4j.thread.InterfaceThread;

public class Interface4j {
  InterfaceThread interfaceThread;

  public Interface4j()
  {
  }

  public Integer start(String[] args)
  {

    interfaceThread = new InterfaceThread(args);

    interfaceThread.start();

    AddShutdownHookSample sample = new AddShutdownHookSample();
    sample.attachShutDownHook();

    try
    {
      while (interfaceThread.isAlive())
      {
        com.commander4j.util.JWait.milliSec(100);
      }
    } catch (Exception ex)
    {
    }

    return null;
  }

  public int stop(int exitCode)
  {

    try
    {
      while (interfaceThread.isAlive())
      {
        interfaceThread.requestStop();
        com.commander4j.util.JWait.milliSec(100);
      }
    } catch (Exception ex)
    {
    }

    return exitCode;
  }

  public class AddShutdownHookSample {
    public void attachShutDownHook()
    {
      Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run()
        {
          System.out.println("Shutdown detected - requesting threads stop.");

          interfaceThread.requestStop();
          System.out.println("Waiting for threads to stop.");
          while (interfaceThread.isAlive())
          {
            com.commander4j.util.JWait.milliSec(100);
          }
          System.out.println("All threads have stopped.");
        }
      });
      System.out.println("Shut Down Hook Active.");
    }
  }

  public static void main(String[] args)
  {
    Interface4j int4j = new Interface4j();
    System.out.println("Starting");
    int4j.start(args);
    System.out.println("Stopped");
  }
}
TOP

Related Classes of com.commander4j.service.Interface4j$AddShutdownHookSample

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.