gnodet-bot commented on code in PR #30547:
URL: https://github.com/apache/camel/pull/30547#discussion_r4651740853


##########
components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java:
##########
@@ -583,9 +608,200 @@ MongoClientSettings createMongoClientSettings() {
                 }
             });
         }
+        applyClientOptions(builder, connectionString);
         return builder.build();
     }
 
+    /**
+     * Applies the client options of the endpoint (replicaSet, readPreference, 
the timeouts, the pool sizes, appName,
+     * compressors and so on) on top of the connection string. An option is 
applied only when its value differs from its
+     * default, so the same option in connectionUriString is kept when the 
endpoint does not change it.
+     */
+    private void applyClientOptions(MongoClientSettings.Builder builder, 
ConnectionString connectionString) {
+        builder.applyToClusterSettings(cluster -> {
+            if (isSet(replicaSet, null)) {
+                cluster.requiredReplicaSetName(replicaSet);
+            }
+            if (isSet(serverSelectionTimeoutMS, 
DEFAULT_SERVER_SELECTION_TIMEOUT_MS)) {
+                cluster.serverSelectionTimeout(serverSelectionTimeoutMS, 
TimeUnit.MILLISECONDS);
+            }
+            if (isSet(localThresholdMS, DEFAULT_LOCAL_THRESHOLD_MS)) {
+                cluster.localThreshold(localThresholdMS, 
TimeUnit.MILLISECONDS);
+            }
+            if (isSet(srvServiceName, null)) {
+                cluster.srvServiceName(srvServiceName);
+            }
+            if (isSet(srvMaxHosts, null)) {
+                cluster.srvMaxHosts(srvMaxHosts);
+            }
+            if (loadBalanced) {
+                cluster.mode(ClusterConnectionMode.LOAD_BALANCED);
+            } else if (directConnection) {
+                cluster.mode(ClusterConnectionMode.SINGLE);
+            }
+        });
+        if (isSet(heartbeatFrequencyMS, null)) {
+            builder.applyToServerSettings(
+                    server -> server.heartbeatFrequency(heartbeatFrequencyMS, 
TimeUnit.MILLISECONDS));
+        }
+        builder.applyToSocketSettings(socket -> {
+            if (isSet(connectTimeoutMS, DEFAULT_CONNECT_TIMEOUT_MS)) {
+                socket.connectTimeout(connectTimeoutMS, TimeUnit.MILLISECONDS);
+            }
+            if (isSet(socketTimeoutMS, 0)) {
+                socket.readTimeout(socketTimeoutMS, TimeUnit.MILLISECONDS);
+            }
+        });
+        builder.applyToConnectionPoolSettings(pool -> {
+            if (isSet(maxIdleTimeMS, 0)) {
+                pool.maxConnectionIdleTime(maxIdleTimeMS, 
TimeUnit.MILLISECONDS);
+            }
+            if (isSet(maxLifeTimeMS, 0)) {
+                pool.maxConnectionLifeTime(maxLifeTimeMS, 
TimeUnit.MILLISECONDS);
+            }
+            if (isSet(minPoolSize, 0)) {
+                pool.minSize(minPoolSize);
+            }
+            if (isSet(maxPoolSize, DEFAULT_MAX_POOL_SIZE)) {
+                pool.maxSize(maxPoolSize);
+            }
+            if (isSet(maxConnecting, DEFAULT_MAX_CONNECTING)) {
+                pool.maxConnecting(maxConnecting);
+            }
+            if (isSet(waitQueueTimeoutMS, DEFAULT_WAIT_QUEUE_TIMEOUT_MS)) {
+                pool.maxWaitTime(waitQueueTimeoutMS, TimeUnit.MILLISECONDS);
+            }
+        });
+        if (isReadPreferenceSet()) {
+            builder.readPreference(createReadPreference());
+        }
+        if (isSet(appName, null)) {
+            builder.applicationName(appName);
+        }
+        if (isSet(compressors, null) || isSet(zlibCompressionLevel, null)) {
+            List<MongoCompressor> list = isSet(compressors, null)
+                    ? createCompressors(compressors) : 
connectionString.getCompressorList();
+            if (isSet(zlibCompressionLevel, null)) {
+                list = list.stream()
+                        .map(c -> "zlib".equals(c.getName())
+                                ? c.withProperty(MongoCompressor.LEVEL, 
zlibCompressionLevel) : c)
+                        .toList();
+            }
+            builder.compressorList(list);
+        }
+        if (!retryWrites) {
+            builder.retryWrites(false);
+        }
+        if (!retryReads) {
+            builder.retryReads(false);
+        }
+    }
+
+    private static boolean isSet(Object value, Object defaultValue) {
+        return value != null && !value.equals(defaultValue);
+    }
+
+    private boolean isReadPreferenceSet() {
+        return readPreference != null && 
!"PRIMARY".equalsIgnoreCase(readPreference) || isSet(readPreferenceTags, null)

Review Comment:
   Consider adding explicit parentheses around the `&&` clause to clarify 
grouping — Java precedence is correct here, but the intent reads more clearly:
   
   ```suggestion
           return (readPreference != null && 
!"PRIMARY".equalsIgnoreCase(readPreference)) || isSet(readPreferenceTags, null)
                   || isSet(maxStalenessSeconds, DEFAULT_MAX_STALENESS_SECONDS);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to