private class Container<Z> extends SoftReference<Z> { long time = System.currentTimeMillis(); long accessed = 0; public Container(Z value) { super(value); } public Z getValue() { if (isRefreshOnAccess()) time = System.currentTimeMillis(); accessed++; return super.get(); } boolean isTimeout() { return MPeriod.isTimeOut(time, timeout) || super.get() == null; } }
public long getAllWeeks() { return getAllMilliseconds() / 1000 / 60 / 60 / 24 / 7; }
public Date join(Date _date) { Calendar cal = Calendar.getInstance(); cal.setTime(_date); join(cal); return cal.getTime(); }
public String getCurrentTimeAsString() { return MPeriod.getIntervalAsString(getCurrentTime()); }
/** * Returns the default interval set by definition or 15 minutes as fall back. * * @return Default configured interval */ public long getDefaultInterval() { String intervalStr = getClass().getAnnotation(ActivityDescription.class).event(); return MPeriod.toMilliseconds(intervalStr, 60000 * 15); }
add(HOUR,Integer.parseInt(MString.beforeIndex(_interval, ':'))); _interval = MString.afterIndex(_interval, ':'); if (_interval.indexOf(":") < 0) { add(MINUTE,Integer.parseInt(_interval)); } else { add(MINUTE,Integer.parseInt(MString.beforeIndex(_interval, ':'))); add(SECOND,Integer.parseInt(MString.afterIndex(_interval, ':'))); optimize(); return; add(WEEK, Integer.parseInt(buffer.toString())); buffer.setLength(0); break; case 'd': add(DAY, Integer.parseInt(buffer.toString())); buffer.setLength(0); break; case 'h': add(HOUR, Integer.parseInt(buffer.toString())); buffer.setLength(0); break; case 'M': add(MINUTE, Integer.parseInt(buffer.toString())); buffer.setLength(0); break; case 's': add(SECOND, Integer.parseInt(buffer.toString())); buffer.setLength(0);
public MPeriod(long millisec) { this.millisec = millisec; optimize(); }
public MPeriod(String _interval) throws NumberFormatException { parse(_interval); }
@Override public String toString() { Entry<String, Long> next = getNextScheduled(); String time = "now"; if (state == STATE_NODE.CLOSED || state == STATE_NODE.FAILED || state == STATE_NODE.STOPPED || state == STATE_NODE.SUSPENDED) { time = "-"; } else if (next != null) time = next.getKey() + "@" + MPeriod.getIntervalAsString((next.getValue() - System.currentTimeMillis() )); return "PNode: " + getCanonicalName() + " " + state + " " + time + " " + id; }
@Override public InternalDbConnection createConnection() throws Exception { // ResourceNode concon = config.getNode("connection"); String driver = config.getExtracted("driver"); String url = config.getExtracted("url"); String user = config.getExtracted("user"); String pass = config.getExtracted("pass"); if (!MString.isEmpty(driver)) { if (activator != null) activator.getClazz(driver); else Class.forName(driver); } log().t(driver,url,user); Connection con = DriverManager.getConnection(url,user,MPassword.decode(pass)); getDialect().prepareConnection(con); JdbcConnection dbCon = new JdbcConnection(this,con); long timeoutUnused = MPeriod.toMilliseconds( config.getExtracted("timeout_unused"), 0 ); long timeoutLifetime = MPeriod.toMilliseconds( config.getExtracted("timeout_lifetime"), 0 ); if (timeoutUnused > 0) dbCon.setTimeoutUnused(timeoutUnused); if (timeoutLifetime > 0) dbCon.setTimeoutLifetime(timeoutLifetime); return dbCon; }
public Change[] clearEventQueue() { // It's an smart queue .... act like it synchronized (queue) { // special logic for big changes - do not update all every second, wait for the end of the big change if (queue.size() == 1 && queue.get(0).event == EVENT.BIG_CHANGE) { if (firstBigChange == 0) { firstBigChange = System.currentTimeMillis(); } else if (MPeriod.isTimeOut(firstBigChange, maxBigChangeWait)) { firstBigChange = 0; Change[] out = queue.toArray(new Change[queue.size()]); queue.clear(); return out; } } else if (firstBigChange != 0) { firstBigChange = 0; } Change[] out = queue.toArray(new Change[queue.size()]); queue.clear(); return out; } }
@Override public void doAnalyze(long connectionId, String original, String query, long delta, Throwable t) { log().i(connectionId,MPeriod.getIntervalAsString(delta),original,query); }
public long getAverageMonths() { return getAllMilliseconds() / MONTH_AVERAGE_MILLISECONDS; }
public synchronized void doValidationCheck() { if (MPeriod.isTimeOut(lastCheck, checkTimeout)) { map.entrySet().removeIf(e -> { return (invalidator != null && invalidator.isInvalid(e.getKey(), e.getValue().get(), e.getValue().time, e.getValue().accessed ) || e.getValue().isTimeout()); }); lastCheck = System.currentTimeMillis(); } }
public long getAllSecounds() { return getAllMilliseconds() / 1000; }
/** * Returns the current time cached 30 minutes. The time will be interpolated with the current time * @return actual time millies in UTC */ public synchronized static long getCurrentTime() { if (MPeriod.isTimeOut(lastUpdate, TIMEOUT_RELOAD)) { for (int i = 0; i < TIME_SERVERS.size(); i++) { try { now = getAtomicTime(TIME_SERVERS.get(currentServer) ); lastUpdate = System.currentTimeMillis(); return now; } catch (Throwable t) { log.i(t); } currentServer = (currentServer + 1) % TIME_SERVERS.size(); } } return now + (System.currentTimeMillis() - lastUpdate); // interpolate with last update }
public long getAllHours() { return getAllMilliseconds() / 1000 / 60 / 60; }
/** * Generating RSA keys needs a lot of resources (ca 100ms per key). Therefore you can * use a keypool. The keypool will regularly renew the keys. * @return A key from the pool */ public synchronized static KeyPair getRsaKeyFromPool() { if (MPeriod.isTimeOut(keyPoolUpdate, CFG_POOL_UPDATE_TIME.value())) { if (keyPool.size() > 0) keyPool.removeFirst(); keyPoolUpdate = System.currentTimeMillis(); } if (keyPool.size() < CFG_POOL_SIZE.value()) { try { KeyPair key = generateRsaKey(RSA_KEY_SIZE_DEFAULT); keyPool.add(key); return key; } catch (NoSuchAlgorithmException | NoSuchProviderException e) { throw new RuntimeException(e); } } int pos = (int)(Math.random() * keyPool.size()); // use a simple random function return keyPool.get( pos ); }
public long getAllDays() { return getAllMilliseconds() / 1000 / 60 / 60 / 24; }