Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何在Java8中实现异步重试机制?_Spring Boot_Asynchronous_Java 8_Spring Retry - Fatal编程技术网

Spring boot 如何在Java8中实现异步重试机制?

Spring boot 如何在Java8中实现异步重试机制?,spring-boot,asynchronous,java-8,spring-retry,Spring Boot,Asynchronous,Java 8,Spring Retry,我正在使用Java8SpringBoot。我有下面的方法 public hello() { try { // send message } catch(HttpClientErrorException e) { if (e.getRawStatusCode() == 401) { // I need to retry the same hello() method for three times as in 10sec, 20sec and 25

我正在使用Java8SpringBoot。我有下面的方法

public hello() {
  try {
    // send message
  }
  catch(HttpClientErrorException e) {
     if (e.getRawStatusCode() == 401) {
          // I need to retry the same hello() method for three times as in 10sec, 20sec and 25sec.
     }
  }
}
我需要调用相同的方法三次,以便在遇到catch块时重试。 如何异步执行此操作

我找到了下面的代码,但它不起作用

@Retryable( value = {RestClientException.class}, maxAttempts = 3, backoff = @Backoff(3000)) 

感谢您的帮助。

对于重试机制,您可以使用
@Retryable(value=RestClientException.class)

要触发此异常,您需要实际抛出此异常(或从RestClientException扩展而来的异常)。由于catch语句,实际上不会引发异常,因此重试机制不会启动

@Retryable( value = {RestClientException.class}, maxAttempts = 3, backoff = @Backoff(3000)) 
public void hello() {
  try {
    // send message
  }
  catch(HttpClientErrorException e) {
     if (e.getRawStatusCode() == 401) {
          throw new RestClientException("meaningfull message");
     }
  }
}
如果要在3次重试失败后运行一些catch代码,可以在恢复方法上使用
@Recover
注释

如果您想了解有关重试机制的更多信息,可以查看

另外,不要忘记在配置中添加
@enablerery
,以便使用注释

使用spring boot的完整代码示例
}

对于重试机制,您可以使用
@Retryable(value=RestClientException.class)

要触发此异常,您需要实际抛出此异常(或从RestClientException扩展而来的异常)。由于catch语句,实际上不会引发异常,因此重试机制不会启动

@Retryable( value = {RestClientException.class}, maxAttempts = 3, backoff = @Backoff(3000)) 
public void hello() {
  try {
    // send message
  }
  catch(HttpClientErrorException e) {
     if (e.getRawStatusCode() == 401) {
          throw new RestClientException("meaningfull message");
     }
  }
}
如果要在3次重试失败后运行一些catch代码,可以在恢复方法上使用
@Recover
注释

如果您想了解有关重试机制的更多信息,可以查看

另外,不要忘记在配置中添加
@enablerery
,以便使用注释

使用spring boot的完整代码示例
}

您可以使用Spring的
@Async
注释来实现这一点。 您必须创建如下配置:

@配置
@启用重试
@使能同步
类RetryConfig{}
当您想将
Async
Retry
一起使用时,必须使用
Async
来修饰该方法,该方法试图调用
Retryable
方法。此外,您必须确保返回的是
Future
或类似的内容,因为您要在后台发送这段代码 我还实现了回退机制,否则请求将以500个异常终止

如果运行下面的代码,您可以看到主请求在线程
http-nio-8080-exec-1
上执行,而异步代码在不同的线程
task-1
上执行

我试图用一个示例服务方法来解释这一点,但本地或远程服务调用的概念是相同的

下面给出了详细的exmaple:

package com.example.silentsudo.springcloudssamples;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.retry.annotation.Backoff;
导入org.springframework.retry.annotation.EnableRetry;
导入org.springframework.retry.annotation.Recover;
导入org.springframework.retry.annotation.Retryable;
导入org.springframework.scheduling.annotation.Async;
导入org.springframework.scheduling.annotation.EnableAsync;
导入org.springframework.stereotype.Service;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.RestController;
导入java.util.concurrent.CompletableFuture;
@SpringBoot应用程序
公共类SpringClouds示例应用程序{
公共静态void main(字符串[]args){
run(SpringCloudsSamplesApplication.class,args);
}
}
@请求映射(path=“sample”)
@RestController
类采样控制器{
私人最终GreetService GreetService;
采样控制器(GreetService GreetService){
this.greetService=greetService;
}
@GetMapping
公共字符串hello(){
System.out.println(Thread.currentThread().getName());
回复“你好!”;
}
@GetMapping(path=“greet”)
公共字符串问候语(@RequestParam(value=“name”,defaultValue=“John”)字符串名){
返回greetService.greet(名称);
}
@异步的
@GetMapping(path=“greet async”)
public CompletableFuture greetAsync(@RequestParam(value=“name”,defaultValue=“John”)字符串名){
返回CompletableFuture.completedFuture(greetService.greet(name));
}
}
@配置
@启用重试
@使能同步
类RetryConfig{
}
@服务
类GreetService{
非公开最终非公开服务非公开服务;
绿色服务(UngaBungaService UNGABUNGASVICE){
this.ungaBungaService=ungaBungaService;
}
@可重试(maxAttempts=5,value=GreetException.class,backoff=@backoff(value=3000L))
公共字符串(字符串名称){
返回ungaBungaService.lol(名称);
}
@恢复
公共字符串recoverGreetException(GreetException GreetException){
返回greetException.getMessage();
}
}
@服务
类服务{
公共字符串lol(字符串名称){
System.out.println(Thread.currentThread().getName());
抛出新的GreetException(“为“+name”调用greet);
}
}
类GreetException扩展了RuntimeException{
公共GreetException(字符串消息){
超级(信息);
}
}

您可以使用Spring的
@Async
注释来实现这一点。 您必须创建如下配置:

@配置
@启用重试
@使能同步
类RetryConfig{}
当您想将
Async
Retry
一起使用时,必须使用
Async
来修饰该方法,该方法试图调用
Retryable
方法。此外,您必须确保返回的是
Future
或类似内容,因为您是