Example usage for java.util.concurrent Executors newSingleThreadScheduledExecutor

List of usage examples for java.util.concurrent Executors newSingleThreadScheduledExecutor

Introduction

In this page you can find the example usage for java.util.concurrent Executors newSingleThreadScheduledExecutor.

Prototype

public static ScheduledExecutorService newSingleThreadScheduledExecutor() 

Source Link

Document

Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically.

Usage

From source file:com.cazcade.billabong.store.impl.CloudFilesBasedBinaryStore.java

private void init() {
    //TODO replace with proper client initialisation...
    try {/*from ww w .j av a  2  s . co m*/
        final boolean login = client.login();
        if (!login) {
            throw new RuntimeException("Could not log into cloud files.");
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    final XStream xStream = new XStream();
    final File serializedStoreFile = new File(SERIALIZED_STORE_FILE);
    if (serializedStoreFile.exists()) {
        try {
            map = (Map<String, BinaryStoreEntry>) xStream.fromXML(new FileInputStream(serializedStoreFile));
            for (BinaryStoreEntry binaryStoreEntry : map.values()) {
                if (binaryStoreEntry instanceof CloudFilesBinaryStoreEntry) {
                    ((CloudFilesBinaryStoreEntry) binaryStoreEntry).setClient(client);
                }
            }
            loadAndSaveExecutor.schedule(new Runnable() {
                @Override
                public void run() {
                    try {
                        initInternal(dateFormat, xStream, serializedStoreFile);

                    } catch (Exception e) {
                        e.printStackTrace();
                        Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
                            @Override
                            public void run() {
                                init();
                            }
                        }, 5, TimeUnit.SECONDS);
                    }
                }
            }, 30, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    } else {
        try {
            initInternal(dateFormat, xStream, serializedStoreFile);
        } catch (Exception e) {
            e.printStackTrace(System.err);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e1) {
                return;
            }
            init();
        }
    }
}

From source file:com.amazonaws.services.dynamodbv2.streamsadapter.functionals.CorrectnessTest.java

/**
 * This test spawns a thread to periodically write items to the source table. It shuts down and restarts the KCL
 * worker while writes are happening (to simulate the real-world situation of a worker dying and another taking its
 * place). There are two things being verified here:
 * 1. New KCL worker resumes from the checkpoint
 * 2. All stream records are processed//ww w.  j  av a2  s  . c  o m
 *
 * @throws Exception
 */
@Test
public void workerFailureTest() throws Exception {
    LOG.info("Starting single shard KCL worker failure test.");

    KinesisClientLibConfiguration workerConfig = new KinesisClientLibConfiguration(leaseTable, streamId,
            credentials, KCL_WORKER_ID).withInitialPositionInStream(InitialPositionInStream.TRIM_HORIZON);

    startKCLWorker(workerConfig);

    // A thread that keeps writing to the table every 2 seconds
    ScheduledExecutorService loadGeneratorService = Executors.newSingleThreadScheduledExecutor();
    loadGeneratorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            insertAndUpdateItems(1);
        }
    }, 0/* initialDelay */, 2/* period */, TimeUnit.SECONDS);

    while (recordProcessorFactory.getNumRecordsProcessed() < 10) {
        LOG.info("Sleep till first few records are processed");
        Thread.sleep(THREAD_SLEEP_2S);
    }

    shutDownKCLWorker();

    // Calculate number of records processed by first worker and also the number of processed-but-not-checkpointed
    // records, since checkpoint happens after every batch of 10 records
    int numRecordsProcessedByFirstWorker = recordProcessorFactory.getNumRecordsProcessed();
    int numRecordsNotCheckpointed = numRecordsProcessedByFirstWorker
            % ReplicatingRecordProcessor.CHECKPOINT_BATCH_SIZE;

    // Start a new worker
    startKCLWorker(workerConfig);

    while (recordProcessorFactory.getNumRecordsProcessed() < 0) {
        LOG.info("Sleep till RecordProcessor is initialized");
        Thread.sleep(THREAD_SLEEP_2S);
    }

    loadGeneratorService.shutdown();

    if (!loadGeneratorService.awaitTermination(THREAD_SLEEP_5S, TimeUnit.MILLISECONDS)) {
        loadGeneratorService.shutdownNow();
    }

    int numStreamRecords = 2 * this.numItemsInSrcTable;
    int remainingRecordsToBeProcessed = numStreamRecords - numRecordsProcessedByFirstWorker
            + numRecordsNotCheckpointed;

    /*
     * The second worker must process atleast remainingRecordsToBeProcessed
     * num of records so that we have replicated everything to destination
     * table. Thus, this should never technically end up as an infinite
     * loop. If it does, something else is gone wrong.
     */
    while (recordProcessorFactory.getNumRecordsProcessed() < remainingRecordsToBeProcessed) {
        LOG.info("Sleep till remaining records are processed");
        Thread.sleep(THREAD_SLEEP_2S);
    }

    shutDownKCLWorker();

    ScanResult srcTableScan = TestUtil.scanTable(dynamoDBClient, srcTable);
    ScanResult destTableScan = TestUtil.scanTable(dynamoDBClient, destTable);
    assertEquals(srcTableScan.getItems(), destTableScan.getItems());
}

From source file:org.apereo.openlrs.storage.aws.elasticsearch.XApiOnlyAwsElasticsearchTierTwoStorage.java

public OpenLRSEntity save(OpenLRSEntity entity) {
    if (entity != null && Statement.OBJECT_KEY.equals(entity.getObjectKey())) {
        try {/*from   w w  w.  j  a  v  a  2  s .co m*/
            if (log.isDebugEnabled()) {
                log.debug("statement to index: {}", entity);
            }
            statementQueue.add((Statement) entity);

            if (executorService == null) {
                log.debug("Init executorService with rate " + bulkIndexScheduleRateSecond);
                executorService = Executors.newSingleThreadScheduledExecutor();
                executorService.scheduleAtFixedRate(task, 0, bulkIndexScheduleRateSecond, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            e.printStackTrace();
        }
    } else if (entity != null) {
        log.warn("XApiOnlyAwsElasticsearchTierTwoStorage does not support " + entity.getObjectKey());
    }

    return entity;
}

From source file:org.wso2.carbon.registry.eventing.RegistryEventDispatcher.java

public RegistryEventDispatcher() {
    digestQueues = new LinkedHashMap<String, Queue<DigestEntry>>();
    for (String s : new String[] { "h", "d", "w", "f", "m", "y" }) {
        //TODO: Identify Queuing mechanisms.
        digestQueues.put(s, new ConcurrentLinkedQueue<DigestEntry>());
    }//from  ww  w  .  j a v  a2 s  .c om
    final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(new Runnable() {
        public void run() {
            GregorianCalendar utc = new GregorianCalendar(SimpleTimeZone.getTimeZone("UTC"));
            Map<String, List<DigestEntry>> digestEntries = new HashMap<String, List<DigestEntry>>();
            try {
                addToDigestEntryQueue(digestEntries, "h");
                if (utc.get(Calendar.HOUR_OF_DAY) == 0) {
                    addToDigestEntryQueue(digestEntries, "d");
                    if (utc.get(Calendar.DAY_OF_WEEK) == 1) {
                        addToDigestEntryQueue(digestEntries, "w");
                        if (utc.get(Calendar.WEEK_OF_YEAR) % 2 != 0) {
                            addToDigestEntryQueue(digestEntries, "f");
                        }
                    }
                    if (utc.get(Calendar.DAY_OF_MONTH) == 1) {
                        addToDigestEntryQueue(digestEntries, "m");
                        if (utc.get(Calendar.DAY_OF_YEAR) == 1) {
                            addToDigestEntryQueue(digestEntries, "y");

                        }
                    }
                }
                for (Map.Entry<String, List<DigestEntry>> e : digestEntries.entrySet()) {
                    List<DigestEntry> value = e.getValue();
                    Collections.sort(value, new Comparator<DigestEntry>() {
                        public int compare(DigestEntry o1, DigestEntry o2) {
                            if (o1.getTime() > o2.getTime()) {
                                return -1;
                            } else if (o1.getTime() < o2.getTime()) {
                                return 1;
                            }
                            return 0;
                        }
                    });
                    StringBuffer buffer = new StringBuffer();
                    for (DigestEntry entry : value) {
                        buffer.append(entry.getMessage()).append("\n\n");
                    }
                    RegistryEvent<String> re = new RegistryEvent<String>(buffer.toString());
                    re.setTopic(RegistryEvent.TOPIC_SEPARATOR + "DigestEvent");
                    DispatchEvent de = new DispatchEvent(re, e.getKey(), true);
                    Subscription subscription = new Subscription();
                    subscription.setTopicName(re.getTopic());
                    publishEvent(de, subscription, e.getKey(), true);
                }
            } catch (RuntimeException ignored) {
                // Eat any runtime exceptions that occurred, we don't care if the message went
                // or not.
            }
        }
    }, System.currentTimeMillis() % (1000 * 60 * 60), 1000 * 60 * 60, TimeUnit.MILLISECONDS);
    try {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                executorService.shutdownNow();
            }
        });
    } catch (IllegalStateException e) {
        executorService.shutdownNow();
        throw new IllegalStateException(
                "Unable to create registry event dispatcher during " + "shutdown process.");
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.router.DnsRoutePerformanceTest.java

@Before
public void before() throws Exception {
    CacheRegister cacheRegister = new CacheRegister();

    JSONTokener healthTokener = new JSONTokener(new FileReader("src/test/db/health.json"));
    JSONObject healthObject = new JSONObject(healthTokener);

    JSONTokener jsonTokener = new JSONTokener(new FileReader("src/test/db/cr-config.json"));
    JSONObject crConfigJson = new JSONObject(jsonTokener);
    JSONObject locationsJo = crConfigJson.getJSONObject("edgeLocations");
    final Set<CacheLocation> locations = new HashSet<CacheLocation>(locationsJo.length());
    for (final String loc : JSONObject.getNames(locationsJo)) {
        final JSONObject jo = locationsJo.getJSONObject(loc);
        locations.add(new CacheLocation(loc, jo.optString("zoneId"),
                new Geolocation(jo.getDouble("latitude"), jo.getDouble("longitude"))));
    }// ww w  .ja  va  2  s.co m

    names = new DnsNameGenerator().getNames(crConfigJson.getJSONObject("deliveryServices"),
            crConfigJson.getJSONObject("config"));

    cacheRegister.setConfig(crConfigJson);
    CacheRegisterBuilder.parseDeliveryServiceConfig(crConfigJson.getJSONObject("deliveryServices"),
            cacheRegister);

    cacheRegister.setConfiguredLocations(locations);
    CacheRegisterBuilder.parseCacheConfig(crConfigJson.getJSONObject("contentServers"), cacheRegister);

    NetworkUpdater networkUpdater = new NetworkUpdater();
    networkUpdater.setDatabasesDirectory(Paths.get("src/test/db"));
    networkUpdater.setDatabaseName("czmap.json");
    networkUpdater.setExecutorService(Executors.newSingleThreadScheduledExecutor());
    networkUpdater.setTrafficRouterManager(mock(TrafficRouterManager.class));
    JSONObject configJson = crConfigJson.getJSONObject("config");
    networkUpdater.setDataBaseURL(configJson.getString("coveragezone.polling.url"),
            configJson.getLong("coveragezone.polling.interval"));

    File coverageZoneFile = new File("src/test/db/czmap.json");
    while (!coverageZoneFile.exists()) {
        Thread.sleep(500);
    }

    NetworkNode.generateTree(coverageZoneFile);

    ZoneManager zoneManager = mock(ZoneManager.class);

    MD5HashFunctionPoolableObjectFactory md5factory = new MD5HashFunctionPoolableObjectFactory();
    GenericObjectPool pool = new GenericObjectPool(md5factory);

    whenNew(ZoneManager.class)
            .withArguments(any(TrafficRouter.class), any(StatTracker.class), any(TrafficOpsUtils.class))
            .thenReturn(zoneManager);

    trafficRouter = new TrafficRouter(cacheRegister, mock(GeolocationService.class),
            mock(GeolocationService.class), pool, mock(StatTracker.class), mock(TrafficOpsUtils.class),
            mock(FederationRegistry.class));

    trafficRouter.setApplicationContext(mock(ApplicationContext.class));

    trafficRouter = spy(trafficRouter);

    doCallRealMethod().when(trafficRouter).getCoverageZoneCache(anyString(), any(DeliveryService.class));

    doCallRealMethod().when(trafficRouter).selectCache(any(Request.class), any(DeliveryService.class),
            any(Track.class));
    doCallRealMethod().when(trafficRouter, "selectCache", any(CacheLocation.class), any(DeliveryService.class));
    doCallRealMethod().when(trafficRouter, "getSupportingCaches", any(List.class), any(DeliveryService.class));
    doCallRealMethod().when(trafficRouter).setState(any(JSONObject.class));
    doCallRealMethod().when(trafficRouter).selectDeliveryService(any(Request.class), anyBoolean());
    doReturn(new Geolocation(39.739167, -104.984722)).when(trafficRouter).getLocation(anyString());

    trafficRouter.setState(healthObject);

    JSONObject coverageZoneMap = new JSONObject(new JSONTokener(new FileReader("src/test/db/czmap.json")));
    JSONObject coverageZones = coverageZoneMap.getJSONObject("coverageZones");

    Iterator iterator = coverageZones.keys();
    Map<String, Integer> seen = new HashMap<String, Integer>();

    while (iterator.hasNext()) {
        String coverageZoneName = (String) iterator.next();
        JSONObject coverageZoneJson = coverageZones.getJSONObject(coverageZoneName);
        JSONArray networks = coverageZoneJson.getJSONArray("network");
        Set<String> hosts = hostMap.get(coverageZoneName);

        if (hosts == null) {
            hosts = new HashSet<String>();
        }

        for (int i = 0; i < networks.length(); i++) {
            String network = networks.getString(i).split("/")[0];
            InetAddress ip = InetAddresses.forString(network);
            ip = InetAddresses.increment(ip);
            String ipstr = InetAddresses.toAddrString(ip);
            hosts.add(ipstr);

            if (seen.containsKey(ipstr)) {
                seen.put(ipstr, seen.get(ipstr) + 1);
            } else {
                seen.put(ipstr, 1);
            }
        }

        final CacheLocation location = cacheRegister.getCacheLocation(coverageZoneName);

        if (location != null || (location == null && coverageZoneJson.has("coordinates"))) {
            coverageZoneRouted.add(coverageZoneName);
        }

        hostMap.put(coverageZoneName, hosts);
    }

    // remove any dups that will cause a GEO result
    for (String ip : seen.keySet()) {
        if (seen.get(ip) > 1) {
            for (String coverageZoneName : hostMap.keySet()) {
                Set<String> ips = hostMap.get(coverageZoneName);
                ips.remove(ip);
            }
        }
    }
}

From source file:org.apereo.openlrs.repositories.statements.ElasticSearchStatementRepository.java

public void onMessage(String statementJSON) {

    try {// w w w  . j a  va 2  s .  c om
        Statement statement = objectMapper.readValue(statementJSON.getBytes(), Statement.class);
        if (log.isDebugEnabled()) {
            log.debug("statement to index: {}", statement);
        }
        statementQueue.add(statement);

        if (executorService == null) {
            log.debug("Init executorService with rate " + bulkIndexScheduleRateSecond);
            executorService = Executors.newSingleThreadScheduledExecutor();
            executorService.scheduleAtFixedRate(task, 0, bulkIndexScheduleRateSecond, TimeUnit.SECONDS);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        //TODO - what else?
    }
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * ?DiamondSubscriber<br>/*ww  w.j  a v a2s .co  m*/
 * 1.?DataId??<br>
 * 2.??DataId??<br>
 */
public synchronized void start() {
    if (isRun) {
        return;
    }

    if (null == scheduledExecutor || scheduledExecutor.isTerminated()) {
        scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
    }

    localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);
    serverAddressProcessor = new ServerAddressProcessor(this.diamondConfigure, this.scheduledExecutor);
    serverAddressProcessor.start();

    this.snapshotConfigInfoProcessor = new SnapshotConfigInfoProcessor(
            this.diamondConfigure.getFilePath() + "/" + SNAPSHOT_DIR);
    // domainNamePos
    randomDomainNamePos();
    initHttpClient();

    // ?
    isRun = true;

    if (log.isInfoEnabled()) {
        log.info("???" + this.diamondConfigure.getDomainNameList());
    }

    if (MockServer.isTestMode()) {
        bFirstCheck = false;
    } else {
        // 
        this.diamondConfigure.setPollingIntervalTime(Constants.POLLING_INTERVAL_TIME);
    }
    // 
    rotateCheckConfigInfo();

    addShutdownHook();
}

From source file:org.dcm4che3.tool.mppsscp.MppsSCP.java

private void configure(boolean hasOptionNoValidate, String mppsNCreateIOD, String mppsNSetIOD, boolean ignore,
        String directory) throws IOException, GeneralSecurityException {
    configureStorageDirectory(this, ignore, directory);
    configureIODs(this, hasOptionNoValidate, mppsNCreateIOD, mppsNSetIOD);

    ExecutorService executorService = Executors.newCachedThreadPool();
    ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    device.setScheduledExecutor(scheduledExecutorService);
    device.setExecutor(executorService);
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * DiamondSubscriber<br>/*  w  w  w  .  ja va  2s .c o  m*/
 * 1.DataId<br>
 * 2.DataId<br>
 */
public synchronized void start() {
    if (isRun) {
        return;
    }

    if (null == scheduledExecutor || scheduledExecutor.isTerminated()) {
        scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
    }

    localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);
    serverAddressProcessor = new ServerAddressProcessor(this.diamondConfigure, this.scheduledExecutor);
    serverAddressProcessor.start();

    this.snapshotConfigInfoProcessor = new SnapshotConfigInfoProcessor(
            this.diamondConfigure.getFilePath() + "/" + SNAPSHOT_DIR);
    // domainNamePos
    randomDomainNamePos();
    initHttpClient();

    // 
    isRun = true;

    if (log.isInfoEnabled()) {
        log.info("" + this.diamondConfigure.getDomainNameList());
    }

    if (MockServer.isTestMode()) {
        bFirstCheck = false;
    } else {
        // 
        this.diamondConfigure.setPollingIntervalTime(Constants.POLLING_INTERVAL_TIME);
    }
    // 
    rotateCheckConfigInfo();

    addShutdownHook();
}

From source file:com.streamsets.datacollector.antennadoctor.storage.AntennaDoctorStorage.java

@Override
protected void initTask() {
    LOG.info("Repository location: {}", repositoryDirectory);

    try {/*w w  w.j  ava2 s  .  c  o  m*/
        // Make sure that we have our own directory to operate in
        if (!Files.exists(repositoryDirectory)) {
            Files.createDirectories(repositoryDirectory);
        }

        Path store = repositoryDirectory.resolve(AntennaDoctorConstants.FILE_DATABASE);
        if (!Files.exists(store)) {
            try (OutputStream stream = Files.newOutputStream(store)) {
                Resources.copy(
                        Resources.getResource(AntennaDoctorStorage.class, AntennaDoctorConstants.FILE_DATABASE),
                        stream);
            }
        }
    } catch (IOException e) {
        LOG.error("Cant initialize repository: {}", e.getMessage(), e);
        return;
    }

    // Schedule override runnable if allowed in configuration
    if (configuration.get(AntennaDoctorConstants.CONF_OVERRIDE_ENABLE,
            AntennaDoctorConstants.DEFAULT_OVERRIDE_ENABLE)) {
        LOG.info("Enabling polling of {} to override the rule database", AntennaDoctorConstants.FILE_OVERRIDE);
        this.executorService = Executors.newSingleThreadExecutor();
        this.overrideRunnable = new OverrideFileRunnable();
        this.future = executorService.submit(this.overrideRunnable);
    }

    // Remote repo handling
    if (configuration.get(AntennaDoctorConstants.CONF_UPDATE_ENABLE,
            AntennaDoctorConstants.DEFAULT_UPDATE_ENABLE)) {
        if (overrideRunnable != null) {
            LOG.info("Using override, not starting update thread.");
        } else {
            this.executorService = Executors.newSingleThreadScheduledExecutor();
            this.updateRunnable = new UpdateRunnable();
            this.future = ((ScheduledExecutorService) executorService).scheduleAtFixedRate(updateRunnable,
                    configuration.get(AntennaDoctorConstants.CONF_UPDATE_DELAY,
                            AntennaDoctorConstants.DEFAULT_UPDATE_DELAY),
                    configuration.get(AntennaDoctorConstants.CONF_UPDATE_PERIOD,
                            AntennaDoctorConstants.DEFAULT_UPDATE_PERIOD),
                    TimeUnit.MINUTES);
        }
    }

    // And finally load rules
    delegate.loadNewRules(loadRules());
}