Skip to content

Commit d96f1c7

Browse files
committed
JAVA-2378: Segregate Netty dependency from MongoClients to avoid a runtime dependency on Netty for applications that don't require it
1 parent 63cfd5b commit d96f1c7

File tree

2 files changed

+65
-38
lines changed

2 files changed

+65
-38
lines changed

driver-async/src/main/com/mongodb/async/client/MongoClients.java

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,16 @@
2929
import com.mongodb.connection.SslSettings;
3030
import com.mongodb.connection.StreamFactory;
3131
import com.mongodb.connection.StreamFactoryFactory;
32-
import com.mongodb.connection.netty.NettyStreamFactory;
3332
import com.mongodb.event.CommandEventMulticaster;
3433
import com.mongodb.event.CommandListener;
3534
import com.mongodb.management.JMXConnectionPoolListener;
36-
import io.netty.channel.EventLoopGroup;
37-
import io.netty.channel.nio.NioEventLoopGroup;
3835
import org.bson.codecs.BsonValueCodecProvider;
3936
import org.bson.codecs.DocumentCodecProvider;
4037
import org.bson.codecs.IterableCodecProvider;
4138
import org.bson.codecs.ValueCodecProvider;
4239
import org.bson.codecs.configuration.CodecRegistry;
4340

4441
import java.io.Closeable;
45-
import java.io.IOException;
4642
import java.util.List;
4743

4844
import static java.util.Arrays.asList;
@@ -94,7 +90,7 @@ public static MongoClient create(final String connectionString) {
9490
* </p>
9591
* <p>
9692
* The connection string's stream type is then applied by setting the
97-
* {@link com.mongodb.connection.StreamFactory} to an instance of {@link NettyStreamFactory},
93+
* {@link com.mongodb.connection.StreamFactory} to an instance of NettyStreamFactory,
9894
* </p>
9995
*
10096
* @param connectionString the settings
@@ -175,21 +171,28 @@ public static MongoClient create(final ConnectionString connectionString, final
175171
private static MongoClient create(final MongoClientSettings settings, final MongoDriverInformation mongoDriverInformation,
176172
final String requestedStreamType) {
177173
String streamType = getStreamType(requestedStreamType);
178-
EventLoopGroup eventLoopGroup = getEventLoopGroupIfNecessary(settings.getStreamFactoryFactory(), streamType);
179-
StreamFactory streamFactory = getStreamFactory(settings.getStreamFactoryFactory(), settings.getSocketSettings(),
180-
settings.getSslSettings(), streamType, eventLoopGroup);
181-
StreamFactory heartbeatStreamFactory = getStreamFactory(settings.getStreamFactoryFactory(), settings.getHeartbeatSocketSettings(),
182-
settings.getSslSettings(), streamType, eventLoopGroup);
174+
if (isNetty(streamType) && settings.getStreamFactoryFactory() == null) {
175+
return NettyMongoClients.create(settings, mongoDriverInformation);
176+
} else {
177+
StreamFactory streamFactory = getStreamFactory(settings.getStreamFactoryFactory(), settings.getSocketSettings(),
178+
settings.getSslSettings(), streamType);
179+
StreamFactory heartbeatStreamFactory = getStreamFactory(settings.getStreamFactoryFactory(),
180+
settings.getHeartbeatSocketSettings(), settings.getSslSettings(), streamType);
181+
return createMongoClient(settings, mongoDriverInformation, streamFactory, heartbeatStreamFactory, null);
182+
}
183+
}
184+
185+
static MongoClient createMongoClient(final MongoClientSettings settings, final MongoDriverInformation mongoDriverInformation,
186+
final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory,
187+
final Closeable externalResourceCloser) {
183188
return new MongoClientImpl(settings, new DefaultClusterFactory().create(settings.getClusterSettings(), settings.getServerSettings(),
184189
settings.getConnectionPoolSettings(), streamFactory,
185190
heartbeatStreamFactory,
186191
settings.getCredentialList(), null, new JMXConnectionPoolListener(), null,
187192
createCommandListener(settings.getCommandListeners()),
188-
settings.getApplicationName(), mongoDriverInformation),
189-
getEventLoopGroupCloser(eventLoopGroup));
193+
settings.getApplicationName(), mongoDriverInformation), externalResourceCloser);
190194
}
191195

192-
193196
/**
194197
* Gets the default codec registry. It includes the following providers:
195198
*
@@ -218,11 +221,9 @@ public static CodecRegistry getDefaultCodecRegistry() {
218221

219222
private static StreamFactory getStreamFactory(final StreamFactoryFactory streamFactoryFactory,
220223
final SocketSettings socketSettings, final SslSettings sslSettings,
221-
final String streamType, final EventLoopGroup eventLoopGroup) {
224+
final String streamType) {
222225
if (streamFactoryFactory != null) {
223226
return streamFactoryFactory.create(socketSettings, sslSettings);
224-
} else if (isNetty(streamType)) {
225-
return new NettyStreamFactory(socketSettings, sslSettings, eventLoopGroup);
226227
} else if (isNio2(streamType)) {
227228
return new AsynchronousSocketChannelStreamFactory(socketSettings, sslSettings);
228229
} else {
@@ -246,28 +247,7 @@ private static String getStreamType(final String requestedStreamType) {
246247
}
247248
}
248249

249-
private static Closeable getEventLoopGroupCloser(final EventLoopGroup eventLoopGroup) {
250-
if (eventLoopGroup == null) {
251-
return null;
252-
} else {
253-
return new Closeable() {
254-
@Override
255-
public void close() throws IOException {
256-
eventLoopGroup.shutdownGracefully().awaitUninterruptibly();
257-
}
258-
};
259-
}
260-
}
261-
private static EventLoopGroup getEventLoopGroupIfNecessary(final StreamFactoryFactory streamFactoryFactory,
262-
final String streamType) {
263-
if (isNetty(streamType) && streamFactoryFactory == null) {
264-
return new NioEventLoopGroup();
265-
} else {
266-
return null;
267-
}
268-
}
269-
270-
private static CommandListener createCommandListener(final List<CommandListener> commandListeners) {
250+
static CommandListener createCommandListener(final List<CommandListener> commandListeners) {
271251
switch (commandListeners.size()) {
272252
case 0:
273253
return null;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2016 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.async.client;
19+
20+
import com.mongodb.client.MongoDriverInformation;
21+
import com.mongodb.connection.StreamFactory;
22+
import com.mongodb.connection.netty.NettyStreamFactory;
23+
import io.netty.channel.EventLoopGroup;
24+
import io.netty.channel.nio.NioEventLoopGroup;
25+
26+
import java.io.Closeable;
27+
import java.io.IOException;
28+
29+
// Creation of MongoClient using NettyStreamFactory is segregated here to avoid a runtime dependency on Netty in MongoClients
30+
final class NettyMongoClients {
31+
static MongoClient create(final MongoClientSettings settings, final MongoDriverInformation mongoDriverInformation) {
32+
final EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
33+
StreamFactory streamFactory = new NettyStreamFactory(settings.getSocketSettings(), settings.getSslSettings(), eventLoopGroup);
34+
StreamFactory heartbeatStreamFactory = new NettyStreamFactory(settings.getHeartbeatSocketSettings(), settings.getSslSettings(),
35+
eventLoopGroup);
36+
return MongoClients.createMongoClient(settings, mongoDriverInformation, streamFactory, heartbeatStreamFactory,
37+
new Closeable() {
38+
@Override
39+
public void close() throws IOException {
40+
eventLoopGroup.shutdownGracefully().awaitUninterruptibly();
41+
}
42+
});
43+
}
44+
45+
private NettyMongoClients() {
46+
}
47+
}

0 commit comments

Comments
 (0)