|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2018 the original author or authors. |
| 2 | + * Copyright 2012-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import javax.annotation.PreDestroy;
|
22 | 22 |
|
23 | 23 | import com.mongodb.MongoClientSettings;
|
| 24 | +import com.mongodb.MongoClientSettings.Builder; |
24 | 25 | import com.mongodb.connection.netty.NettyStreamFactoryFactory;
|
25 | 26 | import com.mongodb.reactivestreams.client.MongoClient;
|
| 27 | +import io.netty.channel.EventLoopGroup; |
| 28 | +import io.netty.channel.nio.NioEventLoopGroup; |
26 | 29 | import io.netty.channel.socket.SocketChannel;
|
27 | 30 | import reactor.core.publisher.Flux;
|
28 | 31 |
|
| 32 | +import org.springframework.beans.factory.DisposableBean; |
29 | 33 | import org.springframework.beans.factory.ObjectProvider;
|
30 | 34 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
31 | 35 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
@@ -77,23 +81,51 @@ public MongoClient reactiveStreamsMongoClient(MongoProperties properties,
|
77 | 81 | }
|
78 | 82 |
|
79 | 83 | @Configuration
|
80 |
| - @ConditionalOnClass(SocketChannel.class) |
| 84 | + @ConditionalOnClass({ SocketChannel.class, NioEventLoopGroup.class }) |
81 | 85 | static class NettyDriverConfiguration {
|
82 | 86 |
|
83 | 87 | @Bean
|
84 | 88 | @Order(Ordered.HIGHEST_PRECEDENCE)
|
85 |
| - public MongoClientSettingsBuilderCustomizer nettyDriverCustomizer( |
| 89 | + public NettyDriverMongoClientSettingsBuilderCustomizer nettyDriverCustomizer( |
86 | 90 | ObjectProvider<MongoClientSettings> settings) {
|
87 |
| - return (builder) -> { |
88 |
| - if (!isStreamFactoryFactoryDefined(settings.getIfAvailable())) { |
89 |
| - builder.streamFactoryFactory( |
90 |
| - NettyStreamFactoryFactory.builder().build()); |
91 |
| - } |
92 |
| - }; |
| 91 | + return new NettyDriverMongoClientSettingsBuilderCustomizer(settings); |
93 | 92 | }
|
94 | 93 |
|
95 |
| - private boolean isStreamFactoryFactoryDefined(MongoClientSettings settings) { |
96 |
| - return settings != null && settings.getStreamFactoryFactory() != null; |
| 94 | + private static final class NettyDriverMongoClientSettingsBuilderCustomizer |
| 95 | + implements MongoClientSettingsBuilderCustomizer, DisposableBean { |
| 96 | + |
| 97 | + private final ObjectProvider<MongoClientSettings> settings; |
| 98 | + |
| 99 | + private volatile EventLoopGroup eventLoopGroup; |
| 100 | + |
| 101 | + private NettyDriverMongoClientSettingsBuilderCustomizer( |
| 102 | + ObjectProvider<MongoClientSettings> settings) { |
| 103 | + this.settings = settings; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void customize(Builder builder) { |
| 108 | + if (!isStreamFactoryFactoryDefined(this.settings.getIfAvailable())) { |
| 109 | + NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(); |
| 110 | + this.eventLoopGroup = eventLoopGroup; |
| 111 | + builder.streamFactoryFactory(NettyStreamFactoryFactory.builder() |
| 112 | + .eventLoopGroup(eventLoopGroup).build()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void destroy() { |
| 118 | + EventLoopGroup eventLoopGroup = this.eventLoopGroup; |
| 119 | + if (eventLoopGroup != null) { |
| 120 | + eventLoopGroup.shutdownGracefully().awaitUninterruptibly(); |
| 121 | + this.eventLoopGroup = null; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private boolean isStreamFactoryFactoryDefined(MongoClientSettings settings) { |
| 126 | + return settings != null && settings.getStreamFactoryFactory() != null; |
| 127 | + } |
| 128 | + |
97 | 129 | }
|
98 | 130 |
|
99 | 131 | }
|
|
0 commit comments