Created
December 21, 2017 08:17
-
-
Save gokhanoner/026c2b90fe3a61b93626383a61932395 to your computer and use it in GitHub Desktop.
Spring Cache Error Handling using Aspect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.cachetest; | |
import lombok.extern.slf4j.Slf4j; | |
import org.aspectj.lang.JoinPoint; | |
import org.aspectj.lang.Signature; | |
import org.aspectj.lang.annotation.AfterThrowing; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cache.annotation.Cacheable; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.cache.interceptor.KeyGenerator; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.stereotype.Component; | |
import org.springframework.stereotype.Service; | |
import java.lang.reflect.Method; | |
import java.util.Arrays; | |
@SpringBootApplication | |
@EnableCaching | |
public class CacheTestApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(CacheTestApplication.class, args); | |
} | |
@Bean | |
CommandLineRunner cli(DummyService dummyService) { | |
return args -> { | |
dummyService.get(1); | |
}; | |
} | |
} | |
@Service | |
class DummyService { | |
@Cacheable(cacheNames = "TestCache",keyGenerator = "customKeyGenerator") | |
public String get(Integer param1) { | |
throw new RuntimeException("11"); | |
//return String.valueOf(param1); | |
} | |
} | |
@Component | |
class CustomKeyGenerator implements KeyGenerator { | |
@Override | |
public Object generate(Object o, Method method, Object... params) { | |
return (Integer)(params[1]) * 2; | |
} | |
} | |
@Aspect | |
@Component | |
@Slf4j | |
class AfterThrowingExample { | |
@AfterThrowing(value = "execution (* com.example.cachetest.DummyService.*(..)) || execution (* com.example.cachetest.CustomKeyGenerator.generate(..))", throwing = "ex") | |
public void handleGetError(JoinPoint joinPoint, Exception ex) { | |
Signature signature = joinPoint.getSignature(); | |
String methodName = signature.getName(); | |
String stuff = signature.toString(); | |
String arguments = Arrays.toString(joinPoint.getArgs()); | |
log.error("handle error for {}. Arguments: {}", stuff, arguments, ex); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>cache-test</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>cache-test</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.7.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-cache</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-aop</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<optional>true</optional> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment