oscerd commented on code in PR #23144:
URL: https://github.com/apache/camel/pull/23144#discussion_r3225642352


##########
components/camel-nats/src/test/java/org/apache/camel/component/nats/jetstream/NatsJetstreamConsumerManualAckIT.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.nats.jetstream;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.nats.NatsConstants;
+import org.apache.camel.component.nats.NatsManualAck;
+import org.apache.camel.component.nats.integration.NatsITSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+@Isolated
+public class NatsJetstreamConsumerManualAckIT extends NatsITSupport {
+
+    @EndpointInject("mock:result")
+    protected MockEndpoint mockResultEndpoint;
+
+    @EndpointInject("mock:input")
+    protected MockEndpoint mockInputEndpoint;
+
+    @Test
+    public void testManualAck() throws Exception {
+        mockResultEndpoint.expectedBodiesReceived("Hello World");
+        mockResultEndpoint.expectedHeaderReceived(NatsConstants.NATS_SUBJECT, 
"mytopic-manualack");
+
+        template.sendBody("direct:send", "Hello World");
+
+        mockResultEndpoint.setAssertPeriod(5000);
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Test
+    public void testManualNak() throws Exception {
+        mockResultEndpoint.expectedBodiesReceived("Hello World");
+        mockResultEndpoint.expectedHeaderReceived("counter", 2);
+        
mockResultEndpoint.expectedHeaderReceived(NatsConstants.NATS_DELIVERY_COUNTER, 
2);
+
+        mockInputEndpoint.expectedMessageCount(2);
+        
mockInputEndpoint.message(0).header(NatsConstants.NATS_DELIVERY_COUNTER).isEqualTo(1);
+        
mockInputEndpoint.message(1).header(NatsConstants.NATS_DELIVERY_COUNTER).isEqualTo(2);
+
+        template.sendBody("direct:send-nak", "Hello World");
+
+        mockResultEndpoint.setAssertPeriod(5000);
+        mockInputEndpoint.setAssertPeriod(5000);
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                String ackUri
+                        = 
"nats:mytopic-manualack?jetstreamEnabled=true&jetstreamName=mystream-manualack&jetstreamAsync=false&durableName=camel-manualack&pullSubscription=false&manualAck=true";
+                String nakUri
+                        = 
"nats:mytopic-manualack-nak?jetstreamEnabled=true&jetstreamName=mystream-manualack-nak&jetstreamAsync=false&durableName=camel-manualack-nak&pullSubscription=false&manualAck=true&nackWait=10";
+
+                from("direct:send")
+                        
.errorHandler(defaultErrorHandler().maximumRedeliveries(5))
+                        .to(ackUri);
+
+                from(ackUri)
+                        .process(new Processor() {
+                            @Override
+                            public void process(Exchange exchange) throws 
Exception {
+                                NatsManualAck manualAck
+                                        = 
exchange.getIn().getHeader(NatsConstants.NATS_MANUAL_ACK, NatsManualAck.class);
+                                assertNotNull(manualAck);
+                                assertInstanceOf(NatsManualAck.class, 
manualAck);

Review Comment:
   Nit: this assertion is redundant — `manualAck` is already typed as 
`NatsManualAck` by the typed `getHeader(..., NatsManualAck.class)` call above, 
so the cast cannot succeed with a different type. The preceding 
`assertNotNull(manualAck)` is sufficient; the `assertInstanceOf` import (line 
32) can be removed too.
   
   ```suggestion
   ```



##########
components/camel-nats/src/main/docs/nats-component.adoc:
##########
@@ -125,5 +125,28 @@ from("nats:mytopic?maxMessages=5&queueName=myqueue")
   .to("mock:result");
 ----
 
+=== Manual Acknowledgment (JetStream)
+
+When consuming from JetStream, by default messages are automatically 
acknowledged
+after successful route processing, or negatively acknowledged (redelivered) on 
failure.
+
+To take full control of acknowledgment, set `manualAck=true` on the consumer 
endpoint.
+This disables automatic acknowledgment and exposes a `NatsManualAck` object as 
the
+`CamelNatsManualAck` message header.
+
+[source,java]
+----
+from("nats:mytopic?jetstreamEnabled=true&jetstreamName=mystream&durableName=myconsumer&pullSubscription=false&manualAck=true")
+    .process(exchange -> {
+        // do work ...
+
+        NatsManualAck manualAck = 
exchange.getIn().getHeader("CamelNatsManualAck", NatsManualAck.class);

Review Comment:
   Minor: prefer the constant over the string literal — matches how camel-kafka 
docs reference `KafkaConstants.MANUAL_COMMIT` and keeps the doc example aligned 
with `NatsConstants.NATS_MANUAL_ACK`.
   
   ```suggestion
           NatsManualAck manualAck = 
exchange.getIn().getHeader(NatsConstants.NATS_MANUAL_ACK, NatsManualAck.class);
   ```



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