Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
8ebe5f99
Commit
8ebe5f99
authored
Apr 02, 2019
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.1.x'
parents
4f42597a
fe4c83a8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
12 deletions
+52
-12
MongoReactiveAutoConfiguration.java
...t/autoconfigure/mongo/MongoReactiveAutoConfiguration.java
+42
-10
MongoReactiveAutoConfigurationTests.java
...oconfigure/mongo/MongoReactiveAutoConfigurationTests.java
+10
-2
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java
View file @
8ebe5f99
...
@@ -19,11 +19,15 @@ package org.springframework.boot.autoconfigure.mongo;
...
@@ -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,23 +64,51 @@ public class MongoReactiveAutoConfiguration {
...
@@ -60,23 +64,51 @@ 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
NettyDriver
MongoClientSettingsBuilderCustomizer
nettyDriverCustomizer
(
ObjectProvider
<
MongoClientSettings
>
settings
)
{
ObjectProvider
<
MongoClientSettings
>
settings
)
{
return
(
builder
)
->
{
return
new
NettyDriverMongoClientSettingsBuilderCustomizer
(
settings
);
if
(!
isStreamFactoryFactoryDefined
(
settings
.
getIfAvailable
()))
{
builder
.
streamFactoryFactory
(
NettyStreamFactoryFactory
.
builder
().
build
());
}
};
}
}
private
boolean
isStreamFactoryFactoryDefined
(
MongoClientSettings
settings
)
{
private
static
final
class
NettyDriverMongoClientSettingsBuilderCustomizer
return
settings
!=
null
&&
settings
.
getStreamFactoryFactory
()
!=
null
;
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
)
{
return
settings
!=
null
&&
settings
.
getStreamFactoryFactory
()
!=
null
;
}
}
}
}
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java
View file @
8ebe5f99
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
mongo
;
package
org
.
springframework
.
boot
.
autoconfigure
.
mongo
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicReference
;
import
com.mongodb.MongoClientSettings
;
import
com.mongodb.MongoClientSettings
;
import
com.mongodb.ReadPreference
;
import
com.mongodb.ReadPreference
;
...
@@ -25,6 +26,7 @@ import com.mongodb.connection.StreamFactory;
...
@@ -25,6 +26,7 @@ import com.mongodb.connection.StreamFactory;
import
com.mongodb.connection.StreamFactoryFactory
;
import
com.mongodb.connection.StreamFactoryFactory
;
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
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
...
@@ -89,11 +91,17 @@ public class MongoReactiveAutoConfigurationTests {
...
@@ -89,11 +91,17 @@ public class MongoReactiveAutoConfigurationTests {
@Test
@Test
public
void
nettyStreamFactoryFactoryIsConfiguredAutomatically
()
{
public
void
nettyStreamFactoryFactoryIsConfiguredAutomatically
()
{
AtomicReference
<
EventLoopGroup
>
eventLoopGroupReference
=
new
AtomicReference
<>();
this
.
contextRunner
.
run
((
context
)
->
{
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
);
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
);
assertThat
(
getSettings
(
context
).
getStreamFactoryFactory
())
StreamFactoryFactory
factory
=
getSettings
(
context
).
getStreamFactoryFactory
();
.
isInstanceOf
(
NettyStreamFactoryFactory
.
class
);
assertThat
(
factory
).
isInstanceOf
(
NettyStreamFactoryFactory
.
class
);
EventLoopGroup
eventLoopGroup
=
(
EventLoopGroup
)
ReflectionTestUtils
.
getField
(
factory
,
"eventLoopGroup"
);
assertThat
(
eventLoopGroup
.
isShutdown
()).
isFalse
();
eventLoopGroupReference
.
set
(
eventLoopGroup
);
});
});
assertThat
(
eventLoopGroupReference
.
get
().
isShutdown
()).
isTrue
();
}
}
@Test
@Test
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment