Spring-Retry

Retry a method based on result (instead of exception)

丶灬走出姿态 提交于 2020-07-06 11:04:47
问题 I have a method with the following signature: public Optional<String> doSomething() { ... } If I get an empty Optional I'd like to retry this method and only after 3 times return the empty Optional . I've looked and found the Retryable spring annotation, but it seems to only work on Exceptions. If possible I'd like to use a library for this, and avoid: Creating and throwing an exception. Writing the logic myself. 回答1: I have been using failsafe build in retry. You can retry based on

Springboot @retryable not retrying

匆匆过客 提交于 2020-06-10 09:33:52
问题 The following code is not retrying. What am I missing? @EnableRetry @SpringBootApplication public class App implements CommandLineRunner { ......... ......... @Retryable() ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception { System.out.println("try!"); throw new Exception(); //return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class); } I have added the following to the pom.xml. <dependency

Springboot @retryable not retrying

拈花ヽ惹草 提交于 2020-06-10 09:32:32
问题 The following code is not retrying. What am I missing? @EnableRetry @SpringBootApplication public class App implements CommandLineRunner { ......... ......... @Retryable() ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception { System.out.println("try!"); throw new Exception(); //return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class); } I have added the following to the pom.xml. <dependency

Spring retry without spring application

喜欢而已 提交于 2020-06-09 06:38:42
问题 I have a java application, which starts from the main class (not Spring boot application). And I want to use Spring retry to retry when a connection is lost. As I know I need to add @EnableRetry annotation above main class in Spring application and then use @Retryable above my method with retrying. But I think it will not work in the non-spring application. Is it possible to use spring retry in simple java application (not spring app)? 回答1: I found that I can use RetryTemplate: RetryTemplate

Spring's @Retryable not working when running JUnit Test

女生的网名这么多〃 提交于 2020-05-29 03:24:49
问题 I have this test: @RunWith(MockitoJUnitRunner.class) public class myServiceTest { @InjectMocks myService subject; private myService spy; @Before public void before() { spy = spy(subject); } @Test public void testing() { when(spy.print2()).thenThrow(new RuntimeException()).thenThrow(new RuntimeException()).thenReturn("completed"); spy.print1(); verify(spy, times(3)).print2(); } and then I have: @Service("myService") public class myService extends myAbstractServiceClass { public String print1()

Spring's @Retryable not working when running JUnit Test

霸气de小男生 提交于 2020-05-29 03:23:53
问题 I have this test: @RunWith(MockitoJUnitRunner.class) public class myServiceTest { @InjectMocks myService subject; private myService spy; @Before public void before() { spy = spy(subject); } @Test public void testing() { when(spy.print2()).thenThrow(new RuntimeException()).thenThrow(new RuntimeException()).thenReturn("completed"); spy.print1(); verify(spy, times(3)).print2(); } and then I have: @Service("myService") public class myService extends myAbstractServiceClass { public String print1()

Spring's @Retryable not working when running JUnit Test

笑着哭i 提交于 2020-05-29 03:22:21
问题 I have this test: @RunWith(MockitoJUnitRunner.class) public class myServiceTest { @InjectMocks myService subject; private myService spy; @Before public void before() { spy = spy(subject); } @Test public void testing() { when(spy.print2()).thenThrow(new RuntimeException()).thenThrow(new RuntimeException()).thenReturn("completed"); spy.print1(); verify(spy, times(3)).print2(); } and then I have: @Service("myService") public class myService extends myAbstractServiceClass { public String print1()

Exponential backoff with message order guarantee using spring-kafka

让人想犯罪 __ 提交于 2020-05-24 20:37:51
问题 I'm trying to implement a Spring Boot-based Kafka consumer that has some very strong message delivery guarentees, even in a case of an error. messages from a partition must be processed in order, if message processing fails, the consumption of the particular partition should be suspended, the processing should be retried with a backoff, until it succeeds. Our current implementation fulfills these requirements: @Bean public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer

spring-retry简单demo(附完整代码)

混江龙づ霸主 提交于 2020-05-08 05:54:09
重试 最近项目要用到重试。开始想自己写,后来想用RetryTemplate,最后想到既然项目用了springboot,还是直接集成spring-retry把。 代码 Application package com.danni; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.retry.annotation.EnableRetry; @SpringBootApplication @EnableRetry public class Application { public static void main(String[] args) throws Exception { @SuppressWarnings("resource") ApplicationContext annotationContext = new

springboot retry

筅森魡賤 提交于 2020-05-08 04:54:12
try/catch,while 循环或者定时任务 这样看起来 好 low sping boot retry , 这样代码更简洁 eg:方式一: @Retryable(value = {RemoteAccessException. class },maxAttempts = 3 ,backoff = @Backoff(delay = 5000l ,multiplier = 1 )) public void hahha() throws Exception { System.err.println( " ************************ " ); System. out .println( " do something... " ); throw new RemoteAccessException( " 调用异常 " ); } @Recover public void recover(RemoteAccessException e) { System. out .println(e.getMessage()); } eg:方式二 , 说明 1.上面的参数,在recover,可以拿到,需要那那个参数,写进去就可以了         2. 如果有返回值,那么返回值的类型必须一样,下例中,都是 void @Retryable(value =