実現したいこと
spring bootのspring retryを使い指定のException(添付ソースはRuntimeException)が発生した場合、(maxAttemptsに)指定した回数リトライする。添付ソースは4回リトライしたいです。
発生している問題・分からないこと
http://localhost:8080/hello
にアクセスすると
リトライが行われません。
spring retryが動いてないようなのですが、どのように改善すればspring retryが起動し指定した回数リトライされるかご教授をお願い致します。
確認した内容:
上記URLアクセス時のコンソールログ一部で「test=」が1回しか出てないためリトライされていない。
hello
test=
[2m2025-03-20T12:25:37.137+09:00[0;39m [31mERROR[0;39m [35m9240[0;39m [2m--- [helloworld] [nio-8080-exec-1] [0;39m[36mo.a.c.c.C.[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.RuntimeException] with root cause
java.lang.RuntimeException: null
エラーメッセージ
error
1上記のとおり、「test=」が1回しか出てないためリトライされていない。
該当のソースコード
java
1package com.hello; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.retry.annotation.Backoff; 7import org.springframework.retry.annotation.EnableRetry; 8import org.springframework.retry.annotation.Retryable; 9import org.springframework.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.RequestParam; 11import org.springframework.web.bind.annotation.RestController; 12 13@SpringBootApplication 14@RestController 15@EnableRetry 16@Configuration 17public class HelloworldApplication { 18 19 public static void main(String[] args) { 20 SpringApplication.run(HelloworldApplication.class, args); 21 } 22 23 @GetMapping("/hello") 24 public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { 25 26 System.out.println("hello"); 27 28 retryTest(); 29 return String.format("Hello %s!", name); 30 } 31 32 @Retryable(retryFor = { RuntimeException.class }, maxAttempts = 4, backoff = @Backoff(3000)) 33 public void retryTest() { 34 System.out.println("test="); 35 36 throw new RuntimeException(); 37 38 } 39} 40 41 42
pom.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>3.4.3</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.hello</groupId> 12 <artifactId>helloworld</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>helloworld</name> 15 <description>Demo project for Spring Boot</description> 16 <url/> 17 <licenses> 18 <license/> 19 </licenses> 20 <developers> 21 <developer/> 22 </developers> 23 <scm> 24 <connection/> 25 <developerConnection/> 26 <tag/> 27 <url/> 28 </scm> 29 <properties> 30 <java.version>21</java.version> 31 </properties> 32 <dependencies> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-thymeleaf</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-web</artifactId> 40 </dependency> 41 42 <dependency> 43 <groupId>org.projectlombok</groupId> 44 <artifactId>lombok</artifactId> 45 <optional>true</optional> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 <scope>test</scope> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework.retry</groupId> 54 <artifactId>spring-retry</artifactId> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-starter-aop</artifactId> 59 </dependency> 60 </dependencies> 61 62 <build> 63 <plugins> 64 <plugin> 65 <groupId>org.apache.maven.plugins</groupId> 66 <artifactId>maven-compiler-plugin</artifactId> 67 <configuration> 68 <annotationProcessorPaths> 69 <path> 70 <groupId>org.projectlombok</groupId> 71 <artifactId>lombok</artifactId> 72 </path> 73 </annotationProcessorPaths> 74 </configuration> 75 </plugin> 76 <plugin> 77 <groupId>org.springframework.boot</groupId> 78 <artifactId>spring-boot-maven-plugin</artifactId> 79 <configuration> 80 <excludes> 81 <exclude> 82 <groupId>org.projectlombok</groupId> 83 <artifactId>lombok</artifactId> 84 </exclude> 85 </excludes> 86 </configuration> 87 </plugin> 88 </plugins> 89 </build> 90 91</project> 92
application.properties
1spring.application.name=helloworld 2
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
https://spring.pleiades.io/quickstart
にて初期プロジェクト作成
リトライ参考サイト
https://github.com/spring-projects/spring-retry?tab=readme-ov-file
https://olafnosuke.hatenablog.com/entry/2024/02/08/205124
https://qiita.com/SotaOishi/items/f19d50794e3fabad5e95
補足
特になし
回答1件
あなたの回答
tips
プレビュー