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
ee7bed18
Commit
ee7bed18
authored
Mar 04, 2019
by
Dmytro Nosan
Committed by
Andy Wilkinson
Apr 02, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure that MongoClient's EventLoopGroup is shut down during context close
See gh-16087
parent
02b24b6e
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
+41
-9
MongoReactiveAutoConfigurationTests.java
...oconfigure/mongo/MongoReactiveAutoConfigurationTests.java
+11
-3
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java
View file @
ee7bed18
...
...
@@ -21,11 +21,15 @@ import java.util.stream.Collectors;
import
javax.annotation.PreDestroy
;
import
com.mongodb.MongoClientSettings
;
import
com.mongodb.MongoClientSettings.Builder
;
import
com.mongodb.connection.netty.NettyStreamFactoryFactory
;
import
com.mongodb.reactivestreams.client.MongoClient
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
reactor.core.publisher.Flux
;
import
org.springframework.beans.factory.DisposableBean
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
...
...
@@ -77,23 +81,51 @@ public class MongoReactiveAutoConfiguration {
}
@Configuration
@ConditionalOnClass
(
SocketChannel
.
class
)
@ConditionalOnClass
(
{
SocketChannel
.
class
,
NioEventLoopGroup
.
class
}
)
static
class
NettyDriverConfiguration
{
@Bean
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
)
public
MongoClientSettingsBuilderCustomizer
nettyDriverCustomizer
(
ObjectProvider
<
MongoClientSettings
>
settings
)
{
return
(
builder
)
->
{
if
(!
isStreamFactoryFactoryDefined
(
settings
.
getIfAvailable
()))
{
builder
.
streamFactoryFactory
(
NettyStreamFactoryFactory
.
builder
().
build
());
}
};
return
new
EventLoopGroupMongoClientSettingsBuilderCustomizer
(
settings
);
}
private
boolean
isStreamFactoryFactoryDefined
(
MongoClientSettings
settings
)
{
return
settings
!=
null
&&
settings
.
getStreamFactoryFactory
()
!=
null
;
private
static
final
class
EventLoopGroupMongoClientSettingsBuilderCustomizer
implements
MongoClientSettingsBuilderCustomizer
,
DisposableBean
{
private
final
ObjectProvider
<
MongoClientSettings
>
settings
;
private
EventLoopGroup
eventLoopGroup
;
private
EventLoopGroupMongoClientSettingsBuilderCustomizer
(
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 @
ee7bed18
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
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.
...
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
mongo
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicReference
;
import
com.mongodb.MongoClientSettings
;
import
com.mongodb.ReadPreference
;
...
...
@@ -25,6 +26,7 @@ import com.mongodb.connection.StreamFactory;
import
com.mongodb.connection.StreamFactoryFactory
;
import
com.mongodb.connection.netty.NettyStreamFactoryFactory
;
import
com.mongodb.reactivestreams.client.MongoClient
;
import
io.netty.channel.EventLoopGroup
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
...
...
@@ -89,11 +91,17 @@ public class MongoReactiveAutoConfigurationTests {
@Test
public
void
nettyStreamFactoryFactoryIsConfiguredAutomatically
()
{
AtomicReference
<
EventLoopGroup
>
capture
=
new
AtomicReference
<>();
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
MongoClient
.
class
);
assertThat
(
getSettings
(
context
).
getStreamFactoryFactory
())
.
isInstanceOf
(
NettyStreamFactoryFactory
.
class
);
StreamFactoryFactory
factory
=
getSettings
(
context
).
getStreamFactoryFactory
();
assertThat
(
factory
).
isInstanceOf
(
NettyStreamFactoryFactory
.
class
);
capture
.
set
((
EventLoopGroup
)
ReflectionTestUtils
.
getField
(
factory
,
"eventLoopGroup"
));
assertThat
(
capture
.
get
()).
isNotNull
();
assertThat
(
capture
.
get
().
isShutdown
()).
isFalse
();
});
assertThat
(
capture
.
get
().
isShutdown
()).
isTrue
();
}
@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