From 2743edc10c65b2d70ddfca0584fce6714304d3da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 16 Dec 2022 13:56:58 +0100 Subject: [PATCH] Add draft EhCache support Does not work yet, see https://github.com/oracle/graalvm-reachability-metadata/pull/124/. --- framework/cache-ehcache/README.adoc | 1 + framework/cache-ehcache/build.gradle | 18 +++++++++++ .../CacheEhcacheApplicationAotTests.java | 31 +++++++++++++++++++ .../java/com/example/cache/ehcache/CLR.java | 30 ++++++++++++++++++ .../cache/ehcache/CacheConfiguration.java | 25 +++++++++++++++ .../ehcache/CacheEhcacheApplication.java | 14 +++++++++ .../cache/ehcache/clazz/TestServiceClass.java | 17 ++++++++++ .../ehcache/iface/TestServiceInterface.java | 7 +++++ .../iface/TestServiceWithInterface.java | 18 +++++++++++ .../src/main/resources/application.properties | 1 + gradle.properties | 2 +- 11 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 framework/cache-ehcache/README.adoc create mode 100644 framework/cache-ehcache/build.gradle create mode 100644 framework/cache-ehcache/src/appTest/java/com/example/cache/ehcache/CacheEhcacheApplicationAotTests.java create mode 100644 framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CLR.java create mode 100644 framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheConfiguration.java create mode 100644 framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheEhcacheApplication.java create mode 100644 framework/cache-ehcache/src/main/java/com/example/cache/ehcache/clazz/TestServiceClass.java create mode 100644 framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceInterface.java create mode 100644 framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceWithInterface.java create mode 100644 framework/cache-ehcache/src/main/resources/application.properties diff --git a/framework/cache-ehcache/README.adoc b/framework/cache-ehcache/README.adoc new file mode 100644 index 00000000..9cc15656 --- /dev/null +++ b/framework/cache-ehcache/README.adoc @@ -0,0 +1 @@ +Tests if caching with EhCache works diff --git a/framework/cache-ehcache/build.gradle b/framework/cache-ehcache/build.gradle new file mode 100644 index 00000000..33ef1f30 --- /dev/null +++ b/framework/cache-ehcache/build.gradle @@ -0,0 +1,18 @@ +plugins { + id 'java' + id 'org.springframework.boot' + id 'org.springframework.aot.smoke-test' + id 'org.graalvm.buildtools.native' +} + +dependencies { + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("org.springframework.boot:spring-boot-starter-cache") + implementation("javax.cache:cache-api") + implementation("org.ehcache:ehcache") + + testImplementation("org.springframework.boot:spring-boot-starter-test") + + appTestImplementation(project(":aot-smoke-test-support")) + appTestImplementation("org.awaitility:awaitility:4.2.0") +} diff --git a/framework/cache-ehcache/src/appTest/java/com/example/cache/ehcache/CacheEhcacheApplicationAotTests.java b/framework/cache-ehcache/src/appTest/java/com/example/cache/ehcache/CacheEhcacheApplicationAotTests.java new file mode 100644 index 00000000..6fac9fec --- /dev/null +++ b/framework/cache-ehcache/src/appTest/java/com/example/cache/ehcache/CacheEhcacheApplicationAotTests.java @@ -0,0 +1,31 @@ +package com.example.cache.ehcache; + +import java.time.Duration; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; + +import org.springframework.aot.smoketest.support.assertj.AssertableOutput; +import org.springframework.aot.smoketest.support.junit.ApplicationTest; + +import static org.assertj.core.api.Assertions.assertThat; + +@ApplicationTest +class CacheEhcacheApplicationAotTests { + + @Test + void methodIsCachedOnClasses(AssertableOutput output) { + Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { + assertThat(output).hasSingleLineContaining("class.invoke: 1").hasNoLinesContaining("class.invoke: 2"); + }); + } + + @Test + void methodIsCachedOnInterfaces(AssertableOutput output) { + Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { + assertThat(output).hasSingleLineContaining("interface.invoke: 1") + .hasNoLinesContaining("interface.invoke: 2"); + }); + } + +} diff --git a/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CLR.java b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CLR.java new file mode 100644 index 00000000..2eed52da --- /dev/null +++ b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CLR.java @@ -0,0 +1,30 @@ +package com.example.cache.ehcache; + +import com.example.cache.ehcache.clazz.TestServiceClass; +import com.example.cache.ehcache.iface.TestServiceInterface; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +class CLR implements CommandLineRunner { + + private final TestServiceClass testServiceClass; + + private final TestServiceInterface testServiceInterface; + + public CLR(TestServiceClass testServiceClass, TestServiceInterface testServiceInterface) { + this.testServiceClass = testServiceClass; + this.testServiceInterface = testServiceInterface; + } + + @Override + public void run(String... args) { + this.testServiceClass.invoke(); + this.testServiceClass.invoke(); + + this.testServiceInterface.invoke(); + this.testServiceInterface.invoke(); + } + +} diff --git a/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheConfiguration.java b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheConfiguration.java new file mode 100644 index 00000000..b2a829eb --- /dev/null +++ b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheConfiguration.java @@ -0,0 +1,25 @@ +package com.example.cache.ehcache; + +import javax.cache.configuration.MutableConfiguration; + +import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@EnableCaching +class CacheConfiguration { + + @Bean + public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() { + return cacheManager -> { + cacheManager.createCache("interface.invoke", cacheConfiguration()); + cacheManager.createCache("class.invoke", cacheConfiguration()); + }; + } + + private javax.cache.configuration.Configuration cacheConfiguration() { + return new MutableConfiguration<>().setStatisticsEnabled(true); + } +} diff --git a/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheEhcacheApplication.java b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheEhcacheApplication.java new file mode 100644 index 00000000..03499f63 --- /dev/null +++ b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/CacheEhcacheApplication.java @@ -0,0 +1,14 @@ +package com.example.cache.ehcache; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CacheEhcacheApplication { + + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(CacheEhcacheApplication.class, args); + Thread.currentThread().join(); // To be able to measure memory consumption + } + +} diff --git a/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/clazz/TestServiceClass.java b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/clazz/TestServiceClass.java new file mode 100644 index 00000000..0d95fac0 --- /dev/null +++ b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/clazz/TestServiceClass.java @@ -0,0 +1,17 @@ +package com.example.cache.ehcache.clazz; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +@Service +public class TestServiceClass { + + private int counter = 1; + + @Cacheable(cacheNames = "class.invoke") + public void invoke() { + System.out.printf("class.invoke: %d%n", this.counter); + this.counter++; + } + +} diff --git a/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceInterface.java b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceInterface.java new file mode 100644 index 00000000..f30b72f6 --- /dev/null +++ b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceInterface.java @@ -0,0 +1,7 @@ +package com.example.cache.ehcache.iface; + +public interface TestServiceInterface { + + void invoke(); + +} diff --git a/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceWithInterface.java b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceWithInterface.java new file mode 100644 index 00000000..3d4cea60 --- /dev/null +++ b/framework/cache-ehcache/src/main/java/com/example/cache/ehcache/iface/TestServiceWithInterface.java @@ -0,0 +1,18 @@ +package com.example.cache.ehcache.iface; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +@Service +class TestServiceWithInterface implements TestServiceInterface { + + private int counter = 1; + + @Override + @Cacheable(cacheNames = "interface.invoke") + public void invoke() { + System.out.printf("interface.invoke: %d%n", this.counter); + this.counter++; + } + +} diff --git a/framework/cache-ehcache/src/main/resources/application.properties b/framework/cache-ehcache/src/main/resources/application.properties new file mode 100644 index 00000000..8b70287a --- /dev/null +++ b/framework/cache-ehcache/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.cache.type=jcache diff --git a/gradle.properties b/gradle.properties index 55c73ebc..60191ec2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ #reachabilityMetadataVersion=0.2.5 -#reachabilityMetadataUrl= +reachabilityMetadataUrl=file:///home/seb/workspace/graalvm-reachability-metadata/metadata kotlinVersion=1.7.21 javaFormatVersion=0.0.34 nbtVersion=0.9.18