对于重试功能,我想使用
org.springframework.batch.retry.interceptor.RetryOperationsInterceptor.
我可以在 spring-batch 2.1.X 中找到它,但在 spring-batch 2.2.X 中找不到。
被删除了吗?它有问题吗?
如果它被删除了,我应该使用不同的方法来代替,它是什么?
我有一个表更新导致死锁,并试图让Spring重试在方法获得某种锁定异常时重试.我已经尝试取出maxAttempts,值和退避但它似乎永远不会捕获任何异常.我错过了什么吗?我是否需要在Application文件中声明一个bean?任何帮助将非常感激!
@SpringBootApplication
@EnableRetry
public class Application extends SpringBootServletInitializer {
Run Code Online (Sandbox Code Playgroud)
@Service
public class DetailService {
@Retryable(maxAttempts = 5, value = { LockAcquisitionException.class, ConcurrencyFailureException.class }, backoff = @Backoff(delay = 500, multiplier = 2) )
public void delete(final String detailCode) {
try {
this.delete(this.dao.findByDetailCode(detailCode));
} catch (LockAcquisitionException | ConcurrencyFailureException e) {
LOG.warn("Locking error! Going to retry", e.getMessage());
throw e;
}
}
public void delete(Details detail) {
this.dao.delete(detail);
}
@Retryable(maxAttempts = 5, value = { LockAcquisitionException.class, ConcurrencyFailureException.class }, backoff = @Backoff(delay = …Run Code Online (Sandbox Code Playgroud) 我有一个类型为B的spring托管bean.我在@Configuration类中有@EnableREtry.当我使用@Retryable时doStuff(),该方法会在失败时按预期重试.
但是,我真正想要重试的方法是在基类A中定义的方法.A是一个具体的类,而不是一个Spring托管bean.doSomethingElse抛出异常时不会重试该方法.
我真的想要重试doSomethingElse,基类方法.但是,我不知道该怎么做.我猜它是因为A是一个具体的类而不是bean,尽管它确实充当了基类.
我是否需要在A类中使用RetryableTemplate?
public class B extends A {
public void doStuff() {
super.doSomethingElse();
}
}
public class A {
// doesn't actually retry
@Retryable
public void doSomething() {
throws new Exception();
}
}
Run Code Online (Sandbox Code Playgroud) 当我运行我的单元测试时,我希望 thisFails() 方法重试 3 次,然后我希望看到恢复记录器行被打印出来,但它只尝试一次然后抛出异常。底部的输出是在我运行测试之后。
我错过了什么?
随意忽略此部分,直接跳到代码。linter 认为我没有足够的说明来发布。我认为措辞足以表达我的问题,但由于某种原因,除非我写更多的东西,否则我不允许发布这个问题。所以这里有更多的东西,等等。
--Spring Boot 应用程序--
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
@EnableRetry
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
- 服务 -
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class MyService {
@Retryable(include = RuntimeException.class)
public int thisFails() {
log.info("Help I am failing");
throw new RuntimeException();
}
@Recover
public int thisRecovers(RuntimeException re) {
log.info("I recovered");
return 0;
} …Run Code Online (Sandbox Code Playgroud) 下面的参数,试图使可配置
@Async("threadPoolTaskExecutor")
@Retryable(value = MessagingException.class, maxAttempts = 2, backoff = @Backoff(delay = 5000))
Run Code Online (Sandbox Code Playgroud)
我在 application.properties 文件中做了以下更改
my.app.maxAttempts = 2
my.app.backOffDelay = 5000
Run Code Online (Sandbox Code Playgroud)
和
@Retryable(value = MessagingException.class, maxAttempts = "${my.app.maxAttempts}", backoff = @Backoff(delay = "${my.app.my.app.backOffDelay}"))
Run Code Online (Sandbox Code Playgroud)
但得到以下错误。不兼容的类型。找到java.lang.String。需要'int'
在 build.gradle 文件中我有
dependencies {
compile ("org.springframework.boot:spring-boot-starter-web")
compile ("org.springframework.boot:spring-boot-starter-mail:2.1.2.RELEASE")
compile ("org.springframework.boot:spring-boot-starter-security:2.1.2.RELEASE")
compile ("org.springframework.boot:spring-boot-starter-aop")
compile ("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jdbc:2.1.0.RELEASE")
compile ("org.springframework.retry:spring-retry:1.2.4.RELEASE")
compile ("io.springfox:springfox-swagger2:2.9.2")
compile ("io.springfox:springfox-swagger-ui:2.9.2")
compile ("io.springfox:springfox-bean-validators:2.9.2")
compile ("javax.validation:validation-api:2.0.0.Final")
compile ("org.hibernate.validator:hibernate-validator")
compile ("org.hibernate.validator:hibernate-validator-annotation-processor")
// compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.8'
compile ("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8")
compile("org.postgresql:postgresql")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.batch:spring-batch-test')
testCompile …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现重试逻辑。我的代码按预期工作,直到重试方法的返回类型为空。当我将其更改为 String 时,@Recover 停止工作。
@Component
public class AdapterImpl {
int count = 0;
@Retryable(include = {NullPointerException.class, IllegalStateException.class}, backoff = @Backoff(delay = 100, maxDelay = 101), maxAttempts = 5)
public void retry(String foo) {
System.out.println(foo + " " + count++);
if (foo.equals("foo")) {
throw new NullPointerException("foo");
} else if (foo.equals("bar")) {
throw new IllegalStateException("bar");
}
// return "hi";
}
@Recover
public void connectionException(NullPointerException e) {
System.out.println("Retry failure NullPointerException");
}
@Recover
public void connectionException(IllegalStateException e) {
System.out.println("Retry failure IllegalStateException");
}
}
Run Code Online (Sandbox Code Playgroud)
对于 …
我在这样的方法上使用@Retryable:-
@Retryable( value = SQLException.class,
maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};
Run Code Online (Sandbox Code Playgroud)
我想打印重试次数,并显示如下消息:
Retry Number : 1
Retry Number : 2
...
Retry Number : 5
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我是新来的spring-retry。基本上,为了重试对 REST API 的调用,我已集成spring-retry到我的 spring-boot 应用程序中。为此,我做了以下更改:
添加spring-retry到 pom.xml。
添加了以下配置:
@Configuration
@EnableRetry
public class RetryConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
最后@Retryable向类(此类不是 Spring Bean)方法添加注释,我希望针对各种异常重试该方法,如下所示:
public class OAuth1RestClient extends OAuthRestClient {
@Override
@Retryable(maxAttempts = 3, value = {
Exception.class},
backoff = @Backoff(delay = 100, multiplier = 3))
public Response executeRequest(OAuthRequest request)
throws InterruptedException, ExecutionException, IOException {
System.out.println("Inside Oauth1 client");
return myService.execute(request);
}
Run Code Online (Sandbox Code Playgroud)
现在,该executeRequest方法不再重试。我无法理解我是否在这里遗漏了任何东西。
有人可以帮忙吗?谢谢。
spring-retry ×8
java ×7
spring ×5
spring-boot ×5
aop ×1
recover ×1
retry-logic ×1
spring-batch ×1