How to limit thread pool size in MongoDB async driver

Viewed 519

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.

2 Answers

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.

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

Related