Étienne Miret
Étienne Miret

Reputation: 6660

How to limit thread pool size in MongoDB async driver

According to JAVA-2561, it is possible to limit the number of threads created by the MongoDB async driver since release 3.6.

However, I couldn’t find any documentation about how to do it.

Upvotes: 2

Views: 1018

Answers (2)

Étienne Miret
Étienne Miret

Reputation: 6660

After inspecting the commit linked by D. SM, I could find out that you need to specify an AsynchronousChannelGroup when creating the AsynchronousSocketChannelStreamFactoryFactory, which in turn you provide to the client settings:

var channelGroup = AsynchronousChannelGroup.withFixedThreadPool (10, Thread::new);
return MongoClients.create (MongoClientSettings.builder ()
    .streamFactoryFactory (AsynchronousSocketChannelStreamFactoryFactory.builder ()
        .group (channelGroup)
        .build ())
    .build ());

AsynchronousChannelGroup has several static factory methods, use the one that best match your needs.

With the 4.0.4 MongoDB driver (at least), in case no group is specified, the JVM system-wide default group is used, which is unbounded.

Upvotes: 4

D. SM
D. SM

Reputation: 14520

The work appears to have been done in this commit. Hope this helps.

Upvotes: 1

Related Questions