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


##########
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:
   https://github.com/apache/camel/pull/18274



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