dmvolod commented on a change in pull request #4508:
URL: https://github.com/apache/camel/pull/4508#discussion_r511732925



##########
File path: 
components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java
##########
@@ -353,15 +356,33 @@ public void setFlowControlWindow(int flowControlWindow) {
         this.flowControlWindow = flowControlWindow;
     }
 
+    public int getMaxMessageSize() {
+        return maxMessageSize;
+    }
+
     /**
      * The maximum message size allowed to be received/sent (MiB)
      */
     public void setMaxMessageSize(int maxMessageSize) {
         this.maxMessageSize = maxMessageSize;
     }
 
-    public int getMaxMessageSize() {
-        return maxMessageSize;
+    /**
+     * Lets the camel route to take control over stream observer. If this 
value is set to true, then the stream

Review comment:
       Camel must be in CamelCase as it's project name but also redundant 
:smile:
   Also name of the property must be documented or separate micro example can 
be provided.
   Please don't forget about constant described below. Info about default false 
value as also redundant as it always false by default.

##########
File path: 
components/camel-grpc/src/main/java/org/apache/camel/component/grpc/server/GrpcMethodHandler.java
##########
@@ -39,19 +39,30 @@ public GrpcMethodHandler(GrpcConsumer consumer) {
         this.consumer = consumer;
     }
 
+    /**
+     * This method deals with the unary and server streaming gRPC calls
+     *
+     * @param  body             The request object sent by the gRPC client to 
the server
+     * @param  responseObserver The response stream observer
+     * @param  methodName       The name of the method invoked using the stub.
+     * @throws Exception        java.lang.Exception
+     */
     public void handle(Object body, StreamObserver<Object> responseObserver, 
String methodName) throws Exception {
         Map<String, Object> grcpHeaders = populateGrpcHeaders(methodName);
         GrpcEndpoint endpoint = (GrpcEndpoint) consumer.getEndpoint();
+
         Exchange exchange = endpoint.createExchange();
         exchange.getIn().setBody(body);
         exchange.getIn().setHeaders(grcpHeaders);
 
-        if (endpoint.isSynchronous()) {
-            consumer.getProcessor().process(exchange);
-        } else {
-            consumer.getAsyncProcessor().process(exchange);
+        if (endpoint.getConfiguration().isRouteControlledStreamObserver()) {
+            exchange.setProperty("responseObserver", responseObserver);

Review comment:
       @rajasekharb would be nice to store this value in constants and give a 
name like `grpcResponseObserver` as generic name `responseObserver` can be 
intersected with other components property.

##########
File path: 
components/camel-grpc/src/test/java/org/apache/camel/component/grpc/RouteControlledStreamObserverTest.java
##########
@@ -0,0 +1,242 @@
+/*
+ * 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.grpc;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import io.grpc.stub.StreamObserver;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class RouteControlledStreamObserverTest extends CamelTestSupport {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(GrpcConsumerAggregationTest.class);
+
+    private static final int GRPC_SYNC_REQUEST_TEST_PORT = 
AvailablePortFinder.getNextAvailable();
+    private static final int GRPC_ASYNC_REQUEST_TEST_PORT = 
AvailablePortFinder.getNextAvailable();
+    private static final int GRPC_TEST_PING_ID = 1;
+    private static final String GRPC_TEST_PING_VALUE = "PING";
+    private static final String GRPC_TEST_PONG_VALUE = "PONG";
+
+    private ManagedChannel syncRequestChannel;
+    private ManagedChannel asyncRequestChannel;
+    private PingPongGrpc.PingPongBlockingStub blockingStub;
+    private PingPongGrpc.PingPongStub nonBlockingStub;
+    private PingPongGrpc.PingPongStub asyncNonBlockingStub;
+
+    @BeforeEach
+    public void startGrpcChannels() {
+        syncRequestChannel = ManagedChannelBuilder.forAddress("localhost", 
GRPC_SYNC_REQUEST_TEST_PORT).usePlaintext().build();
+        asyncRequestChannel
+                = ManagedChannelBuilder.forAddress("localhost", 
GRPC_ASYNC_REQUEST_TEST_PORT).usePlaintext().build();
+        blockingStub = PingPongGrpc.newBlockingStub(syncRequestChannel);
+        nonBlockingStub = PingPongGrpc.newStub(syncRequestChannel);
+        asyncNonBlockingStub = PingPongGrpc.newStub(asyncRequestChannel);
+    }
+
+    @AfterEach
+    public void stopGrpcChannels() {
+        syncRequestChannel.shutdown().shutdownNow();
+        asyncRequestChannel.shutdown().shutdownNow();
+    }
+
+    @Test
+    public void testSyncSyncMethodInSync() {
+        LOG.info("gRPC pingSyncSync method blocking test start");
+        PingRequest pingRequest
+                = 
PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build();
+        PongResponse pongResponse = blockingStub.pingSyncSync(pingRequest);
+
+        assertNotNull(pongResponse);
+        assertEquals(GRPC_TEST_PING_ID, pongResponse.getPongId());
+        assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, 
pongResponse.getPongName());
+    }
+
+    @Test
+    public void testSyncAsyncMethodInSync() {
+        LOG.info("gRPC pingSyncAsync method blocking test start");
+        PingRequest pingRequest
+                = 
PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build();
+        Iterator<PongResponse> pongResponseIter = 
blockingStub.pingSyncAsync(pingRequest);
+        while (pongResponseIter.hasNext()) {
+            PongResponse pongResponse = pongResponseIter.next();
+            assertNotNull(pongResponse);
+            assertEquals(GRPC_TEST_PING_ID, pongResponse.getPongId());
+            assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, 
pongResponse.getPongName());
+        }
+    }
+
+    @Test
+    public void testSyncSyncMethodInAsync() throws Exception {
+        LOG.info("gRPC pingSyncSync method async test start");
+        final CountDownLatch latch = new CountDownLatch(1);
+        PingRequest pingRequest
+                = 
PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build();
+        PongResponseStreamObserver responseObserver = new 
PongResponseStreamObserver(latch);
+
+        nonBlockingStub.pingSyncSync(pingRequest, responseObserver);
+        latch.await(5, TimeUnit.SECONDS);
+
+        PongResponse pongResponse = responseObserver.getPongResponse();
+
+        assertNotNull(pongResponse);
+        assertEquals(GRPC_TEST_PING_ID, pongResponse.getPongId());
+        assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, 
pongResponse.getPongName());
+    }
+
+    @Test
+    public void testSyncAsyncMethodInAsync() throws Exception {
+        LOG.info("gRPC pingSyncAsync method async test start");
+        final CountDownLatch latch = new CountDownLatch(1);
+        PingRequest pingRequest
+                = 
PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build();
+        PongResponseStreamObserver responseObserver = new 
PongResponseStreamObserver(latch);
+
+        nonBlockingStub.pingSyncAsync(pingRequest, responseObserver);
+        latch.await(5, TimeUnit.SECONDS);
+
+        PongResponse pongResponse = responseObserver.getPongResponse();
+
+        assertNotNull(pongResponse);
+        assertEquals(GRPC_TEST_PING_ID, pongResponse.getPongId());
+        assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, 
pongResponse.getPongName());
+    }
+
+    @Test
+    public void testAsyncSyncMethodInAsync() throws Exception {
+        LOG.info("gRPC pingAsyncSync method async test start");
+        final CountDownLatch latch = new CountDownLatch(1);
+        PingRequest pingRequest
+                = 
PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build();
+        PongResponseStreamObserver responseObserver = new 
PongResponseStreamObserver(latch);
+
+        StreamObserver<PingRequest> requestObserver = 
asyncNonBlockingStub.pingAsyncSync(responseObserver);
+        requestObserver.onNext(pingRequest);
+        requestObserver.onNext(pingRequest);
+        requestObserver.onCompleted();
+        latch.await(5, TimeUnit.SECONDS);
+
+        PongResponse pongResponse = responseObserver.getPongResponse();
+
+        assertNotNull(pongResponse);
+        assertEquals(GRPC_TEST_PING_ID, pongResponse.getPongId());
+        assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, 
pongResponse.getPongName());
+    }
+
+    @Test
+    public void testAsyncAsyncMethodInAsync() throws Exception {
+        LOG.info("gRPC pingAsyncAsync method async test start");
+        final CountDownLatch latch = new CountDownLatch(1);
+        PingRequest pingRequest
+                = 
PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build();
+        PongResponseStreamObserver responseObserver = new 
PongResponseStreamObserver(latch);
+
+        StreamObserver<PingRequest> requestObserver = 
asyncNonBlockingStub.pingAsyncAsync(responseObserver);
+        requestObserver.onNext(pingRequest);
+        requestObserver.onNext(pingRequest);
+        requestObserver.onCompleted();
+        latch.await(5, TimeUnit.SECONDS);
+
+        PongResponse pongResponse = responseObserver.getPongResponse();
+
+        assertNotNull(pongResponse);
+        assertEquals(GRPC_TEST_PING_ID, pongResponse.getPongId());
+        assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, 
pongResponse.getPongName());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            private void process(Exchange exchange) {
+                Message message = exchange.getIn();
+                PingRequest pingRequest = message.getBody(PingRequest.class);
+                StreamObserver<Object> responseObserver = 
(StreamObserver<Object>) exchange.getProperty("responseObserver");
+                PongResponse pongResponse
+                        = 
PongResponse.newBuilder().setPongName(pingRequest.getPingName() + 
GRPC_TEST_PONG_VALUE)
+                                .setPongId(pingRequest.getPingId()).build();
+                message.setBody(pongResponse, PongResponse.class);
+                exchange.setMessage(message);
+                responseObserver.onNext(pongResponse);
+                responseObserver.onCompleted();
+            }
+
+            @Override
+            public void configure() {
+                from("grpc://localhost:" + GRPC_SYNC_REQUEST_TEST_PORT
+                     + 
"/org.apache.camel.component.grpc.PingPong?synchronous=true&consumerStrategy=PROPAGATION&routeControlledStreamObserver=true")
+                             .process(this::process);
+
+                from("grpc://localhost:" + GRPC_ASYNC_REQUEST_TEST_PORT
+                     + 
"/org.apache.camel.component.grpc.PingPong?synchronous=true&consumerStrategy=AGGREGATION")

Review comment:
       Seems to routeControlledStreamObserver=true and 
consumerStrategy=AGGREGATION not compatible. We should notify that option will 
be ignored and not applied or just fail route start as it cause unpredictable 
result.

##########
File path: 
components/camel-grpc/src/main/java/org/apache/camel/component/grpc/server/GrpcMethodHandler.java
##########
@@ -62,16 +73,29 @@ public void handle(Object body, StreamObserver<Object> 
responseObserver, String
             Object responseBody = exchange.getIn().getBody();
             if (responseBody instanceof List) {
                 List<Object> responseList = (List<Object>) responseBody;
-                responseList.forEach(responseItem -> {
-                    responseObserver.onNext(responseItem);
-                });
+                responseList.forEach(responseObserver::onNext);
             } else {
                 responseObserver.onNext(responseBody);
             }
             responseObserver.onCompleted();
         }
     }
 
+    private void invokeCamelRoute(GrpcEndpoint endpoint, Exchange exchange) 
throws Exception {

Review comment:
       Camel in this name is redundant. Let's call this function as
   ```suggestion
       private void invokeRoute(GrpcEndpoint endpoint, Exchange exchange) 
throws Exception {
   ```




----------------------------------------------------------------
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:
[email protected]


Reply via email to