spring-retry icon indicating copy to clipboard operation
spring-retry copied to clipboard

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

Open amolkumar18 opened this issue 4 years ago • 13 comments
trafficstars

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

amolkumar18 avatar Jun 01 '21 19:06 amolkumar18

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

garyrussell avatar Jun 01 '21 19:06 garyrussell

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

amolkumar18 avatar Jun 01 '21 19:06 amolkumar18

@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 avatar Jun 01 '21 20:06 amolkumar18

org.springframework.boot spring-boot-starter-parent 2.5.0 pom org.springframework.retry spring-retry 1.3.1

amolkumar18 avatar Jun 01 '21 20:06 amolkumar18

public class Service1{

@Autowired private CustomerMigrationStepRepo repo;

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

amolkumar18 avatar Jun 01 '21 20:06 amolkumar18

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 avatar Jun 01 '21 20:06 amolkumar18

Do we expecting any other information ??

amolkumar18 avatar Jun 03 '21 16:06 amolkumar18

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.

garyrussell avatar Jun 04 '21 15:06 garyrussell

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 ?

kumarvinothjoseph avatar Jan 17 '22 15:01 kumarvinothjoseph

same issue here

samirm avatar Feb 01 '23 14:02 samirm

@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

artembilan avatar Feb 01 '23 15:02 artembilan

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

quaff avatar Aug 21 '24 09:08 quaff

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.

artembilan avatar Aug 21 '24 14:08 artembilan