public void stop() { try { flush(); } catch (IOException e) { LOGGER.error("Error flushing", e); } try { close(); } catch (IOException e) { LOGGER.error("Error closing", e); } try { backgroundFlushTask.shutdown(); // Wait a while for existing tasks to terminate if (!backgroundFlushTask.awaitTermination(15, TimeUnit.SECONDS)) { backgroundFlushTask.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!backgroundFlushTask.awaitTermination(15, TimeUnit.SECONDS)) { LOGGER.error("Stream did not terminate"); } } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted backgroundFlushTask.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } }
synchronized void start() { if (executor.isShutdown()) { throw new IllegalStateException(); } stop(); ticker = executor.scheduleAtFixedRate(this::incrementTicks, 0, tickPeriod, TimeUnit.MICROSECONDS); }
synchronized void shutdown() { stop(); executor.shutdown(); try { executor.awaitTermination(3, TimeUnit.SECONDS); } catch (InterruptedException e) { executor.shutdownNow(); } }
public void run() throws Exception { for (Thread t : searcherThreads) { t.start(); } for (Thread t : writerThreads) { t.start(); } barrier1.await(); Refresher refresher = new Refresher(); scheduledExecutorService.scheduleWithFixedDelay( refresher, refreshSchedule.millis(), refreshSchedule.millis(), TimeUnit.MILLISECONDS); Flusher flusher = new Flusher(); scheduledExecutorService.scheduleWithFixedDelay( flusher, flushSchedule.millis(), flushSchedule.millis(), TimeUnit.MILLISECONDS); StopWatch stopWatch = new StopWatch(); stopWatch.start(); barrier2.await(); latch.await(); stopWatch.stop(); System.out.println("Summary"); System.out.println( " -- Readers [" + searcherThreads.length + "] with [" + searcherIterations + "] iterations"); System.out.println( " -- Writers [" + writerThreads.length + "] with [" + writerIterations + "] iterations"); System.out.println(" -- Took: " + stopWatch.totalTime()); System.out.println( " -- Refresh [" + refresher.id + "] took: " + refresher.stopWatch.totalTime()); System.out.println(" -- Flush [" + flusher.id + "] took: " + flusher.stopWatch.totalTime()); System.out.println(" -- Store size " + store.estimateSize()); scheduledExecutorService.shutdown(); engine.refresh(new Engine.Refresh(true)); stopWatch = new StopWatch(); stopWatch.start(); Engine.Searcher searcher = engine.searcher(); TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), idGenerator.get() + 1); stopWatch.stop(); System.out.println( " -- Indexed [" + idGenerator.get() + "] docs, found [" + topDocs.totalHits + "] hits, took " + stopWatch.totalTime()); searcher.release(); }
void onActivity() { cancel(); if (!inactivityTimer.isShutdown()) { try { inactivityFuture = inactivityTimer.schedule( new FinishListener(activity), INACTIVITY_DELAY_SECONDS, TimeUnit.SECONDS); } catch (RejectedExecutionException ree) { // surprising, but could be normal if for some reason the implementation just doesn't // think it can shcedule again. Since this time-out is non-essential, just forget it } } }
public JobLoggerDelegate(Config config) { JobLoggerFactory jobLoggerFactory = ExtensionLoader.getExtensionLoader(JobLoggerFactory.class).getAdaptiveExtension(); jobLogger = jobLoggerFactory.getJobLogger(config); lazyLog = config.getParameter(Constants.LAZY_JOB_LOGGER, false); if (lazyLog) { // 无界Queue memoryQueue = new LinkedBlockingQueue<JobLogPo>(); maxMemoryLogSize = config.getParameter(Constants.LAZY_JOB_LOGGER_MEM_SIZE, 1000); flushPeriod = config.getParameter(Constants.LAZY_JOB_LOGGER_CHECK_PERIOD, 3); executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("LazyJobLogger")); scheduledFuture = executor.scheduleWithFixedDelay( new Runnable() { @Override public void run() { try { if (flushing.compareAndSet(false, true)) { checkAndFlush(); } } catch (Throwable t) { LOGGER.error("CheckAndFlush log error", t); } } }, flushPeriod, flushPeriod, TimeUnit.SECONDS); } }
protected void initWebServer(ServiceProvider serviceProvider, IdeArguments ideArguments) { port(ideArguments.getPort()); staticFileLocation("/public"); externalStaticFileLocation(staticFolderForSpark); System.out.println("Reports are in: " + reportFolder); new DeviceController(serviceProvider.deviceService()); new DomSnapshotController(serviceProvider.domSnapshotService()); new FileBrowserController(serviceProvider.fileBrowserService()); new SettingsController(serviceProvider.settingsService()); new ProfilesController(serviceProvider.profilesService(), serviceProvider.settingsService()); new TaskResultController(serviceProvider.taskResultService()); new TesterController(serviceProvider.testerService()); new HelpController(); scheduledExecutorService.scheduleAtFixedRate( new TaskResultsStorageCleanupJob( taskResultsStorage, ideArguments.getKeepLastResults(), ideArguments.getZombieResultsTimeout(), reportFolder), ideArguments.getCleanupPeriodInMinutes(), ideArguments.getCleanupPeriodInMinutes(), TimeUnit.MINUTES); if (ideArguments.getProfile() != null) { serviceProvider.profilesService().loadProfile(ideArguments.getProfile()); } }
private Future<Object> exec( final Map<String, Object> userCtx, final ITemplate tmpl, final String template, final File file, final Object... args) { return scheduler.submit( new Callable<Object>() { @Override public Object call() throws Exception { try { engine.prepare(userCtx); ITemplate t = tmpl; if (null != t) { } else if (null != template) { t = engine.getTemplate(template, args); } else if (null != file) { t = engine.getTemplate(file, args); } else { throw new NullPointerException(); } return t.__setSecureCode(code).render(); } catch (Exception e) { return e; } } }); }
protected void prepare( Map stormConf, final TopologyContext context, final IOutputCollector collector) { _rand = new Random(); _collector = collector; _context = context; heartbeatTimeoutMills = getHeartbeatTimeoutMillis(stormConf); _process = new NuShellProcess(_command, this, this); // subprocesses must send their pid first thing Number subpid = _process.launch(stormConf, context); LOG.info("Launched subprocess with pid " + subpid); this.pid = subpid.longValue(); /** * randomizing the initial delay would prevent all shell bolts from heartbeating at the same * time frame */ int initialDelayMillis = random.nextInt(4000) + 1000; BoltHeartbeatTimerTask task = new BoltHeartbeatTimerTask(this); heartBeatExecutorService.scheduleAtFixedRate( task, initialDelayMillis, getHeartbeatPeriodMillis(stormConf), TimeUnit.MILLISECONDS); }
/** * Stop the bundle. Nothing to stop for now. * * @param bundleContext <tt>BundleContext</tt> provided by OSGi framework * @throws Exception if something goes wrong during stop */ public void stop(BundleContext bundleContext) throws Exception { if (logger.isDebugEnabled()) logger.debug("Update checker [STOPPED]"); if (mUpdateExecutor != null) { mUpdateExecutor.shutdown(); mUpdateExecutor = null; } }
/** * Helper method fro providers to fire hotkey event in a separate thread * * @param hotKey hotkey to fire */ protected void fireEvent(HotKey hotKey) { List<HotKeyEvent> listenerList = listeners.get(hotKey); if (listenerList != null) { for (HotKeyEvent event : listenerList) { eventQueue.execute(event); } } }
public void shutdown() { if (!started) { throw new IllegalStateException( "Service was not started, it does not make sense to shut it down"); } shutdownSamp(); shutdownSherpa(); executor.shutdown(); started = false; }
private void fixTimeLimit() { if (timeLimitFuture != null) timeLimitFuture.cancel(true); if (running && limits.containsKey(TIMER_COUNTER)) { long delay = limits.get(TIMER_COUNTER) * 1000 - time; if (delay > 0) { timeLimitFuture = scheduler.schedule(new TimeLimitTask(), delay, TimeUnit.MILLISECONDS); } } }
private void startSherpa() { logger.log(Level.INFO, "Starting Sherpa monitor thread"); Runnable sherpaMonitor = new Runnable() { @Override public void run() { monitorSherpaOnce(); } }; sherpaMonitorHandle = executor.scheduleAtFixedRate(sherpaMonitor, 0, 3, TimeUnit.SECONDS); }
/** {@inheritDoc} */ public ScheduledFuture<String> schedule(Command action, long delay, TimeUnit unit) throws RejectedExecutionException, NullPointerException { try { CallableActionWrapper task = new CallableActionWrapper(action); return mThreadPool.schedule(task, delay, unit); } catch (RejectedExecutionException e) { LOG.error("ActionBase.schedule()... RejectedExecutionException:" + e.getLocalizedMessage()); throw e; } catch (NullPointerException e) { LOG.error("ActionBase.schedule()... Exception in schedule:" + e.getLocalizedMessage()); throw e; } }
public void start() { connectToMongo(); backgroundFlushTask.scheduleAtFixedRate( new Runnable() { @Override public void run() { flushIfNecessary(); } }, 0, MAX_WRITE_LATENCY * 2, TimeUnit.MILLISECONDS); }
public RedisRegistry(AppContext appContext) { super(appContext); Config config = appContext.getConfig(); this.clusterName = config.getClusterName(); this.lock = new RedisLock("LTS_CLEAN_LOCK_KEY", config.getIdentity(), 2 * 60); // 锁两分钟过期 JedisPoolConfig redisConfig = new JedisPoolConfig(); // TODO 可以设置n多参数 String address = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress()); String cluster = config.getParameter("cluster", "failover"); if (!"failover".equals(cluster) && !"replicate".equals(cluster)) { throw new IllegalArgumentException( "Unsupported redis cluster: " + cluster + ". The redis cluster only supported failover or replicate."); } replicate = "replicate".equals(cluster); this.reconnectPeriod = config.getParameter( ExtConfig.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD); String[] addrs = address.split(","); for (String addr : addrs) { int i = addr.indexOf(':'); String host = addr.substring(0, i); int port = Integer.parseInt(addr.substring(i + 1)); this.jedisPools.put(addr, new JedisPool(redisConfig, host, port, Constants.DEFAULT_TIMEOUT)); } this.expirePeriod = config.getParameter(ExtConfig.REDIS_SESSION_TIMEOUT, Constants.DEFAULT_SESSION_TIMEOUT); this.expireFuture = expireExecutor.scheduleWithFixedDelay( new Runnable() { public void run() { try { deferExpired(); // 延长过期时间 } catch (Throwable t) { // 防御性容错 LOGGER.error( "Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t); } } }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS); }
public Future<Object> executeAsync(final ITemplate t) { final Future<Object> f = exec(null, t, null, null, null); // make sure it get cancelled if timeout scheduler.schedule( new Runnable() { @Override public void run() { f.cancel(true); } }, timeout, TimeUnit.MILLISECONDS); return f; }
private void onStartExecution() { log.debug("Starting canvas {}", getLabel()); setRunning(true); setTime(0); synchronized (datesLock) { startTime = new Date(); endTime = null; } timerFuture = scheduler.scheduleAtFixedRate(new TimeUpdateTask(), 250, 250, TimeUnit.MILLISECONDS); fixTimeLimit(); hasStarted = true; setCompleted(false); }
/** {@inheritDoc} */ public Future<String> submit(Command action) throws RejectedExecutionException, NullPointerException { try { return this.schedule(action, 0, TimeUnit.MILLISECONDS); } catch (RejectedExecutionException e) { LOG.error("ActionBase.schedule()... RejectedExecutionException:" + e.getLocalizedMessage()); LOG.error("\tThread Pool Exector Shutdown: " + mThreadPool.isShutdown()); throw e; } catch (NullPointerException e) { LOG.error("ActionBase.schedule()... Exception in schedule:" + e.getLocalizedMessage()); throw e; } }
/** * The dependent service is available and the bundle will start. * * @param dependentService the UIService this activator is waiting. */ @Override public void start(Object dependentService) { if (logger.isDebugEnabled()) logger.debug("Update checker [STARTED]"); ConfigurationService cfg = getConfiguration(); if (OSUtils.IS_WINDOWS) { updateService = new Update(); bundleContext.registerService(UpdateService.class.getName(), updateService, null); // Register the "Check for Updates" menu item if // the "Check for Updates" property isn't disabled. if (!cfg.getBoolean(CHECK_FOR_UPDATES_MENU_DISABLED_PROP, false)) { // Register the "Check for Updates" menu item. CheckForUpdatesMenuItemComponent checkForUpdatesMenuItemComponent = new CheckForUpdatesMenuItemComponent(Container.CONTAINER_HELP_MENU); Hashtable<String, String> toolsMenuFilter = new Hashtable<String, String>(); toolsMenuFilter.put(Container.CONTAINER_ID, Container.CONTAINER_HELP_MENU.getID()); bundleContext.registerService( PluginComponent.class.getName(), checkForUpdatesMenuItemComponent, toolsMenuFilter); } // Check for software update upon startup if enabled. if (cfg.getBoolean(UPDATE_ENABLED, true)) updateService.checkForUpdates(false); } if (cfg.getBoolean(CHECK_FOR_UPDATES_DAILY_ENABLED_PROP, false)) { logger.info("Scheduled update checking enabled"); // Schedule a "check for updates" task that will run once a day int hoursToWait = calcHoursToWait(); Runnable updateRunnable = new Runnable() { public void run() { logger.debug("Performing scheduled update check"); getUpdateService().checkForUpdates(false); } }; mUpdateExecutor = Executors.newSingleThreadScheduledExecutor(); mUpdateExecutor.scheduleAtFixedRate( updateRunnable, hoursToWait, 24 * 60 * 60, TimeUnit.SECONDS); } if (logger.isDebugEnabled()) logger.debug("Update checker [REGISTERED]"); }
void execute(Application application) { final String appId = application.getId(); ScheduledFuture scheduledFuture = scheduledExecutorService.scheduleAtFixedRate( new Runnable() { @Override public void run() { try { Date startDate = DateUtil.getTodayBeginDate(); Date endDate = new Date(); List<Url> urls = applicationRepository.selectAllUrlsWithApplicationId(appId); List<UrlResponseTime> urlResponseTimes = new ArrayList<UrlResponseTime>(); for (Url url : urls) { UrlResponseTime urlResponseTime = urlResponseTimeRepository.selectUrlResponseTimesForMonitorUrl( url.getId(), startDate, endDate, appId); urlResponseTime = urlResponseTime == null ? new UrlResponseTime() : urlResponseTime; urlResponseTime.setApplicationId(appId); urlResponseTime.setUrlId(url.getId()); urlResponseTime.setUrl(url.getUrl()); urlResponseTime.initViewValues(); urlResponseTimes.add(urlResponseTime); } for (UrlResponseTime urlResponseTime : urlResponseTimes) { urlResponseTime.setHealthBar( healthStaCache.getHealthBar(appId, urlResponseTime.getUrlId())); urlResponseTime.setUrlHref( "<a href='javascript:void(0);' onclick=\"urlDetail('" + appId + "', '" + urlResponseTime.getUrlId() + "')\">" + urlResponseTime.getUrl() + "</a>"); } urlResponseTimeCache.put(appId, urlResponseTimes); } catch (Throwable t) { } } }, 0, 27, TimeUnit.SECONDS); threadHolderMap.put(application.getId(), scheduledFuture); }
private void scheduleLedData(final int position, LedData data) { ledDataMap.put(position, data); thereIsNewData = true; if (data.duration > 0) { executorService.schedule( new Runnable() { @Override public void run() { scheduleLedData(position, new LedData()); } }, data.duration, TimeUnit.MILLISECONDS); } }
void scheduleReset( List<Integer> needsReset, AtomicIntegerArray oldUse, AtomicIntegerArray updated) { if (needsReset.size() == 0) return; executor.schedule( () -> { for (int i = 0; i < needsReset.size(); i++) { int index = needsReset.get(i); int value = oldUse.get(index); if (value == 0) { updated.set(index, 0); needsReset.remove(i--); } } scheduleReset(needsReset, oldUse, updated); }, 1, TimeUnit.MILLISECONDS); }
public AbstractPreLoader(final Application application) { if (start.compareAndSet(false, true)) { scheduledFuture = LOAD_EXECUTOR_SERVICE.scheduleWithFixedDelay( new Runnable() { @Override public void run() { for (String loadTaskTrackerNodeGroup : LOAD_SIGNAL) { BlockingQueue<JobPo> queue = JOB_MAP.get(loadTaskTrackerNodeGroup); if (queue.size() / step < factor) { // load List<JobPo> loads = load(loadTaskTrackerNodeGroup, curSequence * step); // 加入到内存中 if (CollectionUtils.isNotEmpty(loads)) { queue.addAll(loads); } } LOAD_SIGNAL.remove(loadTaskTrackerNodeGroup); } } }, 500, 500, TimeUnit.MILLISECONDS); } application .getEventCenter() .subscribe( new EventSubscriber( application.getConfig().getIdentity() + "_preLoader", new Observer() { @Override public void onObserved(EventInfo eventInfo) { setCurSequence(application); } }), EcTopic.NODE_ADD, EcTopic.NODE_REMOVE); setCurSequence(application); }
private void startSamp() { logger.log(Level.INFO, "Initializing Hub Connector"); sampClient = new HubConnector(sampClientProfile); sampClient.declareMetadata(metadata); sampClient.declareSubscriptions(sampClient.computeSubscriptions()); sampClient.setActive(true); Runnable sampMonitor = new Runnable() { @Override public void run() { monitorSampOnce(); } }; if (resourceServer != null) { resourceServer.start(); logger.log(Level.INFO, "Starting SAMP resource server"); } logger.log(Level.INFO, "Starting SAMP monitor thread"); sampMonitorHandle = executor.scheduleAtFixedRate(sampMonitor, 0, 3, TimeUnit.SECONDS); }
/** * Sends a message. * * <p>Current thread is NOT blocked by this method call. But no two response-actions (onReceive or * onFail on any request) or response protocols will be executed at same time, so you can write * not thread-safe code inside them. * * <p> * * <p>This gets being very similar to automaton programming :) * * @param address receiver of message * @param message mail entry * @param type way of sending a message: TCP, single UPD... * @param timeout timeout in milliseconds * @param receiveListener an action to invoke when got an answer. * @param failListener an action to invoke when timeout exceeded. * @param <ReplyType> response message type */ public <ReplyType extends ResponseMessage> void send( InetSocketAddress address, RequestMessage<ReplyType> message, DispatchType type, int timeout, ReceiveListener<ReplyType> receiveListener, FailListener failListener) { BlockingQueue<ResponseMessage> responseContainer = submit(address, message, type); // TODO: make in single thread scheduledExecutor.schedule( () -> { //noinspection unchecked ReplyType response = (ReplyType) responseContainer.poll(); if (response != null) receiveListener.onReceive(address, response); else failListener.onFail(address); }, timeout, TimeUnit.MILLISECONDS); // TODO: clear responseWaiters map }
public void receiveData( String ip, String paramName, String paramNo, int paramBote, int paramLength, int parmParity, int parmStopBit, int parmDelay) { try { try { m = new Mongo(ip, 27017); db = m.getDB(paramName); // db.authenticate("test", "123".toCharArray()); } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } final DBCollection coll = db.getCollection("DATAIN"); final DBCollection collout = db.getCollection("DATAOUT"); DBCollection meta = db.getCollection("META"); // 记录数据字段 final Map map1 = new HashMap(); final Map map2 = new HashMap(); Map map00 = new HashMap(); Map map01 = new HashMap(); Map map02 = new HashMap(); Map map03 = new HashMap(); final Map m_ai_max = new HashMap(); final Map m_ai_min = new HashMap(); final Map m_ao_max = new HashMap(); final Map m_ao_min = new HashMap(); DBCursor cursor = meta.find(); while (cursor.hasNext()) { // 记录数据类型 DBObject dbo = cursor.next(); String name = dbo.get("_id").toString(); String type = dbo.get("type").toString(); String addr = dbo.get("addr").toString(); Double max = (Double) dbo.get("max"); Double min = (Double) dbo.get("min"); if (type.equals("AI")) { map00.put(name, addr); m_ai_max.put(name, max); m_ai_min.put(name, min); } if (type.equals("DI")) { map01.put(name, addr); } if (type.equals("AO")) { map02.put(name, addr); } if (type.equals("DO")) { map03.put(name, addr); } } map1.put("AI", map00); map1.put("DI", map01); map2.put("AO", map02); map2.put("DO", map03); SerialParameters params = new SerialParameters(); params.setCommPortId(paramNo); params.setBaudRate(paramBote); params.setDataBits(paramLength); params.setParity(parmParity); params.setStopBits(parmStopBit); ModbusFactory modbusFactory = new ModbusFactory(); slave = modbusFactory.createRtuSlave(params); slave.addProcessImage(getModscanProcessImage(1, map00, coll)); slave.addProcessImage(getModscanProcessImage(2, map01, coll)); slave.addProcessImage(getModscanProcessImage(3, map02, collout)); slave.addProcessImage(getModscanProcessImage(4, map03, collout)); new Thread( new Runnable() { public void run() { try { slave.start(); // JOptionPane.showMessageDialog(f, "通讯连接成功!"); } catch (ModbusInitException e) { e.printStackTrace(); } } }) .start(); ScheduledExecutorService timerRead = Executors.newScheduledThreadPool(1); timerRead.scheduleAtFixedRate( new Runnable() { public void run() { new TaskRead(map1, m_ai_max, m_ai_min, coll, slave); } }, 500, parmDelay, TimeUnit.MILLISECONDS); ScheduledExecutorService timerWrite = Executors.newScheduledThreadPool(2); timerWrite.scheduleAtFixedRate( new Runnable() { public void run() { new TaskWrite(map2, m_ao_max, m_ao_min, collout, slave); } }, 500, parmDelay, TimeUnit.MILLISECONDS); } catch (Exception ex) { } }
public void shutdown() { cancel(); inactivityTimer.shutdown(); }
public void onActivity() { cancel(); inactivityFuture = inactivityTimer.schedule( new FinishListener(activity), INACTIVITY_DELAY_SECONDS, TimeUnit.SECONDS); }