|
|
@ -19,11 +19,15 @@ package org.springframework.boot.autoconfigure.mongo;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
import com.mongodb.MongoClientSettings;
|
|
|
|
import com.mongodb.MongoClientSettings;
|
|
|
|
|
|
|
|
import com.mongodb.MongoClientSettings.Builder;
|
|
|
|
import com.mongodb.connection.netty.NettyStreamFactoryFactory;
|
|
|
|
import com.mongodb.connection.netty.NettyStreamFactoryFactory;
|
|
|
|
import com.mongodb.reactivestreams.client.MongoClient;
|
|
|
|
import com.mongodb.reactivestreams.client.MongoClient;
|
|
|
|
|
|
|
|
import io.netty.channel.EventLoopGroup;
|
|
|
|
|
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
@ -60,19 +64,45 @@ public class MongoReactiveAutoConfiguration {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@ConditionalOnClass(SocketChannel.class)
|
|
|
|
@ConditionalOnClass({ SocketChannel.class, NioEventLoopGroup.class })
|
|
|
|
static class NettyDriverConfiguration {
|
|
|
|
static class NettyDriverConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
|
public MongoClientSettingsBuilderCustomizer nettyDriverCustomizer(
|
|
|
|
public NettyDriverMongoClientSettingsBuilderCustomizer nettyDriverCustomizer(
|
|
|
|
ObjectProvider<MongoClientSettings> settings) {
|
|
|
|
ObjectProvider<MongoClientSettings> settings) {
|
|
|
|
return (builder) -> {
|
|
|
|
return new NettyDriverMongoClientSettingsBuilderCustomizer(settings);
|
|
|
|
if (!isStreamFactoryFactoryDefined(settings.getIfAvailable())) {
|
|
|
|
}
|
|
|
|
builder.streamFactoryFactory(
|
|
|
|
|
|
|
|
NettyStreamFactoryFactory.builder().build());
|
|
|
|
private static final class NettyDriverMongoClientSettingsBuilderCustomizer
|
|
|
|
|
|
|
|
implements MongoClientSettingsBuilderCustomizer, DisposableBean {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final ObjectProvider<MongoClientSettings> settings;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private volatile EventLoopGroup eventLoopGroup;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private NettyDriverMongoClientSettingsBuilderCustomizer(
|
|
|
|
|
|
|
|
ObjectProvider<MongoClientSettings> settings) {
|
|
|
|
|
|
|
|
this.settings = settings;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void customize(Builder builder) {
|
|
|
|
|
|
|
|
if (!isStreamFactoryFactoryDefined(this.settings.getIfAvailable())) {
|
|
|
|
|
|
|
|
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
|
|
|
|
|
|
|
this.eventLoopGroup = eventLoopGroup;
|
|
|
|
|
|
|
|
builder.streamFactoryFactory(NettyStreamFactoryFactory.builder()
|
|
|
|
|
|
|
|
.eventLoopGroup(eventLoopGroup).build());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void destroy() {
|
|
|
|
|
|
|
|
EventLoopGroup eventLoopGroup = this.eventLoopGroup;
|
|
|
|
|
|
|
|
if (eventLoopGroup != null) {
|
|
|
|
|
|
|
|
eventLoopGroup.shutdownGracefully().awaitUninterruptibly();
|
|
|
|
|
|
|
|
this.eventLoopGroup = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isStreamFactoryFactoryDefined(MongoClientSettings settings) {
|
|
|
|
private boolean isStreamFactoryFactoryDefined(MongoClientSettings settings) {
|
|
|
@ -81,4 +111,6 @@ public class MongoReactiveAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|