@Test public void testWithCustomNewMessageIdentifier() throws Exception { final CountDownLatch latch = new CountDownLatch(1); StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .maxAttempts(5) .newMessageIdentifier(message -> { latch.countDown(); return false; }) .backOffPolicy(new FixedBackOffPolicy()) .build(); assertEquals(5, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); assertEquals(1000L, TestUtils.getPropertyValue(interceptor, "retryOperations.backOffPolicy.backOffPeriod")); final AtomicInteger count = new AtomicInteger(); Foo delegate = createDelegate(interceptor, count); Message message = MessageBuilder.withBody("".getBytes()).setMessageId("foo").setRedelivered(false).build(); try { delegate.onMessage("", message); } catch (RuntimeException e) { assertEquals("foo", e.getMessage()); } assertEquals(1, count.get()); assertTrue(latch.await(0, TimeUnit.SECONDS)); }
@Test public void testWithCustomKeyGenerator() throws Exception { final CountDownLatch latch = new CountDownLatch(1); StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .messageKeyGenerator(message -> { latch.countDown(); return "foo"; }) .build(); assertEquals(3, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); final AtomicInteger count = new AtomicInteger(); Foo delegate = createDelegate(interceptor, count); Message message = MessageBuilder.withBody("".getBytes()).setRedelivered(false).build(); try { delegate.onMessage("", message); } catch (RuntimeException e) { assertEquals("foo", e.getMessage()); } assertEquals(1, count.get()); assertTrue(latch.await(0, TimeUnit.SECONDS)); }
@Test public void testWitCustomRetryPolicyTraverseCause() { StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .retryPolicy(new SimpleRetryPolicy(15, Collections .<Class<? extends Throwable>, Boolean>singletonMap(Exception.class, true), true)) .build(); assertEquals(15, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); }
@Override public StatefulRetryOperationsInterceptor build() { this.applyCommonSettings(this.factoryBean); if (this.messageKeyGenerator != null) { this.factoryBean.setMessageKeyGenerator(this.messageKeyGenerator); } if (this.newMessageIdentifier != null) { this.factoryBean.setNewMessageIdentifier(this.newMessageIdentifier); } return this.factoryBean.getObject(); }
/** * Create a builder for a stateful retry interceptor. * @return The interceptor builder. */ public static StatefulRetryInterceptorBuilder stateful() { return new StatefulRetryInterceptorBuilder(); }
@Test public void testBasic() { StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful().build(); assertEquals(3, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); }
@Test public void testWithMoreAttempts() { StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .maxAttempts(5) .build(); assertEquals(5, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); }
@Test public void testWithCustomBackOffPolicy() { StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .maxAttempts(5) .backOffPolicy(new FixedBackOffPolicy()) .build(); assertEquals(5, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); assertEquals(1000L, TestUtils.getPropertyValue(interceptor, "retryOperations.backOffPolicy.backOffPeriod")); }
/** * Create a builder for a stateful retry interceptor. * @return The interceptor builder. */ public static StatefulRetryInterceptorBuilder stateful() { return new StatefulRetryInterceptorBuilder(); }
@Test public void testWithCustomizedBackOffMoreAttempts() { StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .maxAttempts(5) .backOffOptions(1, 2, 10) .build(); assertEquals(5, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); assertEquals(1L, TestUtils.getPropertyValue(interceptor, "retryOperations.backOffPolicy.initialInterval")); assertEquals(2.0, TestUtils.getPropertyValue(interceptor, "retryOperations.backOffPolicy.multiplier")); assertEquals(10L, TestUtils.getPropertyValue(interceptor, "retryOperations.backOffPolicy.maxInterval")); }
@Override public StatefulRetryOperationsInterceptor build() { this.applyCommonSettings(this.factoryBean); if (this.messageKeyGenerator != null) { this.factoryBean.setMessageKeyGenerator(this.messageKeyGenerator); } if (this.newMessageIdentifier != null) { this.factoryBean.setNewMessageIdentifier(this.newMessageIdentifier); } return this.factoryBean.getObject(); }
@Test public void testWithCustomRetryTemplate() { RetryOperations retryOperations = new RetryTemplate(); StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful() .retryOperations(retryOperations) .build(); assertEquals(3, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts")); assertSame(retryOperations, TestUtils.getPropertyValue(interceptor, "retryOperations")); }