Introduce spring-core-coroutines module
This commit introduces the spring-core-coroutines module in order to avoid referencing Kotlin code from Java one, which is currently not supported by Eclipse. During the build, spring-core-coroutines is merged into spring-core, so this change is expected to have no impact for end users. This module contains functions accessible from Java via the CoroutinesUtils class to adapt Coroutines and Deferred instances to and from Mono. See gh-19975
This commit is contained in:
@@ -25,6 +25,8 @@ import java.util.function.Function;
|
||||
|
||||
import io.reactivex.BackpressureStrategy;
|
||||
import io.reactivex.Flowable;
|
||||
import kotlinx.coroutines.CompletableDeferredKt;
|
||||
import kotlinx.coroutines.Deferred;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -93,7 +95,7 @@ public class ReactiveAdapterRegistry {
|
||||
|
||||
// Coroutines
|
||||
if (ClassUtils.isPresent("kotlinx.coroutines.Deferred", classLoader)) {
|
||||
CoroutinesRegistrarKt.registerAdapter(this);
|
||||
new CoroutinesRegistrar().registerAdapters(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,4 +326,16 @@ public class ReactiveAdapterRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
private static class CoroutinesRegistrar {
|
||||
|
||||
@SuppressWarnings("KotlinInternalInJava")
|
||||
void registerAdapters(ReactiveAdapterRegistry registry) {
|
||||
registry.registerReactiveType(
|
||||
ReactiveTypeDescriptor.singleOptionalValue(Deferred.class, () -> CompletableDeferredKt.CompletableDeferred(null)),
|
||||
source -> CoroutinesUtils.deferredToMono((Deferred<?>) source),
|
||||
source -> CoroutinesUtils.monoToDeferred(Mono.from(source)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2019 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.core
|
||||
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.reactive.awaitFirstOrNull
|
||||
import kotlinx.coroutines.reactor.mono
|
||||
import reactor.core.publisher.toMono
|
||||
|
||||
/**
|
||||
* Register Reactive adapters for Coroutines types.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
internal fun registerAdapter(registry: ReactiveAdapterRegistry) {
|
||||
registry.registerReactiveType(
|
||||
ReactiveTypeDescriptor.singleOptionalValue(Deferred::class.java) { GlobalScope.async {} },
|
||||
{ source -> GlobalScope.mono { (source as Deferred<*>).await() }},
|
||||
{ source -> GlobalScope.async { source.toMono().awaitFirstOrNull() } }
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user