davsclaus commented on a change in pull request #5034:
URL: https://github.com/apache/camel/pull/5034#discussion_r571632550



##########
File path: 
components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java
##########
@@ -17,19 +17,29 @@
 package org.apache.camel.component.nats;
 
 import java.time.Duration;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import io.nats.client.Connection;
 import io.nats.client.Connection.Status;
+import io.nats.client.Message;
+import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
-import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.support.DefaultAsyncProducer;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NatsProducer extends DefaultProducer {
+public class NatsProducer extends DefaultAsyncProducer {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(NatsProducer.class);
 
+    private final ScheduledExecutorService scheduler = 
Executors.newScheduledThreadPool(1);

Review comment:
       We should use Camle's ExecutorStrategy to get a thread pool so its 
properly started/stopped etc. Also it may be desiredable to allow to configure 
a higher pool size, in case there is a lot of timed out messages. Otherwise the 
tasks is piling up as its the timeout future thread that will be used for 
continued routing.

##########
File path: 
components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsProducerReplyToTimeoutTest.java
##########
@@ -0,0 +1,59 @@
+/*
+ * 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;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+public class NatsProducerReplyToTimeoutTest extends NatsTestSupport {
+
+    protected String startUri = "direct:start";
+    protected String middleUri = "nats:foo.middle?requestCleanupInterval=50";

Review comment:
       I think we should use `requestTimeout` that is what the other messaging 
component uses. And it has a default timeout of 20 seconds. So this means 
adding a new option to use that. As that option is some sort of internal job by 
nats to cleanup stale jobs.

##########
File path: 
components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java
##########
@@ -42,21 +47,42 @@ public NatsEndpoint getEndpoint() {
     }
 
     @Override
-    public void process(Exchange exchange) throws Exception {
+    public void process(Exchange exchange) {
         NatsConfiguration config = getEndpoint().getConfiguration();
         byte[] body = exchange.getIn().getBody(byte[].class);
         if (body == null) {
-            // fallback to use string
-            body = exchange.getIn().getMandatoryBody(String.class).getBytes();
+            try {
+                // fallback to use string
+                body = 
exchange.getIn().getMandatoryBody(String.class).getBytes();
+            } catch (InvalidPayloadException e) {
+                exchange.setException(e);

Review comment:
       But that is how the old code was, so it may be something for another PR

##########
File path: 
components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsProducer.java
##########
@@ -42,21 +47,42 @@ public NatsEndpoint getEndpoint() {
     }
 
     @Override
-    public void process(Exchange exchange) throws Exception {
+    public void process(Exchange exchange) {
         NatsConfiguration config = getEndpoint().getConfiguration();
         byte[] body = exchange.getIn().getBody(byte[].class);
         if (body == null) {
-            // fallback to use string
-            body = exchange.getIn().getMandatoryBody(String.class).getBytes();
+            try {
+                // fallback to use string
+                body = 
exchange.getIn().getMandatoryBody(String.class).getBytes();
+            } catch (InvalidPayloadException e) {
+                exchange.setException(e);

Review comment:
       yes but it would actually maybe be better to convert to byte[] directly 
instead of via string as that is double encoding.




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

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


Reply via email to