This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ff9d8e507f26 CAMEL-27033: camel-alibaba-mns - use the client that
initClient returns (#30218)
ff9d8e507f26 is described below
commit ff9d8e507f26a65f4e17617dca0881853e2cfa71
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Mon Nov 16 21:56:43 2026 +0000
CAMEL-27033: camel-alibaba-mns - use the client that initClient returns
(#30218)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/alibaba/mns/MNSConsumer.java | 5 +-
.../camel/component/alibaba/mns/MNSEndpoint.java | 21 +--
.../camel/component/alibaba/mns/MNSProducer.java | 21 +--
.../component/alibaba/mns/MNSEndpointTest.java | 5 +-
.../component/alibaba/mns/MNSInitClientTest.java | 159 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 6 +
6 files changed, 193 insertions(+), 24 deletions(-)
diff --git
a/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSConsumer.java
b/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSConsumer.java
index e34f4174591c..cc34a4471b5a 100644
---
a/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSConsumer.java
+++
b/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSConsumer.java
@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Queue;
import com.aliyun.mns.client.CloudQueue;
+import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.Message;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
@@ -49,9 +50,9 @@ public class MNSConsumer extends
ScheduledBatchPollingConsumer {
pendingExchanges = 0;
MNSEndpoint endpoint = getEndpoint();
- endpoint.initClient();
+ MNSClient client = endpoint.initClient();
- CloudQueue queue =
endpoint.getMnsClient().getQueueRef(endpoint.getQueueName());
+ CloudQueue queue = client.getQueueRef(endpoint.getQueueName());
List<Message> messages = receiveMessages(queue,
endpoint.getMaxMessagesPerPoll(), endpoint.getWaitSeconds());
forceConsumerAsReady();
diff --git
a/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSEndpoint.java
b/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSEndpoint.java
index 1add61f4a698..5af9b5739ab5 100644
---
a/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSEndpoint.java
+++
b/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSEndpoint.java
@@ -136,23 +136,26 @@ public class MNSEndpoint extends ScheduledPollEndpoint {
}
/**
- * Makes sure that this endpoint has the client that the producer and the
consumer send their requests with, which
- * they then get with {@link #getMnsClient()}. If no client is set or
autowired, the first call creates it from the
- * options of this endpoint, while it holds the lock of this endpoint: the
calls made at the same time wait for this
- * creation, and the calls made once the client exists do not take the
lock. A failed creation keeps no client, so
- * each call that waited for it, and each later call, tries again.
+ * Returns the client that the producer and the consumer send their
requests with. If no client is set or autowired,
+ * the first call creates it from the options of this endpoint, while it
holds the lock of this endpoint: the calls
+ * made at the same time wait for this creation, and the calls made once
the client exists do not take the lock. A
+ * failed creation keeps no client, so each call that waited for it, and
each later call, tries again.
*/
- public void initClient() {
- if (mnsClient == null) {
+ public MNSClient initClient() {
+ MNSClient answer = mnsClient;
+ if (answer == null) {
lock.lock();
try {
- if (mnsClient == null) {
- mnsClient = createClient();
+ answer = mnsClient;
+ if (answer == null) {
+ answer = createClient();
+ mnsClient = answer;
}
} finally {
lock.unlock();
}
}
+ return answer;
}
MNSClient createClient() {
diff --git
a/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSProducer.java
b/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSProducer.java
index 46bd1d394cf0..c7ccffbb596e 100644
---
a/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSProducer.java
+++
b/components/camel-alibaba/camel-alibaba-mns/src/main/java/org/apache/camel/component/alibaba/mns/MNSProducer.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.alibaba.mns;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.CloudTopic;
+import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.Base64TopicMessage;
import com.aliyun.mns.model.BaseMessage;
import com.aliyun.mns.model.Message;
@@ -43,23 +44,23 @@ public class MNSProducer extends DefaultProducer {
@Override
public void process(Exchange exchange) throws Exception {
MNSEndpoint endpoint = getEndpoint();
- endpoint.initClient();
+ MNSClient client = endpoint.initClient();
ClientConfigurations configuration =
MNSUtils.createClientConfigurations(endpoint, exchange);
String operation = configuration.operation();
switch (operation) {
- case MNSOperations.SEND_MESSAGE -> sendMessage(endpoint, exchange,
configuration);
- case MNSOperations.DELETE_MESSAGE -> deleteMessage(endpoint,
exchange, configuration);
- case MNSOperations.PUBLISH_MESSAGE -> publishMessage(endpoint,
exchange, configuration);
+ case MNSOperations.SEND_MESSAGE -> sendMessage(client, exchange,
configuration);
+ case MNSOperations.DELETE_MESSAGE -> deleteMessage(client,
exchange, configuration);
+ case MNSOperations.PUBLISH_MESSAGE -> publishMessage(client,
exchange, configuration);
case MNSOperations.RECEIVE_MESSAGE -> throw new
UnsupportedOperationException(
"receiveMessage is not a producer operation: the consumer
receives the messages of the queue");
default -> throw new UnsupportedOperationException("Unsupported
operation: " + operation);
}
}
- private void sendMessage(MNSEndpoint endpoint, Exchange exchange,
ClientConfigurations configuration) throws Exception {
- CloudQueue queue =
endpoint.getMnsClient().getQueueRef(configuration.queueName());
+ private void sendMessage(MNSClient client, Exchange exchange,
ClientConfigurations configuration) throws Exception {
+ CloudQueue queue = client.getQueueRef(configuration.queueName());
Message message = new Message();
MNSUtils.setMessageBody(exchange, message);
@@ -77,23 +78,23 @@ public class MNSProducer extends DefaultProducer {
setMessageResponse(exchange, response);
}
- private void deleteMessage(MNSEndpoint endpoint, Exchange exchange,
ClientConfigurations configuration) throws Exception {
+ private void deleteMessage(MNSClient client, Exchange exchange,
ClientConfigurations configuration) throws Exception {
String receiptHandle = MNSUtils.resolveReceiptHandle(exchange);
if (ObjectHelper.isEmpty(receiptHandle)) {
throw new IllegalArgumentException("Receipt handle is required for
deleteMessage operation");
}
- CloudQueue queue =
endpoint.getMnsClient().getQueueRef(configuration.queueName());
+ CloudQueue queue = client.getQueueRef(configuration.queueName());
queue.deleteMessage(receiptHandle);
}
- private void publishMessage(MNSEndpoint endpoint, Exchange exchange,
ClientConfigurations configuration) throws Exception {
+ private void publishMessage(MNSClient client, Exchange exchange,
ClientConfigurations configuration) throws Exception {
String topic = configuration.topicName();
if (ObjectHelper.isEmpty(topic)) {
throw new IllegalArgumentException("Topic name is required for
publishMessage operation");
}
- CloudTopic cloudTopic = endpoint.getMnsClient().getTopicRef(topic);
+ CloudTopic cloudTopic = client.getTopicRef(topic);
TopicMessage topicMessage = new Base64TopicMessage();
MNSUtils.setMessageBody(exchange, topicMessage);
diff --git
a/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSEndpointTest.java
b/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSEndpointTest.java
index f2c79fdf5a95..56eef8201171 100644
---
a/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSEndpointTest.java
+++
b/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSEndpointTest.java
@@ -99,9 +99,8 @@ class MNSEndpointTest {
for (int i = 0; i < callers; i++) {
results.add(executor.submit(() -> {
threads.add(Thread.currentThread());
- // as the producer and the consumer do, which then get the
client from the endpoint
- endpoint.initClient();
- return endpoint.getMnsClient();
+ // as the producer and the consumer do, which send their
requests with the client it returns
+ return endpoint.initClient();
}));
}
// a creation is held until each caller has started one too, or
waits for the lock of a caller that has,
diff --git
a/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSInitClientTest.java
b/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSInitClientTest.java
new file mode 100644
index 000000000000..8df51c32bd27
--- /dev/null
+++
b/components/camel-alibaba/camel-alibaba-mns/src/test/java/org/apache/camel/component/alibaba/mns/MNSInitClientTest.java
@@ -0,0 +1,159 @@
+/*
+ * 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.alibaba.mns;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import com.aliyun.mns.client.CloudQueue;
+import com.aliyun.mns.client.CloudTopic;
+import com.aliyun.mns.client.MNSClient;
+import com.aliyun.mns.model.Message;
+import com.aliyun.mns.model.TopicMessage;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.alibaba.mns.constants.MNSHeaders;
+import org.apache.camel.component.alibaba.mns.constants.MNSOperations;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The producer and the poll of the consumer send their requests with the
client that {@link MNSEndpoint#initClient()}
+ * returns. A stop of the endpoint closes its client and clears the field, so
a later getMnsClient() could give null.
+ */
+class MNSInitClientTest {
+
+ private final TestConfiguration testConfiguration = new
TestConfiguration();
+ private final String queueName = testConfiguration.getProperty("queue");
+ private final String topicName = testConfiguration.getProperty("topic");
+ private final MNSClient client = mock(MNSClient.class);
+ private final CloudQueue cloudQueue = mock(CloudQueue.class);
+ private final CloudTopic cloudTopic = mock(CloudTopic.class);
+ private final List<Exchange> received = new CopyOnWriteArrayList<>();
+ private CamelContext context;
+
+ @BeforeEach
+ void startContext() {
+ context = new DefaultCamelContext();
+ context.start();
+ when(client.getQueueRef(queueName)).thenReturn(cloudQueue);
+ when(client.getTopicRef(topicName)).thenReturn(cloudTopic);
+ }
+
+ @AfterEach
+ void stopContext() {
+ context.stop();
+ }
+
+ @Test
+ void sendMessageUsesTheClientThatInitClientReturns() throws Exception {
+ new
MNSProducer(endpointThatLosesItsClient()).process(exchange(MNSOperations.SEND_MESSAGE,
"hello"));
+
+ verify(cloudQueue).putMessage(any(Message.class));
+ }
+
+ @Test
+ void deleteMessageUsesTheClientThatInitClientReturns() throws Exception {
+ Exchange exchange = exchange(MNSOperations.DELETE_MESSAGE, null);
+ exchange.getIn().setHeader(MNSHeaders.RECEIPT_HANDLE, "receipt-1");
+
+ new MNSProducer(endpointThatLosesItsClient()).process(exchange);
+
+ verify(cloudQueue).deleteMessage("receipt-1");
+ }
+
+ @Test
+ void publishMessageUsesTheClientThatInitClientReturns() throws Exception {
+ new
MNSProducer(endpointThatLosesItsClient()).process(exchange(MNSOperations.PUBLISH_MESSAGE,
"hello"));
+
+ verify(cloudTopic).publishMessage(any(TopicMessage.class));
+ }
+
+ @Test
+ void pollUsesTheClientThatInitClientReturns() throws Exception {
+ when(cloudQueue.popMessage()).thenReturn(message("receipt-1"));
+ MNSConsumer consumer = startedConsumer(endpointThatLosesItsClient());
+ try {
+ assertThat(consumer.poll()).isEqualTo(1);
+
+ assertThat(received).hasSize(1);
+ assertThat(received.get(0).getIn().getBody()).isEqualTo("hello");
+ } finally {
+ consumer.stop();
+ }
+ }
+
+ /**
+ * An endpoint that creates the mock client, but whose getMnsClient()
returns null, as it would after a stop between
+ * initClient and a later getMnsClient().
+ */
+ private MNSEndpoint endpointThatLosesItsClient() {
+ MNSEndpoint endpoint
+ = new MNSEndpoint("alibaba-mns:" + queueName,
context.getComponent("alibaba-mns", MNSComponent.class)) {
+ @Override
+ MNSClient createClient() {
+ return client;
+ }
+
+ @Override
+ public MNSClient getMnsClient() {
+ return null;
+ }
+ };
+ configure(endpoint);
+ return endpoint;
+ }
+
+ private void configure(MNSEndpoint endpoint) {
+ endpoint.setQueueName(queueName);
+ endpoint.setTopicName(topicName);
+ endpoint.setAccessKey(testConfiguration.getProperty("accessKey"));
+ endpoint.setSecretKey(testConfiguration.getProperty("secretKey"));
+ }
+
+ /**
+ * A consumer whose scheduler does not start, so that only the test polls.
+ */
+ private MNSConsumer startedConsumer(MNSEndpoint endpoint) throws Exception
{
+ MNSConsumer consumer = (MNSConsumer)
endpoint.createConsumer(received::add);
+ consumer.setStartScheduler(false);
+ consumer.start();
+ return consumer;
+ }
+
+ private Exchange exchange(String operation, Object body) {
+ Exchange exchange = new DefaultExchange(context);
+ exchange.getIn().setHeader(MNSHeaders.OPERATION, operation);
+ exchange.getIn().setBody(body);
+ return exchange;
+ }
+
+ private static Message message(String receiptHandle) {
+ Message message = new Message("hello");
+ message.setReceiptHandle(receiptHandle);
+ return message;
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 6044bcad5c57..bceec2f62a6e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -6192,6 +6192,12 @@ which receives the messages of a queue, does not use the
option. A producer with
fails, now with a message that says so. Remove `operation=receiveMessage` from
consumer endpoints, which tooling that
validates endpoint options now reports.
+=== camel-alibaba-mns - MNSEndpoint.initClient returns the client
+
+The public method `MNSEndpoint.initClient()` now returns the `MNSClient` that
the producer and the consumer send their
+requests with, instead of `void`. A call that ignores the result compiles as
before, but code compiled against an
+earlier version must be recompiled, and a subclass that overrides
`initClient()` must return the client.
+
=== camel-minio - a listed object that cannot be got is skipped
When the consumer lists the bucket, a failure to stat or get one of the listed
objects (for example access denied, or