oscerd commented on code in PR #26779: URL: https://github.com/apache/camel/pull/26779#discussion_r4090290903
########## components/camel-pulsar/src/test/java/org/apache/camel/component/pulsar/PulsarMessageListenerAcknowledgementTest.java: ########## @@ -0,0 +1,168 @@ +/* + * 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.pulsar; + +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.ExceptionHandler; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.pulsar.client.api.Consumer; +import org.apache.pulsar.client.api.ConsumerBuilder; +import org.apache.pulsar.client.api.Message; +import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.api.PulsarClientException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Answers.RETURNS_SELF; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * A route failure used to leave the message neither acknowledged nor negatively acknowledged, so it was only + * redelivered once the acknowledgement timeout expired (CAMEL-16073), and a failing acknowledgement was reported + * without its cause. + */ +public class PulsarMessageListenerAcknowledgementTest extends CamelTestSupport { + + private static final String ENDPOINT_URI + = "pulsar:persistent://public/default/camel-ack-test?subscriptionName=camel-subscription"; + + private final MessageId messageId = mock(MessageId.class); + + @Test + public void testSuccessfulExchangeIsAcknowledged() throws Exception { + final Consumer<byte[]> pulsarConsumer = mock(Consumer.class); + + listener().received(pulsarConsumer, message("ok")); + + verify(pulsarConsumer, timeout(5000)).acknowledge(messageId); Review Comment: Agreed, and added in a46191 — `testManualAcknowledgementLeavesTheFailedMessageToTheRoute` flips `allowManualAcknowledgement` on the endpoint configuration, drives a failing exchange through the listener, and asserts nothing at all is sent to the broker (neither overload of `negativeAcknowledge`, nor `acknowledge`). It waits on the exception handler first so the callback has demonstrably run before asserting the absence. Removing the guard from `negativeAcknowledge` fails that test, so it is load-bearing rather than passing by construction. _Claude Code on behalf of oscerd_ ########## docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc: ########## @@ -2580,3 +2580,17 @@ Routes that reference the constants (for example `setHeader(MustacheConstants.MU are unaffected. Routes that set the header by its literal string name, or that use `allowTemplateFromHeader=true` with the old header names, must switch to the new `Camel`-prefixed names. + +=== camel-pulsar - a failed exchange is negatively acknowledged + +When a route fails, the consumer now calls `negativeAcknowledge` on the Pulsar consumer instead of +leaving the message unacknowledged. This only applies when `allowManualAcknowledgement` is `false` +(the default); with manual acknowledgement the route stays in charge, as before. + +This changes when the message comes back. Previously it was redelivered once the acknowledgement +timeout expired, which `camel-pulsar` sets to 10 seconds by default through `ackTimeoutMillis`. A +negative acknowledgement removes the message from the client's unacknowledged-message tracker, so +redelivery now follows `negativeAckRedeliveryDelayMicros`, which defaults to 60 seconds, and honours Review Comment: Correct — and the sentence is now true: the listener passes the `Message` as of a46191. For the record of why it matters, from the `NegativeAcksTracker` bytecode in pulsar-client 4.2.4: ``` add(MessageId) -> add(messageId, 0) add(Message) -> add(message.getMessageId(), message.getRedeliveryCount()) add(MessageId, int redeliveryCount) -> negativeAckRedeliveryBackoff.next(redeliveryCount) ``` With the id overload the backoff is consulted but always asked for the delay of attempt zero, so it never escalates. _Claude Code on behalf of oscerd_ -- 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]
