Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions spring-scheduling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-scheduling</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>com.baeldung</groupId>
Expand Down Expand Up @@ -56,10 +68,21 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<spring-retry.version>2.0.12</spring-retry.version>
<spring-boot.version>4.0.2</spring-boot.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.springretry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.resilience.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class ExternalService {

private static final Logger LOG = LoggerFactory.getLogger(ExternalService.class);

private int attempt = 0;

@Retryable(maxRetries = 2, delay = 500)
public void callExternalApi() {

attempt++;
LOG.info("Attempt {} - Calling external API...", attempt);

throw new RuntimeException("Temporary connection failure!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.springretry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.resilience.retry.MethodRetryEvent;
import org.springframework.stereotype.Component;

@Component
public class RetryEventListener {

private static final Logger log = LoggerFactory.getLogger(RetryEventListener.class);

@EventListener
public void onRetryEvent(MethodRetryEvent event) {
String methodName = event.getMethod()
.getName();
Throwable exception = event.getFailure();

if (event.isRetryAborted()) {
log.error("Retries exhausted for method '{}' after {} attempts. Final exception: {}", methodName, exception.getMessage());

} else {
log.warn("Retry failed for method '{}'. Exception: {}", methodName, exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.springretry;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringRetryListenerIntegrationTest {

@Autowired
private ExternalService externalService;

@Test
void givenFailingExternalService_whenCallExternalApi_thenShouldRetryMultipleTimesAndLogRetriesExhausted() {
assertThrows(RuntimeException.class, () -> {
externalService.callExternalApi();
});
}

}
12 changes: 11 additions & 1 deletion spring-scheduling/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="TEST" class="com.baeldung.springretry.logging.LogAppender"/>
<root level="info">

<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="TEST"/>
</root>

<logger name="com.b.springretry" level="DEBUG"/>

</configuration>