You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/02/06 12:09:50 UTC

[GitHub] [camel] davsclaus commented on a change in pull request #5034: CAMEL-16159 Add support for nats requests.

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



##########
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) {

Review comment:
       The throws exception should be there

##########
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:
       dont catch the exception by let it fail as before as this is the synchronous process method

##########
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);
+            }
         }
 
-        LOG.debug("Publishing to topic: {}", config.getTopic());
+        if (exchange.getPattern().isOutCapable()) {
+            LOG.debug("Requesting to topic: {}", config.getTopic());
 
-        if (ObjectHelper.isNotEmpty(config.getReplySubject())) {
-            String replySubject = config.getReplySubject();
-            connection.publish(config.getTopic(), replySubject, body);
+            CompletableFuture<io.nats.client.Message> future = connection.request(config.getTopic(), body);

Review comment:
       Here is an opportunity to use correct asynchronous by switching over to Camel's process(exchange, callback) where we can the continue the callback when the completable future has a response

##########
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);
+            }
         }
 
-        LOG.debug("Publishing to topic: {}", config.getTopic());
+        if (exchange.getPattern().isOutCapable()) {
+            LOG.debug("Requesting to topic: {}", config.getTopic());
 
-        if (ObjectHelper.isNotEmpty(config.getReplySubject())) {
-            String replySubject = config.getReplySubject();
-            connection.publish(config.getTopic(), replySubject, body);
+            CompletableFuture<io.nats.client.Message> future = connection.request(config.getTopic(), body);
+            try {
+                io.nats.client.Message msg = future.get(config.getRequestCleanupInterval(), TimeUnit.MILLISECONDS);

Review comment:
       the future get will block the current thread which is not ideal




----------------------------------------------------------------
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:
users@infra.apache.org