spring-projects / spring-retry

2.15k stars 516 forks source link

Using @Retryable in @Service at method level not working with @Scheduled method in another class #243

Open amolkumar18 opened 3 years ago

amolkumar18 commented 3 years ago

Using @Retryable in @Service at method level which is going to retry 3 times by default if get any exception and @Recover responsible to sa error count in postgres database table. I have seperate class for @EnableScheduling with scheduled cron run every four hours which will going to process these failed cases by querying this postgres table. When aplication starts and failed for the service class within the method annotated with @Retryable able to save record in db but when trying through scheduler it's not updating the failure count in database

Originally posted by @amolkumar18 in https://github.com/spring-projects/spring-retry/issues/94#issuecomment-852403581

garyrussell commented 3 years ago

You need to show code, configuration and version information or, preferably, a small, complete, example that exhibits the behavior.

amolkumar18 commented 3 years ago

Ok. Give me sometime, will get back to you on this with artifact's n sample code,

amolkumar18 commented 3 years ago
@Service
public class MigrationStepOne {

@Autowired 
private Service1 Service1;

@Retryable(backoff = @Backoff(5000), value = {Exception.class})
public String stepOneProcessing(CustomerMigrationStep cms){
try{
String str = "Hello" + 4/0;
return "successful";
}catch(Exception e){
throw e;
}
}

@Recover
public String recover(Exception e, CustomerMigrationStep cms){
CustomerMigrationStep cmss = null;
cmss.setFailureCount(cms.getFailedCount() + 3);
cms = cmss;
service1.saveFailedCount(cms);
}
}
@Component
@EnableScheduling
public class Retry scheduler {

@Autowired
private CustomerMigrationStepRepo repo;

@scheduled(cron = "0 0 4,8 0 0 0)
public void retryingMethod(){
List<CustomerMigrationStep> list = repo.findAllFailedCountCases();
for ( CustomerMigrationStep step: list){
CallClassXyz method which internally calls the method retryingMethod() of service class MigrationStepOne
}
}
}
amolkumar18 commented 3 years ago
org.springframework.boot spring-boot-starter-parent 2.5.0 pom org.springframework.retry spring-retry 1.3.1
amolkumar18 commented 3 years ago

public class Service1{

@Autowired private CustomerMigrationStepRepo repo;

public void saveFailedCount(CustomerMigrationStep cms){ repo.saveAndFlush(cms); } }

amolkumar18 commented 3 years ago

Table entry CustId. failureCount 1 3 Ideally after scheduler call it should update to 6 but it's not happening. Count remains 3 only.

amolkumar18 commented 3 years ago

Do we expecting any other information ??

garyrussell commented 3 years ago

Please learn how to use GItHub markdown to format code.

This code makes no sense to me

CustomerMigrationStep cmss = null;
cmss.setFailureCount(cms.getFailedCount() + 3);

The second line will throw a NullPointerException.

kumarvinothjoseph commented 2 years ago

Spring Boot Starter - 2.1.7.RELEASE, Spring-aop - 5.1.9.RELEASE, spring-retry -1.2.4.RELEASE,

@EnableRetry in SpringBootApplication class

My Business Logic class is annotated with @Component And one of the method annotated with @Scheduled(cron="") & @Retryable(value = Exception.class, maxAttempts = 3, backoff= @Backoff(delay = 1000))

Here Scheduling is working But Retryable is not working. Could you please help on this ?

samirm commented 1 year ago

same issue here

artembilan commented 1 year ago

@samirm , any chances you can share with us a code which may reproduce the issue? The simple Spring Boot project to play with would be great. Thanks

quaff commented 1 month ago

It's likely due to wrong order of AOP advices, see https://github.com/spring-projects/spring-retry/issues/22.

artembilan commented 1 month ago

Well, according to the issue description this is not the case. Looks like those @Retryable and @Scheduled are in different classes. Therefore there is no AOP advices ordering issue since they are applied to different classes.