Example usage for java.lang Thread Thread

List of usage examples for java.lang Thread Thread

Introduction

In this page you can find the example usage for java.lang Thread Thread.

Prototype

public Thread() 

Source Link

Document

Allocates a new Thread object.

Usage

From source file:com.alibaba.otter.manager.deployer.OtterManagerLauncher.java

public static void main(String[] args) throws Throwable {
    try {//from  w  ww  .ja va2  s .  c o m
        String conf = System.getProperty("otter.conf", "classpath:otter.properties");
        Properties properties = new Properties();
        if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
            conf = StringUtils.substringAfter(conf, CLASSPATH_URL_PREFIX);
            properties.load(OtterManagerLauncher.class.getClassLoader().getResourceAsStream(conf));
        } else {
            properties.load(new FileInputStream(conf));
        }

        // ??system?
        mergeProps(properties);

        logger.info("## start the manager server.");
        final JettyEmbedServer server = new JettyEmbedServer(
                properties.getProperty("otter.jetty", "jetty.xml"));
        server.start();
        logger.info("## the manager server is running now ......");
        Runtime.getRuntime().addShutdownHook(new Thread() {

            public void run() {
                try {
                    logger.info("## stop the manager server");
                    server.join();
                } catch (Throwable e) {
                    logger.warn("##something goes wrong when stopping manager Server:\n{}",
                            ExceptionUtils.getFullStackTrace(e));
                } finally {
                    logger.info("## manager server is down.");
                }
            }

        });
    } catch (Throwable e) {
        logger.error("## Something goes wrong when starting up the manager Server:\n{}",
                ExceptionUtils.getFullStackTrace(e));
        System.exit(0);
    }
}

From source file:com.jivesoftware.os.amza.service.AmzaGetStress.java

public static void main(String[] args) throws IOException {

    args = new String[] { "soa-integ-data11.phx1.jivehosted.com", "1185", "1", "10000" };

    final String hostName = args[0];
    final int port = Integer.parseInt(args[1]);
    final int firstDocId = Integer.parseInt(args[2]);
    final int count = Integer.parseInt(args[3]);
    final int batchSize = 100;

    String partitionName = "lorem";

    for (int i = 0; i < 1024; i++) {
        final String rname = partitionName + i;
        final org.apache.http.client.HttpClient httpClient = HttpClients.createDefault();

        Thread t = new Thread() {
            @Override// www  .j av a 2 s  .  c  o m
            public void run() {
                try {
                    get(httpClient, hostName, port, rname, 0, count, batchSize);
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        };
        t.start();
    }
}

From source file:com.clustercontrol.winsyslog.HinemosWinSyslogMain.java

public static void main(String[] args) {
    try {/*from   w  w w.jav a2 s. c  o  m*/
        log.info("receiver started.");

        // ??
        String etcDir = System.getProperty("hinemos.manager.etc.dir");
        String configFilePath = new File(etcDir, "syslog.conf").getAbsolutePath();
        WinSyslogConfig.init(configFilePath);

        // Sender?
        String targetValue = WinSyslogConfig.getProperty("syslog.send.targets");
        log.info("Sender initialise." + " (target=" + targetValue + ")");
        UdpSender.init(targetValue);

        // Receiver
        boolean tcpEnable = WinSyslogConfig.getBooleanProperty("syslog.receive.tcp");
        boolean udpEnable = WinSyslogConfig.getBooleanProperty("syslog.receive.udp");
        int port = WinSyslogConfig.getIntegerProperty("syslog.receive.port");
        log.info("Receiver starting." + " (tcpEnable=" + tcpEnable + ", udpEnable=" + udpEnable + ", port="
                + port + ")");

        receiver = new SyslogReceiver(tcpEnable, udpEnable, port);
        receiver.start();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                synchronized (shutdownLock) {
                    receiver.shutdown();

                    shutdown = true;
                    shutdownLock.notify();
                }
            }
        });

        synchronized (shutdownLock) {
            while (!shutdown) {
                try {
                    shutdownLock.wait();
                } catch (InterruptedException e) {
                    log.warn("shutdown lock interrupted.", e);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException sleepE) {
                    }
                    ;
                }
            }
        }

        System.exit(0);
    } catch (Exception e) {
        log.error("unknown error.", e);
    }
}

From source file:ezbake.local.accumulo.LocalAccumulo.java

public static void main(String args[]) throws Exception {
    Logger logger = LoggerFactory.getLogger(LocalAccumulo.class);
    final File accumuloDirectory = Files.createTempDir();

    int shutdownPort = 4445;
    MiniAccumuloConfig config = new MiniAccumuloConfig(accumuloDirectory, "strongpassword");
    config.setZooKeeperPort(12181);//from   w w w .j av a  2 s . c  om

    final MiniAccumuloCluster accumulo = new MiniAccumuloCluster(config);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                accumulo.stop();
                FileUtils.deleteDirectory(accumuloDirectory);
                System.out.println("\nShut down gracefully on " + new Date());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    accumulo.start();

    printInfo(accumulo, shutdownPort);

    if (args.length > 0) {
        logger.info("Adding the following authorizations: {}", Arrays.toString(args));
        Authorizations auths = new Authorizations(args);
        Instance inst = new ZooKeeperInstance(accumulo.getConfig().getInstanceName(), accumulo.getZooKeepers());
        Connector client = inst.getConnector("root", new PasswordToken(accumulo.getConfig().getRootPassword()));
        logger.info("Connected...");
        client.securityOperations().changeUserAuthorizations("root", auths);
        logger.info("Auths updated");
    }

    // start a socket on the shutdown port and block- anything connected to this port will activate the shutdown
    ServerSocket shutdownServer = new ServerSocket(shutdownPort);
    shutdownServer.accept();

    System.exit(0);
}

From source file:es.upv.grycap.opengateway.examples.AppDaemon.java

/**
 * Main entry point to the application.//from   www.  jav  a2 s .  com
 * @param args - arguments passed to the application
 * @throws Exception - if the application fails to start the required services
 */
public static void main(final String[] args) throws Exception {
    final AppDaemon daemon = new AppDaemon();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                daemon.stop();
            } catch (Exception e) {
                System.err.println(
                        new StringBuffer("Failed to stop application: ").append(e.getMessage()).toString());
            }
            try {
                daemon.destroy();
            } catch (Exception e) {
                System.err.println(
                        new StringBuffer("Failed to stop application: ").append(e.getMessage()).toString());
            }
        }
    });
    daemon.init(new DaemonContext() {
        @Override
        public DaemonController getController() {
            return null;
        }

        @Override
        public String[] getArguments() {
            return args;
        }
    });
    daemon.start();
}

From source file:edu.hawaii.soest.pacioos.text.TextSourceApp.java

/**
 * @param args//from w  w  w. j  a v a2s  .com
 */
public static void main(String[] args) {

    String xmlConfiguration = null;
    if (args.length != 1) {
        log.error("Please provide the path to the instrument's XML configuration file "
                + "as a single parameter.");
        System.exit(1);
    } else {
        xmlConfiguration = args[0];
    }
    try {
        textSource = TextSourceFactory.getSimpleTextSource(xmlConfiguration);

        if (textSource != null) {
            textSource.start();

        }

        // Handle ctrl-c's and other abrupt death signals to the process
        Runtime.getRuntime().addShutdownHook(new Thread() {
            // stop the streaming process
            public void run() {
                log.info("Stopping the SimpleTextSource driver due to user request");
                textSource.stop();
            }
        });

    } catch (ConfigurationException e) {
        if (log.isDebugEnabled()) {
            e.printStackTrace();
        }

        log.error("There was a problem configuring the driver.  The error message was: " + e.getMessage());
        System.exit(1);

    }
}

From source file:org.robotbrains.data.cloud.timeseries.server.ServerMain.java

public static void main(String[] args) throws Exception {
    final ServerMain main = new ServerMain(args);
    main.startup();// w ww . j  a  v  a 2  s  .  c om

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            System.out.println("Shutdown hook ran!");
            main.shutdown();
        }
    });
}

From source file:io.prometheus.client.examples.random.Main.java

public static void main(final String[] arguments) {
    Registry.defaultInitialize();
    final Server server = new Server(8181);
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    server.setHandler(context);/*from   w  w  w .j a  v a  2  s  .c om*/
    context.addServlet(new ServletHolder(new MetricsServlet()), "/");

    new Thread() {
        @Override
        public void run() {
            final RandomDataImpl randomData = new RandomDataImpl();

            try {
                while (true) {
                    rpcLatency.add(ImmutableMap.of("service", "foo"), randomData.nextLong(0, 200));
                    rpcLatency.add(ImmutableMap.of("service", "bar"), (float) randomData.nextGaussian(100, 20));
                    rpcLatency.add(ImmutableMap.of("service", "zed"), (float) randomData.nextExponential(100));
                    rpcCalls.increment(ImmutableMap.of("service", "foo"));
                    rpcCalls.increment(ImmutableMap.of("service", "bar"));
                    rpcCalls.increment(ImmutableMap.of("service", "zed"));
                    Thread.sleep(1000);
                }
            } catch (final InterruptedException e) {
            }
        }
    }.start();

    try {
        server.start();
        server.join();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.google.endpoints.examples.bookstore.BookstoreServer.java

public static void main(String[] args) throws Exception {
    Options options = createOptions();//from  w  w w.  ja v  a 2 s  .c om
    CommandLineParser parser = new DefaultParser();
    CommandLine line;
    try {
        line = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }

    int port = DEFAULT_PORT;

    if (line.hasOption("port")) {
        String portOption = line.getOptionValue("port");
        try {
            port = Integer.parseInt(portOption);
        } catch (java.lang.NumberFormatException e) {
            System.err.println("Invalid port number: " + portOption);
            printUsage(options);
            return;
        }
    }

    final BookstoreData data = initializeBookstoreData();
    final BookstoreServer server = new BookstoreServer();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                System.out.println("Shutting down");
                server.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    server.start(port, data);
    System.out.format("Bookstore service listening on %d\n", port);
    server.blockUntilShutdown();
}

From source file:morphy.Morphy.java

public static void main(String[] args) {
    if (args.length == 0) {
        System.out.println("Please specify path to configuration file (morphy.properties).");
        System.exit(1);//from  ww  w .j ava 2 s  .  c  om
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            getInstance().shutdown();
        }
    });

    String filePath = args[0];
    File configFile = ensureConfigFileExistsAndReadable(filePath);
    getInstance().init(configFile);
}