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


##########
components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java:
##########
@@ -180,6 +187,84 @@ protected void doStart() throws Exception {
         this.connection = 
this.getEndpoint().getConfiguration().getConnection() != null
                 ? this.getEndpoint().getConfiguration().getConnection()
                 : this.getEndpoint().getConnection();
+
+        final NatsConfiguration config = this.getEndpoint().getConfiguration();
+        final ServerInfo serverInfo = this.connection.getServerInfo();
+        // a restarted producer has a new connection
+        this.jetStream = null;
+        if (config.isJetstreamEnabled() && serverInfo != null && 
serverInfo.isJetStreamAvailable()) {
+            // the JetStream context belongs to the connection, which stays 
the same object when jnats reconnects
+            this.jetStream = createJetStream(config);
+            if (ObjectHelper.isNotEmpty(config.getJetstreamName())) {
+                ensureStream(config);
+            }
+        }
+    }
+
+    private JetStream createJetStream(NatsConfiguration config) throws 
IOException {
+        // jnats uses its own request timeout (the connection timeout) for the 
acknowledgement of a publish, unless the
+        // JetStream context has one
+        final JetStreamOptions.Builder options = JetStreamOptions.builder();
+        if (config.getRequestTimeout() > 0) {
+            
options.requestTimeout(Duration.ofMillis(config.getRequestTimeout()));
+        }
+        return this.connection.jetStream(options.build());
+    }
+
+    private JetStream getJetStream(NatsConfiguration config) throws 
IOException {
+        JetStream js = this.jetStream;
+        if (js == null) {
+            // JetStream was not available when the producer started
+            js = createJetStream(config);
+            this.jetStream = js;
+        }
+        return js;
+    }

Review Comment:
   The lazy fallback in `getJetStream()` has a benign race: two concurrent 
threads can both see `js == null` simultaneously, both call 
`createJetStream()`, and the second overwrites the first. Since 
`createJetStream()` is idempotent (creates a lightweight JetStream wrapper with 
the same options, no resource to leak), this is harmless. Worth a comment so a 
future reader doesn't add unnecessary synchronization:
   
   ```suggestion
       private JetStream getJetStream(NatsConfiguration config) throws 
IOException {
           JetStream js = this.jetStream;
           if (js == null) {
               // JetStream was not available when the producer started; a 
benign race here is fine:
               // createJetStream is idempotent (lightweight context, no 
resource to leak)
               js = createJetStream(config);
               this.jetStream = js;
           }
           return js;
       }
   ```



-- 
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