0

A very simple HelloWorld Test

@Log4j2
@Service
@EnableRetry
@EnableScheduling
public class MyBeanImpl {
    @Scheduled(cron = "0/2 * *  * * ? ")
    @Retryable(value = {RuntimeException.class}, maxAttempts = 4, backoff = @Backoff(delay = 10000))
    public void sched() {
        log.info("Foo sched a = {}", a++);
        throw new RuntimeException("Foo");
    }    
}


@Recover
public void recover(RuntimeException e) {
//.....
}

// Junit Class is here , For Simple POC test, I dont use Interface class, just use Implementation class
@RunWith(SpringRunner.class)
@SpringBootTest
@Log4j2
@ContextConfiguration
public class MyBeanImplTest {
@Autowired    
MyBeanImpl _myBean;

private String input = "HelloWorld";

@Test
public void sched() {
_myBean.sched();
}

Question :

I set maxAttempts = 1, it runs 1 time. why?

I set maxAttempts = 2, it always run 3 times. why?

I set maxAttempts = 4, however it runs 8 times. why?

I set maxAttempts = 6, it runs 11 times. why?

user1638172
  • 103
  • 2
  • 2
  • 6

1 Answers1

0

Probably because it is scheduled as well as being called by the test case.

Look at the thread name in the INFO log.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • As I am new to Spring Retry and Spring Scheduled. I follow your advice and enable debug level, I find there is another thread. In my case it is called my-scheduled-task-pool, and the pool size is set to **10**. Sorry for my stupid mistake. ```java @Configuration public class SchedulerConfig implements SchedulingConfigurer { private final int POOL_SIZE = 10; //.... threadPoolTaskScheduler.setThreadNamePrefix("my-scheduled-task-pool-"); threadPoolTaskScheduler.initialize(); scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler); } } ``` – user1638172 Mar 13 '20 at 09:57