List of usage examples for java.util.concurrent ScheduledExecutorService scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
From source file:de.zib.scalaris.examples.wikipedia.bliki.WikiServlet.java
/** * Starts the service updating the bloom filter for existing pages. *//* w ww.j a va 2 s . c o m*/ protected void startExistingPagesUpdate() { final int rebuildDelay = Options.getInstance().WIKI_REBUILD_PAGES_CACHE; if (rebuildDelay > 0) { updateExistingPages(); ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); ses.scheduleWithFixedDelay(new Runnable() { @Override public void run() { updateExistingPages(); } }, rebuildDelay, rebuildDelay, TimeUnit.SECONDS); } }
From source file:org.fao.geonet.OgpAppHandler.java
/** * Sets up a periodic check whether GeoNetwork can successfully write to the database. If it can't, GeoNetwork will * automatically switch to read-only mode. *//*from w ww . j av a2 s .c om*/ private void createDBHeartBeat(final GeonetContext gc, Integer initialDelay, Integer fixedDelay) throws SchedulerException { logger.info("creating DB heartbeat with initial delay of " + initialDelay + " s and fixed delay of " + fixedDelay + " s"); ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); Runnable DBHeartBeat = new Runnable() { @Override public void run() { try { boolean readOnly = gc.isReadOnly(); logger.debug("DBHeartBeat: GN is read-only ? " + readOnly); boolean canWrite = checkDBWrite(); HarvestManager hm = gc.getBean(HarvestManager.class); if (readOnly && canWrite) { logger.warning("GeoNetwork can write to the database, switching to read-write mode"); readOnly = false; gc.setReadOnly(readOnly); hm.setReadOnly(readOnly); } else if (!readOnly && !canWrite) { logger.warning("GeoNetwork can not write to the database, switching to read-only mode"); readOnly = true; gc.setReadOnly(readOnly); hm.setReadOnly(readOnly); } else { if (readOnly) { logger.info("GeoNetwork remains in read-only mode"); } else { logger.debug("GeoNetwork remains in read-write mode"); } } } catch (Throwable x) { // any uncaught exception would cause the scheduled execution to silently stop logger.error("DBHeartBeat error: " + x.getMessage() + " This error is ignored."); x.printStackTrace(); } } private boolean checkDBWrite() { SettingRepository settingsRepo = gc.getBean(SettingRepository.class); try { Setting newSetting = settingsRepo.save(new Setting().setName("DBHeartBeat").setValue("value")); settingsRepo.flush(); settingsRepo.delete(newSetting); return true; } catch (Exception x) { logger.info("DBHeartBeat Exception: " + x.getMessage()); return false; } } }; scheduledExecutorService.scheduleWithFixedDelay(DBHeartBeat, initialDelay, fixedDelay, TimeUnit.SECONDS); }
From source file:org.wso2.carbon.registry.extensions.internal.RegistrySCMServiceComponent.java
private void registerConnections(RegistryService registryService) throws RegistryException { String configPath = CarbonUtils.getRegistryXMLPath(); if (configPath != null) { File registryXML = new File(configPath); if (registryXML.exists()) { try { CurrentSession.setCallerTenantId(MultitenantConstants.SUPER_TENANT_ID); FileInputStream fileInputStream = new FileInputStream(registryXML); StAXOMBuilder builder = new StAXOMBuilder( CarbonUtils.replaceSystemVariablesInXml(fileInputStream)); OMElement configElement = builder.getDocumentElement(); SecretResolver secretResolver = SecretResolverFactory.create(configElement, false); OMElement scm = configElement.getFirstChildWithName(new QName("scm")); if (scm != null) { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10); Iterator connections = scm.getChildrenWithName(new QName("connection")); while (connections.hasNext()) { OMElement connection = (OMElement) connections.next(); String checkOutURL = connection.getAttributeValue(new QName("checkOutURL")); // Read-Only by default, and can be disabled if needed by setting // Read-Only to false. boolean readOnly = !Boolean.toString(false) .equalsIgnoreCase(connection.getAttributeValue(new QName("readOnly"))); String checkInURL = connection.getAttributeValue(new QName("checkInURL")); String workingDir = connection.getAttributeValue(new QName("workingDir")); String mountPoint = connection.getAttributeValue(new QName("mountPoint")); String username = connection.getFirstChildWithName(new QName("username")).getText(); String password = connection.getFirstChildWithName(new QName("password")).getText(); int updateFrequency = DEFAULT_UPDATE_FREQUENCY; try { updateFrequency = Integer .parseInt(connection.getAttributeValue(new QName("updateFrequency"))); } catch (NumberFormatException ignore) { }//from ww w. j av a 2 s . c om UserRegistry registry = registryService .getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME); File directory = new File(workingDir); if (!directory.exists() && !directory.isDirectory()) { log.error("A valid directory was not found in path: " + workingDir); continue; } String filePath = directory.getAbsolutePath(); if (!registry.resourceExists(mountPoint)) { Collection collection = registry.newCollection(); collection.setProperty(RegistryConstants.REGISTRY_NON_RECURSIVE, "true"); registry.put(mountPoint, collection); } loadRegistryResources(registry, directory, filePath, mountPoint); ExternalContentHandler externalContentHandler = new ExternalContentHandler(); externalContentHandler.setFilePath(filePath); externalContentHandler.setMountPath(mountPoint); URLMatcher urlMatcher = new URLMatcher(); urlMatcher .setPattern(Pattern.quote(mountPoint) + "($|" + RegistryConstants.PATH_SEPARATOR + ".*|" + RegistryConstants.URL_SEPARATOR + ".*)"); RegistryContext registryContext = registry.getRegistryContext(); registryContext.registerNoCachePath(mountPoint); registryContext.getHandlerManager().addHandler(null, urlMatcher, externalContentHandler, HandlerLifecycleManager.TENANT_SPECIFIC_SYSTEM_HANDLER_PHASE); executorService .scheduleWithFixedDelay( new SCMUpdateTask(directory, checkOutURL, checkInURL, readOnly, externalContentHandler, username, CommonUtil .getResolvedPassword(secretResolver, "scm", password)), 0, updateFrequency, TimeUnit.MINUTES); } } } catch (XMLStreamException e) { log.error("Unable to parse registry.xml", e); } catch (IOException e) { log.error("Unable to read registry.xml", e); } catch (CarbonException e) { log.error("An error occurred during system variable replacement", e); } finally { CurrentSession.removeCallerTenantId(); } } } }
From source file:org.fao.geonet.Geonetwork.java
/** * Sets up a periodic check whether GeoNetwork can successfully write to the database. If it can't, GeoNetwork will * automatically switch to read-only mode. */// www .j a v a 2 s . c om private void createDBHeartBeat(final ResourceManager rm, final GeonetContext gc, Integer initialDelay, Integer fixedDelay) { logger.info("creating DB heartbeat with initial delay of " + initialDelay + " s and fixed delay of " + fixedDelay + " s"); ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); Runnable DBHeartBeat = new Runnable() { private static final String INSERT = "INSERT INTO Settings(id, parentId, name, value) VALUES(?, ?, ?, ?)"; private static final String REMOVE = "DELETE FROM Settings WHERE id=?"; /** * */ @Override public void run() { try { boolean readOnly = gc.isReadOnly(); logger.debug("DBHeartBeat: GN is read-only ? " + readOnly); boolean canWrite = checkDBWrite(); HarvestManager hm = gc.getHarvestManager(); if (readOnly && canWrite) { logger.warning("GeoNetwork can write to the database, switching to read-write mode"); readOnly = false; gc.setReadOnly(readOnly); hm.setReadOnly(readOnly); } else if (!readOnly && !canWrite) { logger.warning("GeoNetwork can not write to the database, switching to read-only mode"); readOnly = true; gc.setReadOnly(readOnly); hm.setReadOnly(readOnly); } else { if (readOnly) { logger.info("GeoNetwork remains in read-only mode"); } else { logger.debug("GeoNetwork remains in read-write mode"); } } } // any uncaught exception would cause the scheduled execution to silently stop catch (Throwable x) { logger.error("DBHeartBeat error: " + x.getMessage() + " This error is ignored."); x.printStackTrace(); } } /** * * @return */ private boolean checkDBWrite() { Dbms dbms = null; try { Integer testId = Integer.valueOf("100000"); dbms = (Dbms) rm.openDirect(Geonet.Res.MAIN_DB); dbms.execute(INSERT, testId, Integer.valueOf("1"), "DBHeartBeat", "Yeah !"); dbms.execute(REMOVE, testId); return true; } catch (Exception x) { logger.info("DBHeartBeat Exception: " + x.getMessage()); return false; } finally { try { if (dbms != null) rm.close(Geonet.Res.MAIN_DB, dbms); } catch (Exception x) { logger.error("DBHeartBeat failed to close DB connection. Your system is unstable! Error: " + x.getMessage()); x.printStackTrace(); } } } }; scheduledExecutorService.scheduleWithFixedDelay(DBHeartBeat, initialDelay, fixedDelay, TimeUnit.SECONDS); }