Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retryable doesn't work for inherited class methods #267

Closed
elafontaine opened this issue Feb 15, 2022 · 4 comments
Closed

Retryable doesn't work for inherited class methods #267

elafontaine opened this issue Feb 15, 2022 · 4 comments
Labels

Comments

@elafontaine
Copy link

Hi all,

So I've been trying to implement a Retryable annotation on a public method of a Bean that is inherited from another class.
When I put the annotation on the child class (on which the annotation for repository is) and use it on a method there, I'm able to make it work, but not if I put the Retryable at the parent level, it goes undetected.

i.e.

@Repository
public class something() extends something2 {

}

public abstract class something2 {
    @Retryable
    public result myMethod(){
        // some logic that can fail
    }
}

Not sure if this is a valid usecase though. Please let me know what you think.

@garyrussell
Copy link
Contributor

garyrussell commented Feb 15, 2022

That other issue you commented on was about a different type of @Repository where Spring Data generates the implementation.

What version are you using? Some problems like this were fixed long ago; I just tested it with the latest version (1.3.1) and it works fine for me;

@SpringBootApplication
@EnableRetry
public class Retgh267Application {

	public static void main(String[] args) {
		SpringApplication.run(Retgh267Application.class, args);
	}

	@Bean
	ApplicationRunner runner(Bar bar) {
		return args -> {
			bar.canRetry();
		};
	}

}

class Foo {

	@Retryable
	void canRetry() {
		System.out.println("here");
		throw new RuntimeException("test");
	}
}

@Repository
class Bar extends Foo {

}
here
here
here
2022-02-15 14:00:19.297  INFO 59778 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-15 14:00:19.313 ERROR 59778 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute ApplicationRunner
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:763) ~[spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:750) ~[spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:309) ~[spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.6.3.jar:2.6.3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.6.3.jar:2.6.3]
	at com.example.demo.Retgh267Application.main(Retgh267Application.java:16) ~[classes/:na]
Caused by: java.lang.RuntimeException: test

@elafontaine
Copy link
Author

Hi Gary, I figured out what was happening;
I believe we call them proxy bean and I'm still learning how they work (sorry for that).

So the issue I faced was not inheritance in the end, but local function redirections;

@Repository
public class something() extends something2 {
      public result mypubliclyexposedmethod(){
            return myMethod()
      }
}

public abstract class something2 {
    @Retryable
    public result myMethod(){
        // some logic that can fail
    }
}

And so, in this case, the "local" function call redirection to the same object by-pass the proxy object and so the retry are not seen and acted on. (From what I could get! I may be wrong)

@garyrussell
Copy link
Contributor

Yes; local calls bypass the proxy; advised methods must be called from another bean.

@garyrussell
Copy link
Contributor

Closing as resolved; please reopen if you feel you need something more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants