bvahdat commented on code in PR #18212:
URL: https://github.com/apache/camel/pull/18212#discussion_r2126027187


##########
components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java:
##########
@@ -123,37 +131,88 @@ class NatsConsumingTask implements Runnable {
         @Override
         public void run() {
             try {
-                NatsConsumer.this.dispatcher = 
this.connection.createDispatcher(new CamelNatsMessageHandler());
-                if 
(ObjectHelper.isNotEmpty(this.configuration.getQueueName())) {
-                    NatsConsumer.this.dispatcher = 
NatsConsumer.this.dispatcher.subscribe(
-                            
NatsConsumer.this.getEndpoint().getConfiguration().getTopic(),
-                            
NatsConsumer.this.getEndpoint().getConfiguration().getQueueName());
-                    if 
(ObjectHelper.isNotEmpty(NatsConsumer.this.getEndpoint().getConfiguration().getMaxMessages()))
 {
-                        NatsConsumer.this.dispatcher.unsubscribe(
-                                
NatsConsumer.this.getEndpoint().getConfiguration().getTopic(),
-                                
Integer.parseInt(NatsConsumer.this.getEndpoint().getConfiguration().getMaxMessages()));
-                    }
-                    if (NatsConsumer.this.dispatcher.isActive()) {
-                        NatsConsumer.this.setActive(true);
-                    }
+                NatsConfiguration config = getEndpoint().getConfiguration();
+                String topic = config.getTopic();
+                String queueName = config.getQueueName();
+                String maxMessagesStr = config.getMaxMessages();
+                Integer maxMessages = null;
+                if (ObjectHelper.isNotEmpty(maxMessagesStr)) {
+                    maxMessages = Integer.parseInt(maxMessagesStr);
+                }
+
+                if (config.isJetstreamEnabled() && 
connection.getServerInfo().isJetStreamAvailable()) {
+                    setupJetStreamConsumer(topic, queueName);
                 } else {
-                    NatsConsumer.this.dispatcher = NatsConsumer.this.dispatcher
-                            
.subscribe(NatsConsumer.this.getEndpoint().getConfiguration().getTopic());
-                    if 
(ObjectHelper.isNotEmpty(NatsConsumer.this.getEndpoint().getConfiguration().getMaxMessages()))
 {
-                        NatsConsumer.this.dispatcher.unsubscribe(
-                                
NatsConsumer.this.getEndpoint().getConfiguration().getTopic(),
-                                
Integer.parseInt(NatsConsumer.this.getEndpoint().getConfiguration().getMaxMessages()));
-                    }
-                    if (NatsConsumer.this.dispatcher.isActive()) {
-                        NatsConsumer.this.setActive(true);
-                    }
+                    setupStandardNatsConsumer(topic, queueName, maxMessages);
                 }
             } catch (final Exception e) {
                 NatsConsumer.this.getExceptionHandler().handleException("Error 
during processing", e);
             }
 
         }
 
+        private void setupJetStreamConsumer(String topic, String queueName) 
throws IOException, JetStreamApiException {
+            String streamName = this.configuration.getJetstreamName();
+            String consumerName
+                    = ObjectHelper.isNotEmpty(queueName) ? queueName : 
"consumer-" + System.currentTimeMillis(); // Generate a default consumer name 
if queueName is not provided
+            LOG.debug("Setting up JetStream PUSH consumer for stream: '{}', 
durable: '{}', topic: {} ", streamName,
+                    consumerName, this.configuration.getTopic());
+
+            JetStreamManagement jsm = connection.jetStreamManagement();
+            try {
+                // Try to get the stream, create it if it doesn't exist

Review Comment:
   Sorry for my late comment on something being already merged, but instead of 
trying to make use of a (potentially) thrown Exception as the flow control for 
the logic:
   
   https://ahdak.github.io/blog/effective-java-part-9/
   
   Can't we make use of another API to find it out beforehand if stream 
available, and then create if required? Maybe using?
   
   [JetStreamManagement#getStreams(String 
subjectFilter)](https://github.com/nats-io/nats.java/blob/b1b12b4194f479dae3cfed4e130e64a3ebcbee45/src/main/java/io/nats/client/JetStreamManagement.java#L250)



-- 
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: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to