Skip to content

Commit 5c68a27

Browse files
committed
Add SchedulerFactoryBean runtime hints
Also require oracle/graalvm-reachability-metadata#19. Closes gh-28725
1 parent 6ad7b85 commit 5c68a27

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.quartz;
18+
19+
import java.util.List;
20+
21+
import org.springframework.aot.hint.MemberCategory;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
24+
import org.springframework.aot.hint.TypeReference;
25+
import org.springframework.util.ClassUtils;
26+
27+
/**
28+
* {@link RuntimeHintsRegistrar} implementation that makes sure {@link SchedulerFactoryBean}
29+
* reflection entries are registered.
30+
*
31+
* @author Sebastien Deleuze
32+
* @since 6.0
33+
*/
34+
public class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {
35+
36+
private static String SCHEDULER_FACTORY_CLASS_NAME = "org.quartz.impl.StdSchedulerFactory";
37+
38+
private static TypeReference FACTORY_BEAN_TYPE_REFERENCE = TypeReference.of(SchedulerFactoryBean.class);
39+
40+
@Override
41+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
42+
if (ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
43+
hints.reflection().registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME),
44+
builder -> builder
45+
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
46+
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
47+
hints.reflection().registerType(ResourceLoaderClassLoadHelper.class,
48+
builder -> builder
49+
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
50+
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
51+
hints.reflection().registerType(LocalTaskExecutorThreadPool.class,
52+
builder -> builder
53+
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
54+
.withMethod("setInstanceId", List.of(TypeReference.of(String.class)), b -> {})
55+
.withMethod("setInstanceName", List.of(TypeReference.of(String.class)), b -> {})
56+
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
57+
hints.reflection().registerType(LocalDataSourceJobStore.class,
58+
builder -> builder
59+
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
60+
.onReachableType(FACTORY_BEAN_TYPE_REFERENCE));
61+
62+
}
63+
}
64+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.springframework.aot.hint.RuntimeHintsRegistrar= \
2-
org.springframework.mail.javamail.JavaMailMimeTypesRuntimeHints
2+
org.springframework.mail.javamail.JavaMailMimeTypesRuntimeHints,\
3+
org.springframework.scheduling.quartz.SchedulerFactoryBeanRuntimeHints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.quartz;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
27+
import org.springframework.beans.factory.aot.AotServices;
28+
import org.springframework.util.ClassUtils;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link SchedulerFactoryBeanRuntimeHints}.
34+
*
35+
* @author Sebastien Deleuze
36+
*/
37+
public class SchedulerFactoryBeanRuntimeHintsTests {
38+
39+
private final RuntimeHints hints = new RuntimeHints();
40+
41+
@BeforeEach
42+
void setup() {
43+
AotServices.factories().load(RuntimeHintsRegistrar.class)
44+
.forEach(registrar -> registrar.registerHints(this.hints,
45+
ClassUtils.getDefaultClassLoader()));
46+
}
47+
48+
@Test
49+
void stdSchedulerFactoryHasHints() {
50+
assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of("org.quartz.impl.StdSchedulerFactory"))
51+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
52+
}
53+
54+
@Test
55+
void defaultClassLoadHelperHasHints() {
56+
assertThat(RuntimeHintsPredicates.reflection().onType(ResourceLoaderClassLoadHelper.class)
57+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
58+
}
59+
60+
@Test
61+
void defaultThreadPoolHasHints() {
62+
assertThat(RuntimeHintsPredicates.reflection().onType(LocalTaskExecutorThreadPool.class)
63+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
64+
.accepts(this.hints);
65+
}
66+
67+
@Test
68+
void defaultJobStoreHasHints() {
69+
assertThat(RuntimeHintsPredicates.reflection().onType(LocalDataSourceJobStore.class)
70+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
71+
.accepts(this.hints);
72+
}
73+
}

0 commit comments

Comments
 (0)