Browse Source

add try catch in connect

zhiru 5 years ago
parent
commit
b2c29fe9ef

+ 0 - 8
src/main/java/io/milvus/client/MilvusClient.java

@@ -33,14 +33,6 @@ public interface MilvusClient {
 
     GetTableRowCountResponse getTableRowCount(TableParam tableParam);
 
-//    ClientResponse serverVersion() {
-//
-//    }
-//
-//    ClientResponse serverStatus() {
-//
-//    }
-
     //Cmd(Command) not implemented
 
     Response deleteByRange(DeleteByRangeParam deleteByRangeParam);

+ 11 - 5
src/main/java/io/milvus/client/MilvusGrpcClient.java

@@ -32,14 +32,20 @@ public class MilvusGrpcClient implements MilvusClient {
         if (channel != null) {
             logWarning("You have already connected!");
             return new Response(Response.Status.CONNECT_FAILED, "You have already connected!");
-        } else {
+        }
+
+        try {
             channel = ManagedChannelBuilder
-                      .forAddress(connectParam.getHost(), Integer.parseInt(connectParam.getPort()))
-                      .usePlaintext()
-                      .build();
+                    .forAddress(connectParam.getHost(), Integer.parseInt(connectParam.getPort()))
+                    .usePlaintext()
+                    .build();
+
+            blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
+        } catch (Exception e) {
+            logSevere("Connect failed!", e.getMessage());
+            return new Response(Response.Status.CONNECT_FAILED);
         }
 
-        blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
         logInfo("Connected successfully!\n{0}", connectParam.toString());
         return new Response(Response.Status.SUCCESS);
     }

+ 2 - 3
src/test/java/io/milvus/client/MilvusGrpcClientTest.java

@@ -21,13 +21,12 @@ class MilvusGrpcClientTest {
 
     private MilvusGrpcClient client;
 
-    RandomStringGenerator generator;
+    private RandomStringGenerator generator;
 
     private String randomTableName;
     private long size;
     private long dimension;
     private TableParam tableParam;
-    private TableSchemaParam tableSchemaParam;
 
     @org.junit.jupiter.api.BeforeEach
     void setUp() throws Exception {
@@ -57,7 +56,7 @@ class MilvusGrpcClientTest {
                                                     .withIndexFileSize(1024)
                                                     .withMetricType(MetricType.L2)
                                                     .build();
-        tableSchemaParam = new TableSchemaParam.Builder(tableSchema).build();
+        TableSchemaParam tableSchemaParam = new TableSchemaParam.Builder(tableSchema).build();
 
         assertTrue(client.createTable(tableSchemaParam).ok());
     }