Example usage for java.util.concurrent ScheduledExecutorService shutdownNow

List of usage examples for java.util.concurrent ScheduledExecutorService shutdownNow

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledExecutorService shutdownNow.

Prototype

List<Runnable> shutdownNow();

Source Link

Document

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

Usage

From source file:org.apache.pulsar.compaction.CompactorTool.java

public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander(arguments);
    jcommander.setProgramName("PulsarTopicCompactor");

    // parse args by JCommander
    jcommander.parse(args);/*from w  w w  .  j a  v a2  s.c  om*/
    if (arguments.help) {
        jcommander.usage();
        System.exit(-1);
    }

    // init broker config
    ServiceConfiguration brokerConfig;
    if (isBlank(arguments.brokerConfigFile)) {
        jcommander.usage();
        throw new IllegalArgumentException("Need to specify a configuration file for broker");
    } else {
        brokerConfig = PulsarConfigurationLoader.create(arguments.brokerConfigFile, ServiceConfiguration.class);
    }

    ClientBuilder clientBuilder = PulsarClient.builder();

    if (isNotBlank(brokerConfig.getBrokerClientAuthenticationPlugin())) {
        clientBuilder.authentication(brokerConfig.getBrokerClientAuthenticationPlugin(),
                brokerConfig.getBrokerClientAuthenticationParameters());
    }

    if (brokerConfig.getBrokerServicePortTls().isPresent()) {
        clientBuilder.serviceUrl(PulsarService.brokerUrlTls(brokerConfig))
                .allowTlsInsecureConnection(brokerConfig.isTlsAllowInsecureConnection())
                .tlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());

    } else {
        clientBuilder.serviceUrl(PulsarService.brokerUrl(brokerConfig));
    }

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());

    OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
    ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);

    ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(),
            ZooKeeperClientFactory.SessionType.ReadWrite, (int) brokerConfig.getZooKeeperSessionTimeoutMillis())
            .get();
    BookKeeperClientFactory bkClientFactory = new BookKeeperClientFactoryImpl();
    BookKeeper bk = bkClientFactory.create(brokerConfig, zk);
    try (PulsarClient pulsar = clientBuilder.build()) {
        Compactor compactor = new TwoPhaseCompactor(brokerConfig, pulsar, bk, scheduler);
        long ledgerId = compactor.compact(arguments.topic).get();
        log.info("Compaction of topic {} complete. Compacted to ledger {}", arguments.topic, ledgerId);
    } finally {
        bk.close();
        bkClientFactory.close();
        zk.close();
        scheduler.shutdownNow();
        executor.shutdown();
    }
}

From source file:com.mapr.synth.Synth.java

public static void main(String[] args)
        throws IOException, CmdLineException, InterruptedException, ExecutionException {
    final Options opts = new Options();
    CmdLineParser parser = new CmdLineParser(opts);
    try {/*from   ww w . ja v  a  2  s.  c  o  m*/
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println("Usage: " + "[ -count <number>G|M|K ] " + "-schema schema-file "
                + "[-quote DOUBLE_QUOTE|BACK_SLASH|OPTIMISTIC] " + "[-format JSON|TSV|CSV|XML ] "
                + "[-threads n] " + "[-output output-directory-name] ");
        throw e;
    }
    Preconditions.checkArgument(opts.threads > 0 && opts.threads <= 2000,
            "Must have at least one thread and no more than 2000");

    if (opts.threads > 1) {
        Preconditions.checkArgument(!"-".equals(opts.output),
                "If more than on thread is used, you have to use -output to set the output directory");
    }

    File outputDir = new File(opts.output);
    if (!"-".equals(opts.output)) {
        if (!outputDir.exists()) {
            Preconditions.checkState(outputDir.mkdirs(),
                    String.format("Couldn't create output directory %s", opts.output));
        }
        Preconditions.checkArgument(outputDir.exists() && outputDir.isDirectory(),
                String.format("Couldn't create directory %s", opts.output));
    }

    if (opts.schema == null) {
        throw new IllegalArgumentException("Must specify schema file using [-schema filename] option");
    }
    final SchemaSampler sampler = new SchemaSampler(opts.schema);
    final AtomicLong rowCount = new AtomicLong();

    final List<ReportingWorker> tasks = Lists.newArrayList();
    int limit = (opts.count + opts.threads - 1) / opts.threads;
    int remaining = opts.count;
    for (int i = 0; i < opts.threads; i++) {

        final int count = Math.min(limit, remaining);
        remaining -= count;

        tasks.add(new ReportingWorker(opts, sampler, rowCount, count, i));
    }

    final double t0 = System.nanoTime() * 1e-9;
    ExecutorService pool = Executors.newFixedThreadPool(opts.threads);
    ScheduledExecutorService blinker = Executors.newScheduledThreadPool(1);
    final AtomicBoolean finalRun = new AtomicBoolean(false);

    final PrintStream sideLog = new PrintStream(new FileOutputStream("side-log"));
    Runnable blink = new Runnable() {
        public double oldT;
        private long oldN;

        @Override
        public void run() {
            double t = System.nanoTime() * 1e-9;
            long n = rowCount.get();
            System.err.printf("%s\t%d\t%.1f\t%d\t%.1f\t%.3f\n", finalRun.get() ? "F" : "R", opts.threads,
                    t - t0, n, n / (t - t0), (n - oldN) / (t - oldT));
            for (ReportingWorker task : tasks) {
                ReportingWorker.ThreadReport r = task.report();
                sideLog.printf("\t%d\t%.2f\t%.2f\t%.2f\t%.1f\t%.1f\n", r.fileNumber, r.threadTime, r.userTime,
                        r.wallTime, r.rows / r.threadTime, r.rows / r.wallTime);
            }
            oldN = n;
            oldT = t;
        }
    };
    if (!"-".equals(opts.output)) {
        blinker.scheduleAtFixedRate(blink, 0, 10, TimeUnit.SECONDS);
    }
    List<Future<Integer>> results = pool.invokeAll(tasks);

    int total = 0;
    for (Future<Integer> result : results) {
        total += result.get();
    }
    Preconditions.checkState(total == opts.count, String
            .format("Expected to generate %d lines of output, but actually generated %d", opts.count, total));
    pool.shutdownNow();
    blinker.shutdownNow();
    finalRun.set(true);
    sideLog.close();
    blink.run();
}

From source file:org.wso2.carbon.repository.core.handlers.builtin.OperationStatisticsHandler.java

private static synchronized void initializeStatisticsLogging() {
    if (executor != null) {
        return;//from  w  ww.  j ava  2 s  . c  o  m
    }
    records = new HashMap<Method, Long>();
    executor = Executors.newCachedThreadPool();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            executor.shutdownNow();
        }
    });

    for (Method method : new Method[] { Method.GET, Method.PUT, Method.IMPORT, Method.MOVE, Method.COPY,
            Method.RENAME, Method.DELETE, Method.ADD_ASSOCIATION, Method.REMOVE_ASSOCIATION,
            Method.GET_ASSOCIATIONS, Method.GET_ALL_ASSOCIATIONS, Method.EXECUTE_QUERY, Method.RESOURCE_EXISTS,
            Method.DUMP, Method.RESTORE }) {
        records.put(method, 0l);
    }

    final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            scheduler.shutdownNow();
        }
    });

    Runnable runnable = new Runnable() {
        public void run() {
            if (records == null) {
                log.error("Unable to store operation statistics.");
            } else {
                synchronized (this) {
                    statsLog.debug("Total Number of get calls                : " + records.get(Method.GET));
                    statsLog.debug("Total Number of put calls                : " + records.get(Method.PUT));
                    statsLog.debug("Total Number of import calls             : " + records.get(Method.IMPORT));
                    statsLog.debug("Total Number of move calls               : " + records.get(Method.MOVE));
                    statsLog.debug("Total Number of copy calls               : " + records.get(Method.COPY));
                    statsLog.debug("Total Number of rename calls             : " + records.get(Method.RENAME));
                    statsLog.debug("Total Number of delete calls             : " + records.get(Method.DELETE));
                    statsLog.debug("Total Number of addAssociation calls     : "
                            + records.get(Method.ADD_ASSOCIATION));
                    statsLog.debug("Total Number of removeAssociation calls  : "
                            + records.get(Method.REMOVE_ASSOCIATION));
                    statsLog.debug("Total Number of getAssociations calls    : "
                            + records.get(Method.GET_ASSOCIATIONS));
                    statsLog.debug("Total Number of getAllAssociations calls : "
                            + records.get(Method.GET_ALL_ASSOCIATIONS));
                    statsLog.debug(
                            "Total Number of executeQuery calls       : " + records.get(Method.EXECUTE_QUERY));
                    statsLog.debug("Total Number of resourceExists calls     : "
                            + records.get(Method.RESOURCE_EXISTS));
                    statsLog.debug("Total Number of dump calls               : " + records.get(Method.DUMP));
                    statsLog.debug("Total Number of restore calls            : " + records.get(Method.RESTORE));
                }
            }
        }
    };

    scheduler.scheduleAtFixedRate(runnable, 60, 60, TimeUnit.SECONDS);
}

From source file:org.wso2.carbon.registry.core.jdbc.handlers.builtin.OperationStatisticsHandler.java

private static synchronized void initializeStatisticsLogging() {
    if (executor != null) {
        return;//  ww w .jav  a 2  s  .  c o m
    }
    records = new HashMap<String, Long>();
    executor = Executors.newCachedThreadPool();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            executor.shutdownNow();
        }
    });
    for (String s : new String[] { Filter.GET, Filter.PUT, Filter.IMPORT, Filter.MOVE, Filter.COPY,
            Filter.RENAME, Filter.DELETE, Filter.ADD_ASSOCIATION, Filter.REMOVE_ASSOCIATION,
            Filter.GET_ASSOCIATIONS, Filter.GET_ALL_ASSOCIATIONS, Filter.EXECUTE_QUERY, Filter.RESOURCE_EXISTS,
            Filter.DUMP, Filter.RESTORE }) {
        records.put(s, 0l);
    }
    final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            scheduler.shutdownNow();
        }
    });
    Runnable runnable = new Runnable() {
        public void run() {
            if (records == null) {
                log.error("Unable to store operation statistics.");
            } else {
                synchronized (this) {
                    statsLog.debug("Total Number of get calls                : " + records.get(Filter.GET));
                    statsLog.debug("Total Number of put calls                : " + records.get(Filter.PUT));
                    statsLog.debug("Total Number of import calls             : " + records.get(Filter.IMPORT));
                    statsLog.debug("Total Number of move calls               : " + records.get(Filter.MOVE));
                    statsLog.debug("Total Number of copy calls               : " + records.get(Filter.COPY));
                    statsLog.debug("Total Number of rename calls             : " + records.get(Filter.RENAME));
                    statsLog.debug("Total Number of delete calls             : " + records.get(Filter.DELETE));
                    statsLog.debug("Total Number of addAssociation calls     : "
                            + records.get(Filter.ADD_ASSOCIATION));
                    statsLog.debug("Total Number of removeAssociation calls  : "
                            + records.get(Filter.REMOVE_ASSOCIATION));
                    statsLog.debug("Total Number of getAssociations calls    : "
                            + records.get(Filter.GET_ASSOCIATIONS));
                    statsLog.debug("Total Number of getAllAssociations calls : "
                            + records.get(Filter.GET_ALL_ASSOCIATIONS));
                    statsLog.debug(
                            "Total Number of executeQuery calls       : " + records.get(Filter.EXECUTE_QUERY));
                    statsLog.debug("Total Number of resourceExists calls     : "
                            + records.get(Filter.RESOURCE_EXISTS));
                    statsLog.debug("Total Number of dump calls               : " + records.get(Filter.DUMP));
                    statsLog.debug("Total Number of restore calls            : " + records.get(Filter.RESTORE));
                }
            }
        }
    };
    scheduler.scheduleAtFixedRate(runnable, 60, 60, TimeUnit.SECONDS);
}

From source file:com.opengamma.component.tool.ToolContextUtils.java

private static ToolContext createToolContextByHttp(String configResourceLocation,
        Class<? extends ToolContext> toolContextClazz, List<String> classifierChain) {
    configResourceLocation = StringUtils.stripEnd(configResourceLocation, "/");
    if (configResourceLocation.endsWith("/jax") == false) {
        configResourceLocation += "/jax";
    }//from   w w  w .  java  2 s.c  om

    // Get the remote component server using the supplied URI
    RemoteComponentServer remoteComponentServer = new RemoteComponentServer(URI.create(configResourceLocation));
    ComponentServer componentServer = remoteComponentServer.getComponentServer();

    // Attempt to build a tool context of the specified type
    ToolContext toolContext;
    try {
        toolContext = toolContextClazz.newInstance();
    } catch (Throwable t) {
        return null;
    }

    // Populate the tool context from the remote component server
    for (MetaProperty<?> metaProperty : toolContext.metaBean().metaPropertyIterable()) {
        if (!metaProperty.name().equals("contextManager")) {
            try {
                ComponentInfo componentInfo = getComponentInfo(componentServer, classifierChain,
                        metaProperty.propertyType());
                if (componentInfo == null) {
                    s_logger.warn("Unable to populate tool context '" + metaProperty.name()
                            + "', no appropriate component found on the server");
                    continue;
                }
                if (ViewProcessor.class.equals(componentInfo.getType())) {
                    final JmsConnector jmsConnector = createJmsConnector(componentInfo);
                    final ScheduledExecutorService scheduler = Executors
                            .newSingleThreadScheduledExecutor(new NamedThreadFactory("rvp"));
                    ViewProcessor vp = new RemoteViewProcessor(componentInfo.getUri(), jmsConnector, scheduler);
                    toolContext.setViewProcessor(vp);
                    toolContext.setContextManager(new Closeable() {
                        @Override
                        public void close() throws IOException {
                            scheduler.shutdownNow();
                            jmsConnector.close();
                        }
                    });
                } else {
                    String clazzName = componentInfo.getAttribute(ComponentInfoAttributes.REMOTE_CLIENT_JAVA);
                    if (clazzName == null) {
                        s_logger.warn("Unable to populate tool context '" + metaProperty.name()
                                + "', no remote access class found");
                        continue;
                    }
                    Class<?> clazz = Class.forName(clazzName);
                    metaProperty.set(toolContext,
                            clazz.getConstructor(URI.class).newInstance(componentInfo.getUri()));
                    s_logger.info("Populated tool context '" + metaProperty.name() + "' with "
                            + metaProperty.get(toolContext));
                }
            } catch (Throwable ex) {
                s_logger.warn(
                        "Unable to populate tool context '" + metaProperty.name() + "': " + ex.getMessage());
            }
        }
    }
    return toolContext;
}

From source file:it.crs4.pydoop.mapreduce.pipes.TaskLog.java

public static synchronized void syncLogsShutdown(ScheduledExecutorService scheduler) {
    // flush standard streams
    ////from   ww  w  .j a  v  a 2  s .  c om
    System.out.flush();
    System.err.flush();

    if (scheduler != null) {
        scheduler.shutdownNow();
    }

    // flush & close all appenders
    LogManager.shutdown();
}

From source file:com.netflix.curator.framework.imps.TestTempFramework.java

@Test
public void testInactivity() throws Exception {
    final CuratorTempFrameworkImpl client = (CuratorTempFrameworkImpl) CuratorFrameworkFactory.builder()
            .connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1))
            .buildTemp(1, TimeUnit.SECONDS);
    try {//from ww  w.j a va  2 s  .  com
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        Runnable command = new Runnable() {
            @Override
            public void run() {
                client.updateLastAccess();
            }
        };
        service.scheduleAtFixedRate(command, 10, 10, TimeUnit.MILLISECONDS);
        client.inTransaction().create().forPath("/foo", "data".getBytes()).and().commit();
        service.shutdownNow();
        Thread.sleep(2000);

        Assert.assertNull(client.getCleanup());
        Assert.assertNull(client.getClient());
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.dns.ZoneManager.java

protected static void initZoneCache(final TrafficRouter tr) {
    synchronized (ZoneManager.class) {
        final CacheRegister cacheRegister = tr.getCacheRegister();
        final JSONObject config = cacheRegister.getConfig();

        int poolSize = 1;
        final double scale = config.optDouble("zonemanager.threadpool.scale", 0.75);
        final int cores = Runtime.getRuntime().availableProcessors();

        if (cores > 2) {
            final Double s = Math.floor((double) cores * scale);

            if (s.intValue() > 1) {
                poolSize = s.intValue();
            }/*from   w  ww  . j av  a  2 s .  c  o  m*/
        }

        final ExecutorService initExecutor = Executors.newFixedThreadPool(poolSize);

        final ExecutorService ze = Executors.newFixedThreadPool(poolSize);
        final ScheduledExecutorService me = Executors.newScheduledThreadPool(2); // 2 threads, one for static, one for dynamic, threads to refresh zones
        final int maintenanceInterval = config.optInt("zonemanager.cache.maintenance.interval", 300); // default 5 minutes
        final String dspec = "expireAfterAccess="
                + config.optString("zonemanager.dynamic.response.expiration", "300s"); // default to 5 minutes

        final LoadingCache<ZoneKey, Zone> dzc = createZoneCache(ZoneCacheType.DYNAMIC,
                CacheBuilderSpec.parse(dspec));
        final LoadingCache<ZoneKey, Zone> zc = createZoneCache(ZoneCacheType.STATIC);

        initZoneDirectory();

        try {
            LOGGER.info("Generating zone data");
            generateZones(tr, zc, dzc, initExecutor);
            initExecutor.shutdown();
            initExecutor.awaitTermination(5, TimeUnit.MINUTES);
            LOGGER.info("Zone generation complete");
        } catch (final InterruptedException ex) {
            LOGGER.warn("Initialization of zone data exceeded time limit of 5 minutes; continuing", ex);
        } catch (IOException ex) {
            LOGGER.fatal("Caught fatal exception while generating zone data!", ex);
        }

        me.scheduleWithFixedDelay(getMaintenanceRunnable(dzc, ZoneCacheType.DYNAMIC, maintenanceInterval), 0,
                maintenanceInterval, TimeUnit.SECONDS);
        me.scheduleWithFixedDelay(getMaintenanceRunnable(zc, ZoneCacheType.STATIC, maintenanceInterval), 0,
                maintenanceInterval, TimeUnit.SECONDS);

        final ExecutorService tze = ZoneManager.zoneExecutor;
        final ScheduledExecutorService tme = ZoneManager.zoneMaintenanceExecutor;
        final LoadingCache<ZoneKey, Zone> tzc = ZoneManager.zoneCache;
        final LoadingCache<ZoneKey, Zone> tdzc = ZoneManager.dynamicZoneCache;

        ZoneManager.zoneExecutor = ze;
        ZoneManager.zoneMaintenanceExecutor = me;
        ZoneManager.dynamicZoneCache = dzc;
        ZoneManager.zoneCache = zc;

        if (tze != null) {
            tze.shutdownNow();
        }

        if (tme != null) {
            tme.shutdownNow();
        }

        if (tzc != null) {
            tzc.invalidateAll();
        }

        if (tdzc != null) {
            tdzc.invalidateAll();
        }
    }
}

From source file:com.ntsync.android.sync.activities.PaymentVerificationService.java

private static void runVerifier(final Context context, final ScheduledExecutorService sched) {
    SystemHelper.initSystem(context);//from  w  w w. j  a  v  a  2  s .co m
    // Check if PaymentData available -> when no cancel
    AccountManager acm = AccountManager.get(context);
    Account[] accounts = acm.getAccountsByType(Constants.ACCOUNT_TYPE);
    boolean foundPaymentData = false;
    for (Account account : accounts) {
        PaymentData paymentData = SyncUtils.getPayment(account, acm);
        if (paymentData != null) {
            if (SyncUtils.isPaymentVerificationStarted()) {
                return;
            }

            if (System.currentTimeMillis() > paymentData.paymentSaveDate + TIMEOUT_PENDING_PAYMENT) {
                sendNotification(context, account, R.string.shop_activity_pendingverification,
                        ShopActivity.class, false);
            }
            foundPaymentData = true;
            break;
        }
    }

    if (foundPaymentData) {
        // Check if user has to be notified

        // Start Service
        Intent verifService = new Intent(context, PaymentVerificationService.class);
        context.startService(verifService);
        LogHelper.logD(TAG, "Start PaymentVerificationService", null);
    } else {
        sched.shutdownNow();
    }
}

From source file:hivemall.mix.server.MixServer.java

public void start() throws CertificateException, SSLException, InterruptedException {
    // Configure SSL.
    final SslContext sslCtx;
    if (ssl) {/*from  www . j  a  v a  2 s.c  o  m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    // configure metrics
    ScheduledExecutorService metricCollector = Executors.newScheduledThreadPool(1);
    MixServerMetrics metrics = new MixServerMetrics();
    ThroughputCounter throughputCounter = new ThroughputCounter(metricCollector, 5000L, metrics);
    if (jmx) {// register mbean
        MetricsRegistry.registerMBeans(metrics, port);
    }

    // configure initializer
    SessionStore sessionStore = new SessionStore();
    MixServerHandler msgHandler = new MixServerHandler(sessionStore, syncThreshold, scale);
    MixServerInitializer initializer = new MixServerInitializer(msgHandler, throughputCounter, sslCtx);

    Runnable cleanSessionTask = new IdleSessionSweeper(sessionStore, sessionTTLinSec * 1000L);
    ScheduledExecutorService idleSessionChecker = Executors.newScheduledThreadPool(1);
    try {
        // start idle session sweeper
        idleSessionChecker.scheduleAtFixedRate(cleanSessionTask, sessionTTLinSec + 10L, sweepIntervalInSec,
                TimeUnit.SECONDS);
        // accept connections
        acceptConnections(initializer, port, numWorkers);
    } finally {
        // release threads
        idleSessionChecker.shutdownNow();
        if (jmx) {
            MetricsRegistry.unregisterMBeans(port);
        }
        metricCollector.shutdownNow();
    }
}