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 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 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; } }
/** * 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 }
/** * 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 ); }
if (MPeriod.isTimeOut(lastMailTransport, CFG_CONNECTION_TIMEOUT.value()) || !transport.isConnected()) { reset();
protected void doTick() { synchronized (jobs) { if (MPeriod.isTimeOut(lastQueueCheck, queueCheckTimeout)) doQueueCheck();
@Override public void doBeat() { if (isClosed()) return; log().d("beat",dest); try { Session session = getSession(); if ( session instanceof ActiveMQSession && ((ActiveMQSession)getSession()).isClosed() ) { log().i("reconnect because session is closed",getName()); consumer = null; } open(); // try to reopen and re-listen if (inactivityTimeout.value() > 0 && MPeriod.isTimeOut(lastActivity, inactivityTimeout.value() )) reset(); } catch (JMSException e) { log().d(e); } }