Refine search
@Override @Nullable public T get(long timeout, TimeUnit unit) { Assert.notNull(unit, "TimeUnit must not be null"); Duration duration = Duration.ofMillis(TimeUnit.MILLISECONDS.convert(timeout, unit)); return this.processor.block(duration); }
public static String elapsedTime(Date d1, Date d2){ try{ Duration period = Duration.between(d1.toInstant(), d2.toInstant()); // Note: this will become easier with Java 9, using toDaysPart() etc. long days = period.toDays(); period = period.minusDays(days); long hours = period.toHours(); period = period.minusHours(hours); long minutes = period.toMinutes(); period = period.minusMinutes(minutes); long seconds = period.getSeconds(); return days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds"; } catch(java.lang.IllegalArgumentException e) { log.warn(e); } return ""; }
@Test public void handleReturnValueLastModified() throws Exception { Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); long timestamp = currentTime.toEpochMilli(); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifModifiedSince(timestamp)); ResponseEntity<String> entity = ok().lastModified(oneMinAgo.toEpochMilli()).body("body"); MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class)); HandlerResult result = handlerResult(entity, returnType); this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, null, oneMinAgo); }
/** * Schedule the given {@link Runnable}, starting as soon as possible and invoking it with * the given delay between the completion of one execution and the start of the next. * <p>Execution will end once the scheduler shuts down or the returned * {@link ScheduledFuture} gets cancelled. * @param task the Runnable to execute whenever the trigger fires * @param delay the delay between the completion of one execution and the start of the next * @return a {@link ScheduledFuture} representing pending completion of the task * @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted * for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress) * @since 5.0 * @see #scheduleWithFixedDelay(Runnable, long) */ default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) { return scheduleWithFixedDelay(task, delay.toMillis()); }
/** * Refresh all the metric value and the bindingMap who notes user's authorization. */ private void refreshMetricsPeriodic() { if (Duration.between(timeForRefreshMetric, Instant.now()).getSeconds() > 5) { metrics.refreshAllMetrics(); timeForRefreshMetric = Instant.now(); if (bindingMap != null) { refreshBindingMap(); } } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getName()).append('=').append(getValue()); if (StringUtils.hasText(getPath())) { sb.append("; Path=").append(getPath()); } if (StringUtils.hasText(this.domain)) { sb.append("; Domain=").append(this.domain); } if (!this.maxAge.isNegative()) { sb.append("; Max-Age=").append(this.maxAge.getSeconds()); sb.append("; Expires="); long millis = this.maxAge.getSeconds() > 0 ? System.currentTimeMillis() + this.maxAge.toMillis() : 0; sb.append(HttpHeaders.formatDate(millis)); } if (this.secure) { sb.append("; Secure"); } if (this.httpOnly) { sb.append("; HttpOnly"); } if (StringUtils.hasText(this.sameSite)) { sb.append("; SameSite=").append(this.sameSite); } return sb.toString(); }
@Override public void serialize(Duration duration, JsonGenerator generator, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { if (useNanoseconds(provider)) { generator.writeNumber(DecimalUtils.toBigDecimal( duration.getSeconds(), duration.getNano() )); } else { generator.writeNumber(duration.toMillis()); } } else { // Does not look like we can make any use of DateTimeFormatter here? generator.writeString(duration.toString()); } }
private static SnapshotDto findNearestSnapshotToTargetDate(List<SnapshotDto> snapshots, Instant targetDate) { // FIXME shouldn't this be the first analysis after targetDate? Duration bestDuration = null; SnapshotDto nearest = null; for (SnapshotDto snapshot : snapshots) { Instant createdAt = Instant.ofEpochMilli(snapshot.getCreatedAt()); Duration duration = Duration.between(targetDate, createdAt).abs(); if (bestDuration == null || duration.compareTo(bestDuration) <= 0) { bestDuration = duration; nearest = snapshot; } } return nearest; }
private Duration nextDurationRaw() { return Duration.ofSeconds( nextLong( DAYS.getDuration().getSeconds() ), nextLong( NANOS_PER_SECOND ) ); }
@Test public void maxAge() { Duration maxAge = Duration.ofDays(365); String expires = HttpHeaders.formatDate(System.currentTimeMillis() + maxAge.toMillis()); expires = expires.substring(0, expires.indexOf(":") + 1); assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge).build().toString(), allOf( startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires), endsWith(" GMT"))); assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge.getSeconds()).build().toString(), allOf( startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires), endsWith(" GMT"))); }
private long getLockWaitTimeoutInSeconds(int timeoutInMilliseconds) { Duration duration = Duration.ofMillis( timeoutInMilliseconds ); long timeoutInSeconds = duration.getSeconds(); if ( duration.getNano() != 0 ) { LOG.info( "Changing the query timeout from " + timeoutInMilliseconds + " ms to " + timeoutInSeconds + " s, because HANA requires the timeout in seconds" ); } return timeoutInSeconds; }
@Test public void shouldSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid() { testSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid( 3, Duration.ofSeconds( 5 ) ); testSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid( 1, Duration.ofSeconds( 11 ) ); testSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid( 22, Duration.ofMinutes( 2 ) ); testSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid( 42, Duration.ofDays( 4 ) ); }
@Test public void shouldSlowRequestRateOnMultipleFailedAttempts() { testSlowRequestRateOnMultipleFailedAttempts( 3, Duration.ofSeconds( 5 ) ); testSlowRequestRateOnMultipleFailedAttempts( 1, Duration.ofSeconds( 10 ) ); testSlowRequestRateOnMultipleFailedAttempts( 6, Duration.ofMinutes( 1 ) ); testSlowRequestRateOnMultipleFailedAttempts( 42, Duration.ofMinutes( 2 ) ); }