-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expose more reflection hints #3951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
garyrussell
merged 2 commits into
spring-projects:main
from
artembilan:More_AOT_and_Docs
Nov 18, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
54 changes: 54 additions & 0 deletions
54
...tion-kafka/src/main/java/org/springframework/integration/kafka/aot/KafkaRuntimeHints.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.kafka.aot; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.springframework.aot.hint.ExecutableMode; | ||
import org.springframework.aot.hint.ReflectionHints; | ||
import org.springframework.aot.hint.RuntimeHints; | ||
import org.springframework.aot.hint.RuntimeHintsRegistrar; | ||
import org.springframework.integration.kafka.inbound.KafkaInboundGateway; | ||
import org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter; | ||
import org.springframework.integration.kafka.inbound.KafkaMessageSource; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
/** | ||
* {@link RuntimeHintsRegistrar} for Spring Integration Kafka module. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 6.0 | ||
*/ | ||
class KafkaRuntimeHints implements RuntimeHintsRegistrar { | ||
|
||
@Override | ||
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { | ||
ReflectionHints reflectionHints = hints.reflection(); | ||
|
||
// Java DSL does not register beans during AOT phase, so @Reflective is not reachable from Pausable | ||
Stream.of("pause", "resume", "isPaused") | ||
.flatMap((name) -> | ||
Stream.of(KafkaMessageDrivenChannelAdapter.class, | ||
KafkaInboundGateway.class, | ||
KafkaMessageSource.class) | ||
.filter((type) -> reflectionHints.getTypeHint(type) == null) | ||
.flatMap((type) -> Stream.ofNullable(ReflectionUtils.findMethod(type, name)))) | ||
.forEach(method -> reflectionHints.registerMethod(method, ExecutableMode.INVOKE)); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
spring-integration-kafka/src/main/resources/META-INF/spring/aot.factories
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.springframework.aot.hint.RuntimeHintsRegistrar=org.springframework.integration.kafka.aot.KafkaRuntimeHints |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[[native-images-support]] | ||
=== Native Images Support | ||
|
||
Starting with version 6.0, GraalVM compilation of Spring Integration applications to native images is supported by https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aot[Spring AOT] native hints. | ||
For most common use cases, such as endpoint definitions with `@Bean` methods, Java DSL configuration with lambdas and `@MessagingGateway` interface scanning (importing), the framework provides respective reflection, proxy and serialization hints. | ||
If configuration uses messaging annotations (`@ServiceActivator`, `@Splitter` etc.) on POJO methods, or POJO methods are used with the `IntegrationFlowBuilder.handle(Object service, String methodName)` API, they have to be also marked with a `@Reflective` annotation since they are invoked by the framework reflectively. | ||
|
||
IMPORTANT: XML configuration is not supported for native images. | ||
|
||
As stated before, service interfaces with the `@MessagingGateway` annotation, when they are scanned by the `@IntegrationComponentScan` or used in an `@Import` annotation, are processed by the framework and the respective proxy hint is exposed into the AOT contribution. | ||
When gateways are declared using the `IntegrationFlow.from(Class<?> serviceInterface)` API, the proxy configured for such interfaces have to be exposed manually: | ||
|
||
==== | ||
[source,java] | ||
---- | ||
@Configuration | ||
@EnableIntegration | ||
@ImportRuntimeHints(GatewayRuntimeHints.class) | ||
public class IntegrationConfiguration { | ||
|
||
@Bean | ||
IntegrationFlow someFlow() { | ||
return IntegrationFlow.from(SomeGateway) | ||
// ... | ||
.get(); | ||
} | ||
|
||
public interface SomeGateway { | ||
|
||
void doSomething(Object payload); | ||
|
||
} | ||
|
||
private static class GatewayRuntimeHints implements RuntimeHintsRegistrar { | ||
|
||
@Override | ||
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { | ||
hints.proxies().registerJdkProxy( | ||
AopProxyUtils.completeJdkProxyInterfaces(SomeGateway)); | ||
} | ||
|
||
} | ||
|
||
} | ||
---- | ||
==== | ||
|
||
NOTE: The `IntegrationFlow` content is not processed during the AOT processing phase. | ||
Therefore, some hints, such as the one mentioned above for a gateway proxy, must be provided by the target application. | ||
|
||
Of course, configuration is just a piece of an integration solution. | ||
The most important part is data transferring over the network as well as persistent storage. | ||
That's where serialization comes handy for many use-cases. | ||
Spring Integration exposes serialization hints into a native image configuration for these types used by the framework internally: `String`, `Number`, `Long`, `Date`, `ArrayList`, `HashMap`, `Properties`, `Hashtable`, `Exception`, `UUID`, `GenericMessage`, `ErrorMessage`, `MessageHeaders`, `AdviceMessage`, `MutableMessage`, `MutableMessageHeaders`, `MessageGroupMetadata`, `MessageHolder`, `MessageMetadata`, `MessageHistory`, `MessageHistory.Entry`, `DelayHandler.DelayedMessageWrapper`. | ||
For user specific data, mostly present as a message payload, the serialization hint must be exposed manually via a `RuntimeHintsRegistrar` implementation, as is shown above for a gateway proxy, and the respective `RuntimeHints.serialization().registerType()` API. | ||
|
||
NOTE: It is recommended that native integration applications are developed with Spring Boot, using its respective build tools. |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to
KafkaIntegrationRuntimeHints
to avoid using the same class name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was thinking about, but there is a package in a class name either way.
Plus this one is
public
as any libraryRuntimeHintsRegistrar
impls registered in theaot.factories
file.See other Spring Integration impls:
HttpRuntimeHints
,JdbcRuntimeHints
etc.I really decided that
integration
in a name is going to be redundant if we have it already in a package name, plus this one is not public API to be used from end-user perspective.