Native Images 支援
從 6.0 版本開始,Spring Integration 應用程式使用 GraalVM 編譯為 Native Images 的功能已透過 Spring AOT native hints 得到支援。對於大多數常見用例,例如使用 @Bean
方法定義端點、使用 lambda 的 Java DSL 配置以及 @MessagingGateway
介面掃描(匯入),框架提供了相應的反射、代理和序列化提示。如果配置在 POJO 方法上使用了訊息註解(@ServiceActivator
、@Splitter
等),或者 POJO 方法與 IntegrationFlowBuilder.handle(Object service, String methodName)
API 一起使用,則這些方法也必須標記 @Reflective
註解,因為它們是由框架透過反射呼叫的。
Native Images 不支援 XML 配置。 |
如前所述,帶有 @MessagingGateway
註解的服務介面在被 @IntegrationComponentScan
掃描或在 @Import
註解中使用時,會由框架處理並將相應的代理提示暴露到 AOT contribution 中。當使用 IntegrationFlow.from(Class<?> serviceInterface)
API 宣告閘道器時,必須手動暴露為此類介面配置的代理
@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));
}
}
}
IntegrationFlow 的內容在 AOT 處理階段不會被處理。因此,一些提示(例如上面提到的閘道器代理提示)必須由目標應用程式手動提供。 |
當然,配置只是整合解決方案的一部分。最重要的部分是資料在網路上的傳輸以及持久化儲存。這正是序列化在許多用例中派上用場的地方。Spring Integration 為框架內部使用的以下型別向 native image 配置暴露了序列化提示:String
、Number
、Long
、Date
、ArrayList
、HashMap
、Properties
、Hashtable
、Exception
、UUID
、GenericMessage
、ErrorMessage
、MessageHeaders
、AdviceMessage
、MutableMessage
、MutableMessageHeaders
、MessageGroupMetadata
、MessageHolder
、MessageMetadata
、MessageHistory
、MessageHistory.Entry
、DelayHandler.DelayedMessageWrapper
。對於使用者特定的資料(主要以訊息 payload 的形式存在),必須透過 RuntimeHintsRegistrar
實現手動暴露序列化提示,正如上面閘道器代理所示的那樣,並使用相應的 RuntimeHints.serialization().registerType()
API。
建議使用 Spring Boot 及其相應的構建工具開發 native integration 應用程式。 |