net.jodah.lyra.util.Duration Java Examples
The following examples show how to use
net.jodah.lyra.util.Duration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChannelInvocationTest.java From lyra with Apache License 2.0 | 6 votes |
/** * Asserts that invocation failures are rethrown when a connection is shutdown and a retry policy * is not set, but the connection and channel should still be recovered. */ public void shouldThrowOnConnectionShutdownWithNoRetryPolicy() throws Throwable { config = new Config().withRetryPolicy(RetryPolicies.retryNever()).withRecoveryPolicy( RecoveryPolicies.recoverAlways()); performThrowableInvocation(retryableConnectionShutdownSignal()); // Assert that the channel is recovered asynchronously assertTrue(mockChannel(1).channelHandler.circuit.await(Duration.secs(1))); verifyCxnCreations(2); verifyChannelCreations(1, 2); verifyChannelCreations(2, 2); verifyConsumerCreations(1, 1, 2); verifyConsumerCreations(1, 2, 2); verifyConsumerCreations(2, 5, 2); verifyConsumerCreations(2, 6, 2); verifyInvocations(1); }
Example #2
Source File: ChannelInvocationTest.java From lyra with Apache License 2.0 | 6 votes |
/** * Asserts that a failed invocation results in the failure being rethrown immediately if retries * are not configured while recovery takes place in the background. */ public void shouldThrowOnChannelShutdownWithNoRetryPolicy() throws Throwable { config = new Config().withRetryPolicy(RetryPolicies.retryNever()).withRecoveryPolicy( RecoveryPolicies.recoverAlways()); performThrowableInvocation(retryableChannelShutdownSignal()); // Assert that the channel is recovered asynchronously assertTrue(mockChannel(1).channelHandler.circuit.await(Duration.secs(1))); verifyCxnCreations(1); verifyChannelCreations(1, 2); verifyChannelCreations(2, 1); verifyConsumerCreations(1, 1, 2); verifyConsumerCreations(1, 2, 2); verifyConsumerCreations(2, 5, 1); verifyConsumerCreations(2, 6, 1); verifyInvocations(1); }
Example #3
Source File: ConnectionInvocationTest.java From lyra with Apache License 2.0 | 6 votes |
/** * Asserts that a failed invocation results in the failure being rethrown immediately if retries * are not configured while recovery takes place in the background. */ public void shouldThrowImmediatelyOnInvocationFailureWithNoRetryPolicy() throws Throwable { config = new Config().withRetryPolicy(RetryPolicies.retryNever()).withRecoveryPolicy( RecoveryPolicies.recoverAlways()); performThrowableInvocation(retryableConnectionShutdownSignal()); // Assert that the connection is recovered asynchronously assertTrue(connectionHandler.circuit.await(Duration.secs(1))); verifyCxnCreations(2); verifyChannelCreations(1, 2); verifyChannelCreations(2, 2); verifyConsumerCreations(1, 1, 2); verifyConsumerCreations(1, 2, 2); verifyConsumerCreations(2, 5, 2); verifyConsumerCreations(2, 6, 2); verifyInvocations(1); }
Example #4
Source File: InterruptableWaiterTest.java From lyra with Apache License 2.0 | 6 votes |
public void shouldInterruptTimedWaiters() throws Throwable { final InterruptableWaiter iw = new InterruptableWaiter(); final Waiter waiter = new Waiter(); for (int i = 0; i < 3; i++) new Thread(new Runnable() { @Override public void run() { try { iw.await(Duration.mins(1)); } catch (InterruptedException expected) { waiter.resume(); } } }).start(); Thread.sleep(100); iw.interruptWaiters(); waiter.await(500); }
Example #5
Source File: ConnectionUtils.java From simpleci with MIT License | 5 votes |
public static Connection createRabbitmqConnection(String host, int port, String user, String password) throws IOException, TimeoutException { Config config = new Config() .withRecoveryPolicy(new RecoveryPolicy() .withBackoff(Duration.seconds(1), Duration.seconds(30)) .withMaxAttempts(20)); ConnectionOptions options = new ConnectionOptions() .withHost(host) .withPort(port) .withUsername(user) .withPassword(password); return Connections.create(options, config); }
Example #6
Source File: AbstractFunctionalTest.java From lyra with Apache License 2.0 | 5 votes |
protected void mockConnection() throws IOException, TimeoutException { if (connectionFactory == null) { mockConnectionOnly(); connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.getVirtualHost()).thenReturn("/"); when(connectionFactory.newConnection(any(ExecutorService.class), any(Address[].class), anyString())) .thenReturn(connection); } if (options == null) options = new ConnectionOptions().withHost("test-host"); options.withConnectionFactory(connectionFactory); if (config == null) config = new Config().withRetryPolicy( RetryPolicies.retryAlways().withInterval(Duration.millis(10))).withRecoveryPolicy( RecoveryPolicies.recoverAlways()); if (connectionHandler == null) { connectionHandler = new ConnectionHandler(options, config, Connection.class.getClassLoader()); connectionProxy = (ConfigurableConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(), new Class<?>[] {ConfigurableConnection.class}, connectionHandler); connectionHandler.createConnection(connectionProxy); channels = new HashMap<Integer, MockChannel>(); } }
Example #7
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 5 votes |
/** * Sets the max duration to perform attempts for. * * @throws NullPointerException if {@code maxDuration} is null */ @SuppressWarnings("unchecked") public T withMaxDuration(Duration maxDuration) { Assert.notNull(maxDuration, "maxDuration"); this.maxDuration = maxDuration; return (T) this; }
Example #8
Source File: ChannelRecoveryTest.java From lyra with Apache License 2.0 | 5 votes |
/** * Asserts that a failure from a channel listener during recovery results in the channel being * recovered. */ public void shouldHandleRecoveryFailureFromChannelListener() throws Throwable { final AtomicBoolean shutdownCalled = new AtomicBoolean(); config = new Config().withRetryPolicy( RetryPolicies.retryAlways().withInterval(Duration.millis(10))) .withRecoveryPolicy(RecoveryPolicies.recoverAlways()) .withChannelListeners(new DefaultChannelListener() { @Override public void onRecovery(Channel channel) { if (!shutdownCalled.get() && channel == mockChannel(2).proxy) { ShutdownSignalException e = nonRetryableChannelShutdownSignal(); shutdownCalled.set(true); callShutdownListener(mockChannel(2).channelHandler, e); throw e; } } }); performRecovery(mockChannel(2).channelHandler, mockChannel(2).channelHandler, 0, 0); verifyCxnCreations(1); verifyChannelCreations(1, 1); verifyConsumerCreations(1, 1, 1); verifyConsumerCreations(1, 2, 1); verifyChannelCreations(2, 3); verifyConsumerCreations(2, 5, 2); // Only attempted once since first attempt fails verifyConsumerCreations(2, 6, 2); }
Example #9
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 5 votes |
/** * Sets the {@code interval} to pause for between attempts, exponentially backing of to the * {@code maxInterval} multiplying successive intervals by the {@code intervalMultiplier}. * * @throws NullPointerException if {@code interval} or {@code maxInterval} are null * @throws IllegalArgumentException if {@code interval} is <= 0, {@code interval} is >= * {@code maxInterval} or the {@code intervalMultiplier} is <= 1 */ @SuppressWarnings("unchecked") public T withBackoff(Duration interval, Duration maxInterval, int intervalMultiplier) { Assert.notNull(interval, "interval"); Assert.notNull(maxInterval, "maxInterval"); Assert.isTrue(interval.length > 0, "The interval must be greater than 0"); Assert.isTrue(interval.toNanoseconds() < maxInterval.toNanoseconds(), "The interval must be less than the maxInterval"); Assert.isTrue(intervalMultiplier > 1, "The intervalMultiplier must be greater than 1"); this.interval = interval; this.maxInterval = maxInterval; this.intervalMultiplier = intervalMultiplier; return (T) this; }
Example #10
Source File: ReentrantCircuitTest.java From lyra with Apache License 2.0 | 5 votes |
public void shouldReturnWhenAwaitAndAlreadyClosed() throws Throwable { long t = System.currentTimeMillis(); circuit.await(); circuit.await(Duration.mins(3)); // Awaits should return immediately assertTrue(System.currentTimeMillis() - t < 500); }
Example #11
Source File: RecurringStatsTest.java From lyra with Apache License 2.0 | 5 votes |
public void shouldNotAttemptWhenMaxDurationExceeded() throws Exception { RecurringStats stats = new RecurringStats(new RetryPolicy().withMaxDuration(Duration.millis(25))); stats.incrementTime(); assertFalse(stats.isPolicyExceeded()); assertFalse(stats.isPolicyExceeded()); Thread.sleep(50); assertTrue(stats.isPolicyExceeded()); }
Example #12
Source File: RecurringStatsTest.java From lyra with Apache License 2.0 | 5 votes |
public void shouldAdjustWaitTimeForMaxDuration() throws Throwable { RecurringStats stats = new RecurringStats(new RetryPolicy().withInterval(Duration.millis(150)) .withMaxDuration(Duration.millis(100))); stats.incrementTime(); assertFalse(stats.isPolicyExceeded()); assertEquals(stats.getWaitTime().toMillis(), 100); Thread.sleep(25); stats.incrementTime(); assertFalse(stats.isPolicyExceeded()); assertTrue(stats.getWaitTime().toMillis() < 100); }
Example #13
Source File: RecurringStatsTest.java From lyra with Apache License 2.0 | 5 votes |
public void waitTimeShouldIncreaseExponentially() throws Exception { RecurringStats stats = new RecurringStats(new RetryPolicy().withBackoff(Duration.millis(1), Duration.millis(5))); stats.incrementTime(); assertEquals(stats.getWaitTime().toMillis(), 1); stats.incrementTime(); assertEquals(stats.getWaitTime().toMillis(), 2); stats.incrementTime(); assertEquals(stats.getWaitTime().toMillis(), 4); stats.incrementTime(); assertEquals(stats.getWaitTime().toMillis(), 5); stats.incrementTime(); assertEquals(stats.getWaitTime().toMillis(), 5); }
Example #14
Source File: InterruptableWaiterTest.java From lyra with Apache License 2.0 | 4 votes |
public void timedWaiterShouldTimeoutQuietly() throws Throwable { final InterruptableWaiter iw = new InterruptableWaiter(); iw.await(Duration.millis(100)); }
Example #15
Source File: RecurringStatsTest.java From lyra with Apache License 2.0 | 4 votes |
public void waitTimeShouldBeConstant() { RecurringStats stats = new RecurringStats(new RetryPolicy().withInterval(Duration.millis(5))); assertEquals(stats.getWaitTime().toMillis(), 5); stats.incrementAttempts(); assertEquals(stats.getWaitTime().toMillis(), 5); }
Example #16
Source File: RecurringPolicyTest.java From lyra with Apache License 2.0 | 4 votes |
public void shouldAllowDifferentUnitsForIntervalDurations() throws Throwable { RecurringPolicy recurringPolicy = new RecurringPolicy(){}; recurringPolicy.withBackoff(Duration.millis(100), Duration.days(1)); }
Example #17
Source File: RabbitMQMessagingService.java From elasticactors with Apache License 2.0 | 4 votes |
@PostConstruct public void start() throws IOException, TimeoutException { // millis connectionFactory.setConnectionTimeout(1000); // seconds connectionFactory.setRequestedHeartbeat(4); // turn off recovery as we are using Lyra connectionFactory.setAutomaticRecoveryEnabled(false); connectionFactory.setTopologyRecoveryEnabled(false); // lyra reconnect logic Config config = new Config() .withRecoveryPolicy(new RecoveryPolicy() .withMaxAttempts(-1) .withInterval(Duration.seconds(1))) .withChannelListeners(this); ConnectionOptions connectionOptions = new ConnectionOptions(connectionFactory) .withHosts(StringUtils.commaDelimitedListToStringArray(rabbitmqHosts)) .withPort(rabbitmqPort) .withUsername(username) .withPassword(password); // create single connection //clientConnection = connectionFactory.newConnection(Address.parseAddresses(rabbitmqHosts)); clientConnection = Connections.create(connectionOptions,config); // create a seperate consumer channel consumerChannel = clientConnection.createChannel(); consumerChannel.basicQos(prefetchCount); // prepare the consumer channels for (int i = 0; i < queueExecutor.getThreadCount(); i++) { producerChannels.add(clientConnection.createChannel()); } // add logging shutdown listener consumerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE); for (Channel producerChannel : producerChannels) { producerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE); } // ensure the exchange is there consumerChannel.exchangeDeclare(exchangeName,"direct",true); if(ackType == BUFFERED) { messageAcker = new BufferingMessageAcker(consumerChannel); } else if(ackType == WRITE_BEHIND) { messageAcker = new WriteBehindMessageAcker(consumerChannel); } else if(ackType == ASYNC) { messageAcker = new AsyncMessageAcker(consumerChannel); } else { messageAcker = new DirectMessageAcker(consumerChannel); } messageAcker.start(); }
Example #18
Source File: RabbitMQMessagingService.java From elasticactors with Apache License 2.0 | 4 votes |
@PostConstruct public void start() throws IOException, TimeoutException { // millis connectionFactory.setConnectionTimeout(1000); // seconds connectionFactory.setRequestedHeartbeat(4); // turn off recovery as we are using Lyra connectionFactory.setAutomaticRecoveryEnabled(false); connectionFactory.setTopologyRecoveryEnabled(false); // lyra reconnect logic Config config = new Config() .withRecoveryPolicy(new RecoveryPolicy() .withMaxAttempts(-1) .withInterval(Duration.seconds(1))) .withChannelListeners(this); ConnectionOptions connectionOptions = new ConnectionOptions(connectionFactory) .withHosts(StringUtils.commaDelimitedListToStringArray(rabbitmqHosts)) .withPort(rabbitmqPort) .withUsername(username) .withPassword(password); // create single connection //clientConnection = connectionFactory.newConnection(Address.parseAddresses(rabbitmqHosts)); clientConnection = Connections.create(connectionOptions,config); // create a seperate producer and a seperate consumer channel consumerChannel = clientConnection.createChannel(); consumerChannel.basicQos(prefetchCount); producerChannel = clientConnection.createChannel(); // add logging shutdown listener consumerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE); producerChannel.addShutdownListener(LoggingShutdownListener.INSTANCE); // ensure the exchange is there consumerChannel.exchangeDeclare(exchangeName,"direct",true); if(ackType == BUFFERED) { messageAcker = new BufferingMessageAcker(consumerChannel); } else if(ackType == WRITE_BEHIND) { messageAcker = new WriteBehindMessageAcker(consumerChannel); } else if(ackType == ASYNC) { messageAcker = new AsyncMessageAcker(consumerChannel); } else { messageAcker = new DirectMessageAcker(consumerChannel); } messageAcker.start(); }
Example #19
Source File: RetryableResource.java From lyra with Apache License 2.0 | 4 votes |
/** * Calls the {@code callable} with retries, throwing a failure if retries are exhausted. */ <T> T callWithRetries(Callable<T> callable, RecurringPolicy<?> recurringPolicy, RecurringStats retryStats, Set<Class<? extends Exception>> retryableExceptions, boolean recoverable, boolean logFailures) throws Exception { boolean recovery = retryStats != null; while (true) { try { return callable.call(); } catch (Exception e) { ShutdownSignalException sse = extractCause(e, ShutdownSignalException.class); if (sse == null && logFailures && recurringPolicy != null && recurringPolicy.allowsAttempts()) log.error("Invocation of {} failed.", callable, e); if (sse != null && (recovery || !recoverable)) throw e; if (!closed) { try { // Retry on channel recovery failure or retryable exception boolean retryable = recurringPolicy != null && recurringPolicy.allowsAttempts() && isRetryable(retryableExceptions, e, sse); long startTime = System.nanoTime(); if (retryable) { if (retryStats == null) retryStats = new RecurringStats(recurringPolicy); // Wait for pending recovery if (sse != null) { if (recurringPolicy.getMaxDuration() == null) circuit.await(); else if (!circuit.await(retryStats.getMaxWaitTime())) { log.debug("Exceeded max wait time while waiting for {} to recover", this); throw e; } } // Continue retries retryStats.incrementAttempts(); if (!retryStats.isPolicyExceeded()) { long remainingWaitTime = retryStats.getWaitTime().toNanos() - (System.nanoTime() - startTime); if (remainingWaitTime > 0) retryWaiter.await(Duration.nanos(remainingWaitTime)); continue; } } } catch (Throwable ignore) { } } throw e; } } }
Example #20
Source File: InterruptableWaiter.java From lyra with Apache License 2.0 | 4 votes |
/** * Waits for the {@code waitDuration}, aborting if interrupted. */ public void await(Duration waitDuration) throws InterruptedException { sync.tryAcquireSharedNanos(0, waitDuration.toNanos()); }
Example #21
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 3 votes |
/** * Sets the {@code interval} to pause for between attempts. * * @throws NullPointerException if {@code interval} is null * @throws IllegalStateException if backoff intervals have already been set via * {@link #withBackoff(Duration, Duration)} or * {@link #withBackoff(Duration, Duration, int)} */ @SuppressWarnings("unchecked") public T withInterval(Duration interval) { Assert.notNull(interval, "interval"); Assert.state(maxInterval == null, "Backoff intervals have already been set"); this.interval = interval; return (T) this; }
Example #22
Source File: RecoveryPolicy.java From lyra with Apache License 2.0 | 2 votes |
/** * Sets the max duration to perform attempts for. * * @throws NullPointerException if {@code maxDuration} is null */ @Override public RecoveryPolicy withMaxDuration(Duration maxDuration) { return super.withMaxDuration(maxDuration); }
Example #23
Source File: ConnectionOptions.java From lyra with Apache License 2.0 | 2 votes |
/** * Set the connection timeout, zero for infinite, for an individual connection attempt. * * @throws NullPointerException if {@code connectionTimeout} is null */ public ConnectionOptions withConnectionTimeout(Duration connectionTimeout) { factory.setConnectionTimeout((int) connectionTimeout.toMillis()); return this; }
Example #24
Source File: ConnectionOptions.java From lyra with Apache License 2.0 | 2 votes |
/** * Set the requested heartbeat, zero for none. * * @throws NullPointerException if {@code requestedHeartbeat} is null */ public ConnectionOptions withRequestedHeartbeat(Duration requestedHeartbeat) { factory.setRequestedHeartbeat((int) requestedHeartbeat.toSeconds()); return this; }
Example #25
Source File: RecurringStats.java From lyra with Apache License 2.0 | 2 votes |
/** * Returns the max amount of time that an external caller should wait before the retry policy is * exceeded. The max wait time is calculated each time {@link #incrementAttempts()} or * {@link #incrementTime()} is called. */ public Duration getMaxWaitTime() { return maxWaitTime == -1 ? Duration.inf() : Duration.nanos(maxWaitTime); }
Example #26
Source File: RecurringStats.java From lyra with Apache License 2.0 | 2 votes |
/** * Returns the amount of time that an external caller should wait, based on the retry policy, * before performing a retry. The wait time is calculated each time {@link #incrementAttempts()} * or {@link #incrementTime()} is called. */ public Duration getWaitTime() { return Duration.nanos(waitTime); }
Example #27
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 2 votes |
/** * Returns the interval between attempts. * * @see #withInterval(Duration) * @see #withBackoff(Duration, Duration) * @see #withBackoff(Duration, Duration, int) */ public Duration getInterval() { return interval; }
Example #28
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 2 votes |
/** * Returns the max duration to perform attempts for. * * @see #withMaxDuration(Duration) */ public Duration getMaxDuration() { return maxDuration; }
Example #29
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 2 votes |
/** * Returns the max interval between backoff attempts. * * @see #withBackoff(Duration, Duration) */ public Duration getMaxInterval() { return maxInterval; }
Example #30
Source File: RecurringPolicy.java From lyra with Apache License 2.0 | 2 votes |
/** * Sets the {@code interval} to pause for between attempts, exponentially backing of to the * {@code maxInterval} multiplying successive intervals by a factor of 2. * * @throws NullPointerException if {@code interval} or {@code maxInterval} are null * @throws IllegalArgumentException if {@code interval} is <= 0 or {@code interval} is >= * {@code maxInterval} */ public T withBackoff(Duration interval, Duration maxInterval) { return withBackoff(interval, maxInterval, 2); }