List of usage examples for com.google.common.util.concurrent MoreExecutors getExitingScheduledExecutorService
@Beta @GwtIncompatible("TODO") public static ScheduledExecutorService getExitingScheduledExecutorService( ScheduledThreadPoolExecutor executor)
From source file:org.prebake.service.Main.java
public static final void main(String[] argv) { // The prebakery does not read stdin and neither should any execed process. Closeables.closeQuietly(System.in); final Logger logger = Logger.getLogger(Main.class.getName()); CommandLineArgs args = new CommandLineArgs(argv); if (!CommandLineArgs.setUpLogger(args, logger)) { System.out.println(USAGE); System.exit(0);/* w w w .ja va 2 s . c o m*/ } FileSystem fs = FileSystems.getDefault(); Config config; { MessageQueue mq = new MessageQueue(); config = new CommandLineConfig(fs, mq, args); if (mq.hasErrors()) { System.err.println(USAGE); System.err.println(); for (String msg : mq.getMessages()) { System.err.println(msg); } System.exit(-1); } } ImmutableMap<String, ?> env = CommonEnvironment.makeEnvironment(config.getClientRoot().getRoot(), getSystemPropertyMap()); ScheduledExecutorService execer = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(16)); OperatingSystem os = new RealOperatingSystem(fs, execer); final String token; { byte[] bytes = new byte[256]; new SecureRandom().nextBytes(bytes); token = new BigInteger(bytes).toString(Character.MAX_RADIX); } final Clock clock = SystemClock.instance(); final Path clientRoot = config.getClientRoot(); final LogHydra hydra = new LogHydra(clientRoot.resolve(FileNames.DIR).resolve(FileNames.LOGS), clock) { @Override protected void doInstall(OutputStream[] wrappedInheritedProcessStreams, Handler logHandler) { System.setOut(new PrintStream(wrappedInheritedProcessStreams[0], true)); System.setErr(new PrintStream(wrappedInheritedProcessStreams[1], true)); logger.addHandler(logHandler); } }; hydra.install(new FileOutputStream(FileDescriptor.out), new FileOutputStream(FileDescriptor.err)); final HighLevelLog highLevelLog = new HighLevelLog(clock); final Logs logs = new Logs(highLevelLog, logger, hydra); final Prebakery pb = new Prebakery(config, env, execer, os, logs) { @Override protected String makeToken() { return token; } @Override protected int openChannel(int portHint, final BlockingQueue<Commands> q) throws IOException { final ServerSocket ss = new ServerSocket(portHint); Thread th = new Thread(new Runnable() { public void run() { while (true) { try { boolean closeSock = true; Socket sock = ss.accept(); // TODO: move sock handling to a worker or use java.nio stuff. try { byte[] bytes = ByteStreams.toByteArray(sock.getInputStream()); sock.shutdownInput(); String commandText = new String(bytes, Charsets.UTF_8); try { q.put(Commands.fromJson(clientRoot, new JsonSource(new StringReader(commandText)), sock.getOutputStream())); // Closing sock is now the service's responsibility. closeSock = false; } catch (InterruptedException ex) { continue; } } finally { if (closeSock) { sock.close(); } } } catch (IOException ex) { logger.log(Level.WARNING, "Connection failed", ex); } } } }, Main.class.getName() + "#command_receiver"); th.setDaemon(true); th.start(); return ss.getLocalPort(); } @Override protected Environment createDbEnv(Path dir) { EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); return new Environment(new File(dir.toUri()), envConfig); } @Override protected Map<?, ?> getSysProps() { return Collections.unmodifiableMap(System.getProperties()); } @Override protected Map<String, String> getSysEnv() { return Collections.unmodifiableMap(new ProcessBuilder().environment()); } }; final Server server; if (config.getWwwPort() > 0) { server = new Server(config.getWwwPort()) { @Override public String toString() { return "[Prebake Web Server]"; } }; server.setSendServerVersion(false); server.setHandler(new AbstractHandler() { MainServlet servlet = new MainServlet(token, pb, TimeZone.getDefault()); public void handle(String tgt, Request r, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { servlet.service(req, resp); } catch (RuntimeException ex) { logger.log(Level.WARNING, "Web request failed", ex); throw ex; } } }); server.setStopAtShutdown(true); try { server.start(); } catch (Exception ex) { logger.log(Level.SEVERE, "Failed to start http server on port " + config.getWwwPort(), ex); } } else { server = null; } // If an interrupt signal is received, Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { pb.close(); try { if (server != null && !(server.isStopping() || server.isStopped())) { server.stop(); } } catch (Throwable th) { logger.log(Level.SEVERE, "Error shutting down http server", th); // Don't propagate since the process will soon be dead anyway. } } })); final boolean[] exitMutex = new boolean[1]; synchronized (exitMutex) { // Start the prebakery with a handler that will cause the main thread to // complete when it receives a shutdown command or is programatically // closed. pb.start(new Runnable() { public void run() { // When a shutdown command is received, signal the main thread so // it can complete. synchronized (exitMutex) { exitMutex[0] = true; exitMutex.notifyAll(); } } }); // The loop below gets findbugs to shut up about a wait outside a loop. while (!exitMutex[0]) { try { exitMutex.wait(); logger.log(Level.INFO, "Shut down cleanly"); } catch (InterruptedException ex) { // Just exit below if the main thread is interrupted. break; } } } System.exit(0); }
From source file:com.addthis.codec.utils.ScheduledExecutorServiceBuilder.java
public ScheduledExecutorService build() { ScheduledThreadPoolExecutor service = new ScheduledThreadPoolExecutor(coreThreads, threadFactory); if (shutdownHook) { return MoreExecutors.getExitingScheduledExecutorService(service); } else {// w w w .ja v a2 s .c o m return service; } }
From source file:backtype.storm.spout.ShellSpout.java
public void open(Map stormConf, TopologyContext context, SpoutOutputCollector collector) { _collector = collector;/* www.j a v a2 s . com*/ _context = context; workerTimeoutMills = 1000 * RT.intCast(stormConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS)); _process = new ShellProcess(_command); Number subpid = _process.launch(stormConf, context); LOG.info("Launched subprocess with pid " + subpid); heartBeatExecutorService = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); }
From source file:gobblin.metrics.RootMetricContext.java
private RootMetricContext(List<Tag<?>> tags) throws NameConflictException { super(ROOT_METRIC_CONTEXT, null, tags, true); this.innerMetricContexts = Sets.newConcurrentHashSet(); this.referenceQueue = new ReferenceQueue<>(); this.referenceQueueExecutorService = ExecutorsUtils.loggingDecorator( MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1, ExecutorsUtils .newThreadFactory(Optional.of(log), Optional.of("GobblinMetrics-ReferenceQueue"))))); this.referenceQueueExecutorService.scheduleWithFixedDelay(new CheckReferenceQueue(), 0, 2, TimeUnit.SECONDS);//from ww w . jav a2 s. c om this.reporters = Sets.newConcurrentHashSet(); this.reportingStarted = false; addShutdownHook(); }
From source file:com.hortonworks.streamline.streams.runtime.processor.MultiLangProcessorRuntime.java
@Override public void initialize(Map<String, Object> config) { command = (String[]) config.get(COMMAND); processTimeoutMills = (int) config.get(PROCESS_TIMEOUT_MILLS); Map<String, Object> processorConfig = (Map<String, Object>) config.get(PROCESS_CONFIG); ShellContext shellContext = (ShellContext) config.get(SHELL_CONTEXT); List<String> outputStreams = (List<String>) config.get(OUTPUT_STREAMS); Map<String, String> envMap = (Map<String, String>) config.get(SHELL_ENVIRONMENT); String className = (String) config.get(MULTILANG_SERIALIZER); shellProcess = new ShellProcess(command); if (className != null) shellProcess.setSerializerClassName(className); shellProcess.setEnv(envMap);/* w ww . ja v a 2s.com*/ //subprocesses must send their pid first thing Long subpid = shellProcess.launch(processorConfig, shellContext, outputStreams); LOG.info("Launched subprocess with pid " + subpid); LOG.info("Start checking heartbeat..."); setHeartbeat(); heartBeatExecutorService = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); heartBeatExecutorService.scheduleAtFixedRate(new HeartbeatTimerTask(this), 1, 1, TimeUnit.SECONDS); }
From source file:org.apache.storm.spout.ShellSpout.java
public void open(Map<String, Object> topoConf, TopologyContext context, SpoutOutputCollector collector) { _collector = collector;/*from ww w .j av a 2 s . c o m*/ _context = context; if (topoConf.containsKey(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)) { workerTimeoutMills = 1000 * ObjectReader.getInt(topoConf.get(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)); } else { workerTimeoutMills = 1000 * ObjectReader.getInt(topoConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS)); } _process = new ShellProcess(_command); if (!env.isEmpty()) { _process.setEnv(env); } Number subpid = _process.launch(topoConf, context, changeDirectory); LOG.info("Launched subprocess with pid " + subpid); heartBeatExecutorService = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); }
From source file:backtype.storm.task.ShellBolt.java
public void prepare(Map stormConf, TopologyContext context, final OutputCollector collector) { Object maxPending = stormConf.get(Config.TOPOLOGY_SHELLBOLT_MAX_PENDING); if (maxPending != null) { this._pendingWrites = new LinkedBlockingQueue(((Number) maxPending).intValue()); }/*w ww . j a va2 s . c o m*/ _rand = new Random(); _collector = collector; _context = context; workerTimeoutMills = 1000 * RT.intCast(stormConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS)); _process = new ShellProcess(_command); // subprocesses must send their pid first thing Number subpid = _process.launch(stormConf, context); LOG.info("Launched subprocess with pid " + subpid); // reader _readerThread = new Thread(new BoltReaderRunnable()); _readerThread.start(); _writerThread = new Thread(new BoltWriterRunnable()); _writerThread.start(); heartBeatExecutorService = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); heartBeatExecutorService.scheduleAtFixedRate(new BoltHeartbeatTimerTask(this), 1, 1, TimeUnit.SECONDS); LOG.info("Start checking heartbeat..."); setHeartbeat(); }
From source file:org.apache.storm.task.ShellBolt.java
public void prepare(Map<String, Object> topoConf, TopologyContext context, final OutputCollector collector) { if (ConfigUtils.isLocalMode(topoConf)) { _isLocalMode = true;/*from ww w.j ava2 s . c o m*/ } Object maxPending = topoConf.get(Config.TOPOLOGY_SHELLBOLT_MAX_PENDING); if (maxPending != null) { this._pendingWrites = new ShellBoltMessageQueue(((Number) maxPending).intValue()); } _rand = new Random(); _collector = collector; _context = context; if (topoConf.containsKey(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)) { workerTimeoutMills = 1000 * ObjectReader.getInt(topoConf.get(Config.TOPOLOGY_SUBPROCESS_TIMEOUT_SECS)); } else { workerTimeoutMills = 1000 * ObjectReader.getInt(topoConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS)); } _process = new ShellProcess(_command); if (!env.isEmpty()) { _process.setEnv(env); } //subprocesses must send their pid first thing Number subpid = _process.launch(topoConf, context, changeDirectory); LOG.info("Launched subprocess with pid " + subpid); // reader _readerThread = new Thread(new BoltReaderRunnable()); _readerThread.start(); _writerThread = new Thread(new BoltWriterRunnable()); _writerThread.start(); LOG.info("Start checking heartbeat..."); setHeartbeat(); heartBeatExecutorService = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); heartBeatExecutorService.scheduleAtFixedRate(new BoltHeartbeatTimerTask(this), 1, 1, TimeUnit.SECONDS); }
From source file:edu.umich.robot.HeadlessApplication.java
/** * <p>/* ww w . j a va 2 s . co m*/ * Start Soar, wait for timeout or Soar to stop. * * @param controller * Simulation controller initialized. * @throws InterruptedException * Thrown on thread interrupt. */ private void run(Controller controller) throws InterruptedException { final CountDownLatch doneSignal = new CountDownLatch(1); Thread shutdownHook = new Thread() { @Override public void run() { logger.warn("Shutdown detected."); shutdown.set(true); doneSignal.countDown(); } }; Runtime.getRuntime().addShutdownHook(shutdownHook); try { controller.addListener(SoarStoppedEvent.class, new RobotEventListener() { public void onEvent(RobotEvent event) { logger.info("Soar stop detected."); doneSignal.countDown(); } }); ScheduledExecutorService schexec = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1)); ScheduledFuture<?> task = null; if (seconds > 0) { task = schexec.schedule(new Runnable() { public void run() { logger.info("Time up."); doneSignal.countDown(); } }, seconds, TimeUnit.SECONDS); } controller.startSoar(cycles); doneSignal.await(); if (task != null) task.cancel(true); schexec.shutdown(); } finally { if (!shutdown.get()) Runtime.getRuntime().removeShutdownHook(shutdownHook); } }
From source file:com.addthis.hydra.job.spawn.SpawnBalancer.java
public SpawnBalancer(Spawn spawn, HostManager hostManager) { this.spawn = spawn; this.hostManager = hostManager; this.config = loadConfigFromDataStore(new SpawnBalancerConfig()); taskExecutor = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(2, new ThreadFactoryBuilder().setNameFormat("spawnBalancer-%d").build())); taskExecutor.scheduleAtFixedRate(new AggregateStatUpdaterTask(), AGGREGATE_STAT_UPDATE_INTERVAL, AGGREGATE_STAT_UPDATE_INTERVAL, TimeUnit.MILLISECONDS); this.taskSizer = new SpawnBalancerTaskSizer(spawn, hostManager); }