public static final int getTTLSeconds(Duration duration) { Preconditions.checkArgument(duration!=null && !duration.isZero(),"Must provide non-zero TTL"); long ttlSeconds = Math.max(1,duration.getSeconds()); assert ttlSeconds>0; Preconditions.checkArgument(ttlSeconds<=Integer.MAX_VALUE, "tll value is too large [%s] - value overflow",duration); return (int)ttlSeconds; }
@Override public void writeObject(ObjectOutput output, Duration duration) throws IOException { output.writeLong(duration.getSeconds()); output.writeInt(duration.getNano()); }
public void start() { this.scheduler.scheduleAtFixedRate(() -> { FlowTriggerExecutionCleaner.this.flowTriggerInstanceLoader .deleteTriggerExecutionsFinishingOlderThan(System .currentTimeMillis() - RETENTION_PERIOD.toMillis()); }, 0, CLEAN_INTERVAL.getSeconds(), TimeUnit.SECONDS); }
@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(); }
public static DurationValue duration( Duration value ) { requireNonNull( value, "Duration" ); return newDuration( 0, 0, value.getSeconds(), value.getNano() ); }
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; }
private Duration nextDurationRaw() { return Duration.ofSeconds( nextLong( DAYS.getDuration().getSeconds() ), nextLong( NANOS_PER_SECOND ) ); }
@Override public void write(WriteBuffer buffer, Duration attribute) { secondsSerializer.write(buffer,attribute.getSeconds()); nanosSerializer.write(buffer,attribute.getNano()); } }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new Cookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
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 void shouldConvertNanosOfDayToUTC() { int nanosOfDayLocal = 42; Duration offsetDuration = Duration.ofMinutes( 35 ); long nanosOfDayUTC = TemporalUtil.nanosOfDayToUTC( nanosOfDayLocal, (int) offsetDuration.getSeconds() ); assertEquals( nanosOfDayLocal - offsetDuration.toNanos(), nanosOfDayUTC ); }
@Test public void testCircuitBreakerOnIgnoredErrorEvent() { CircuitBreakerOnIgnoredErrorEvent circuitBreakerEvent = new CircuitBreakerOnIgnoredErrorEvent("test", Duration.ofSeconds(1), new IOException()); assertThat(circuitBreakerEvent.getCircuitBreakerName()).isEqualTo("test"); assertThat(circuitBreakerEvent.getElapsedDuration().getSeconds()).isEqualTo(1); assertThat(circuitBreakerEvent.getThrowable()).isInstanceOf(IOException.class); assertThat(circuitBreakerEvent.getEventType()).isEqualTo(Type.IGNORED_ERROR); assertThat(circuitBreakerEvent.toString()).contains("CircuitBreaker 'test' recorded an error which has been ignored: 'java.io.IOException'."); }
@Test public void testCircuitBreakerOnErrorEvent() { CircuitBreakerOnErrorEvent circuitBreakerEvent = new CircuitBreakerOnErrorEvent("test", Duration.ofSeconds(1), new IOException()); assertThat(circuitBreakerEvent.getCircuitBreakerName()).isEqualTo("test"); assertThat(circuitBreakerEvent.getElapsedDuration().getSeconds()).isEqualTo(1); assertThat(circuitBreakerEvent.getThrowable()).isInstanceOf(IOException.class); assertThat(circuitBreakerEvent.getEventType()).isEqualTo(Type.ERROR); assertThat(circuitBreakerEvent.toString()).contains("CircuitBreaker 'test' recorded an error: 'java.io.IOException'."); }
@Test public void testCircuitBreakerOnSuccessEvent() { CircuitBreakerOnSuccessEvent circuitBreakerEvent = new CircuitBreakerOnSuccessEvent("test", Duration.ofSeconds(1)); assertThat(circuitBreakerEvent.getCircuitBreakerName()).isEqualTo("test"); assertThat(circuitBreakerEvent.getElapsedDuration().getSeconds()).isEqualTo(1); assertThat(circuitBreakerEvent.getEventType()).isEqualTo(Type.SUCCESS); assertThat(circuitBreakerEvent.toString()).contains("CircuitBreaker 'test' recorded a successful call."); }
@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()); } }
@Test() public void shouldSetWaitInterval() { CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().waitDurationInOpenState(Duration.ofSeconds(1)).build(); then(circuitBreakerConfig.getWaitDurationInOpenState().getSeconds()).isEqualTo(1); }
@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"))); }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new CookieImpl(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.exchange.getResponseCookies().putIfAbsent(name, cookie); } } }
@Test() public void shouldSetDefaultSettings() { CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.ofDefaults(); then(circuitBreakerConfig.getFailureRateThreshold()).isEqualTo(CircuitBreakerConfig.DEFAULT_MAX_FAILURE_THRESHOLD); then(circuitBreakerConfig.getRingBufferSizeInHalfOpenState()).isEqualTo(CircuitBreakerConfig.DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE); then(circuitBreakerConfig.getRingBufferSizeInClosedState()).isEqualTo(CircuitBreakerConfig.DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE); then(circuitBreakerConfig.getWaitDurationInOpenState().getSeconds()).isEqualTo(CircuitBreakerConfig.DEFAULT_WAIT_DURATION_IN_OPEN_STATE); then(circuitBreakerConfig.getRecordFailurePredicate()).isNotNull(); }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }