質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.32%
Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

128閲覧

spring retryが動かない

totomore

総合スコア49

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2025/03/20 03:36

編集2025/03/20 03:55

実現したいこと

spring bootのspring retryを使い指定のException(添付ソースはRuntimeException)が発生した場合、(maxAttemptsに)指定した回数リトライする。添付ソースは4回リトライしたいです。

発生している問題・分からないこと

http://localhost:8080/hello
にアクセスすると
リトライが行われません。
spring retryが動いてないようなのですが、どのように改善すればspring retryが起動し指定した回数リトライされるかご教授をお願い致します。

確認した内容:
上記URLアクセス時のコンソールログ一部で「test=」が1回しか出てないためリトライされていない。
hello
test=
2025-03-20T12:25:37.137+09:00 ERROR 9240 --- [helloworld] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]  : 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

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KT001

2025/03/20 14:15 編集

直接、retryTest()メソッドを呼び出してしまっているので、公式のようにService化(DI経由で呼び出す)をお試しください。
totomore

2025/03/21 00:21

ご回答ありがとうございます。 上記アドバイスで試してみます。
guest

回答1

0

自己解決

仕事場PCで対応したので対応結果は載せていませんが、DI化だけでは対応不可で
対応内容としては
リトライをしたいメソッドは別のクラスから呼び出すことでリトライ出来ました。

参考サイト
https://scrapbox.io/ponpoko04-blog/Spring_Retry%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%BF%E3%82%8B

投稿2025/03/21 09:30

totomore

総合スコア49

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KT001

2025/03/21 10:31

動いて良かったです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.32%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問