You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by di...@apache.org on 2021/09/02 20:13:02 UTC

[airavata-mft] branch develop updated: Making MFTApiClient a closable one

This is an automated email from the ASF dual-hosted git repository.

dimuthuupe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-mft.git


The following commit(s) were added to refs/heads/develop by this push:
     new c9b3c39  Making MFTApiClient a closable one
c9b3c39 is described below

commit c9b3c3973dddee709a0dc347ba3a15d3ef1078b2
Author: Dimuthu Wannipurage <di...@gmail.com>
AuthorDate: Thu Sep 2 16:12:50 2021 -0400

    Making MFTApiClient a closable one
---
 .../airavata/mft/api/client/MFTApiClient.java      | 28 ++++++++++++----------
 .../airavata/mft/api/client/examples/Example.java  |  2 +-
 .../mft/examples/http/DownloadExample.java         |  2 +-
 .../airavata/mft/examples/metadata/SCPExample.java |  2 +-
 .../mft/examples/transfer/LocalExample.java        |  2 +-
 .../airavata/mft/examples/transfer/S3Example.java  |  2 +-
 .../airavata/mft/examples/transfer/SCPExample.java |  2 +-
 7 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/api/client/src/main/java/org/apache/airavata/mft/api/client/MFTApiClient.java b/api/client/src/main/java/org/apache/airavata/mft/api/client/MFTApiClient.java
index 82bfcbb..d103849 100644
--- a/api/client/src/main/java/org/apache/airavata/mft/api/client/MFTApiClient.java
+++ b/api/client/src/main/java/org/apache/airavata/mft/api/client/MFTApiClient.java
@@ -21,24 +21,28 @@ import io.grpc.ManagedChannel;
 import io.grpc.ManagedChannelBuilder;
 import org.apache.airavata.mft.api.service.*;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.util.Collections;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
-public class MFTApiClient {
-    private static Map<String, Map<Integer, MFTApiServiceGrpc.MFTApiServiceBlockingStub>> stubCache = new ConcurrentHashMap<>();
+public class MFTApiClient implements Closeable {
 
-    public static MFTApiServiceGrpc.MFTApiServiceBlockingStub buildClient(String hostName, int port) {
+    private final ManagedChannel channel;
 
-        if (stubCache.containsKey(hostName)) {
-            if (stubCache.get(hostName).containsKey(port)) {
-                return stubCache.get(hostName).get(port);
-            }
-        }
+    public MFTApiClient(String hostName, int port) {
+        channel = ManagedChannelBuilder.forAddress(hostName, port).usePlaintext().build();
+    }
 
-        ManagedChannel channel = ManagedChannelBuilder.forAddress(hostName, port).usePlaintext().build();
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub stub = MFTApiServiceGrpc.newBlockingStub(channel);
-        stubCache.put(hostName, Collections.singletonMap(port, stub));
-        return stub;
+    public MFTApiServiceGrpc.MFTApiServiceBlockingStub get() {
+        return MFTApiServiceGrpc.newBlockingStub(channel);
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (channel != null) {
+            channel.shutdown();
+        }
     }
 }
diff --git a/api/client/src/main/java/org/apache/airavata/mft/api/client/examples/Example.java b/api/client/src/main/java/org/apache/airavata/mft/api/client/examples/Example.java
index ca63e1c..808c913 100644
--- a/api/client/src/main/java/org/apache/airavata/mft/api/client/examples/Example.java
+++ b/api/client/src/main/java/org/apache/airavata/mft/api/client/examples/Example.java
@@ -8,7 +8,7 @@ import org.apache.airavata.mft.api.service.ResourceAvailabilityRequest;
 public class Example {
 
     public static void main(String a[]) {
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub mftClient = MFTApiClient.buildClient("localhost", 7004);
+        MFTApiServiceGrpc.MFTApiServiceBlockingStub mftClient = new MFTApiClient("localhost", 7004).get();
         mftClient.getResourceAvailability(ResourceAvailabilityRequest.newBuilder()
                 .setResourceId("a")
                 .setResourceToken("b")
diff --git a/examples/src/main/java/org/apache/airavata/mft/examples/http/DownloadExample.java b/examples/src/main/java/org/apache/airavata/mft/examples/http/DownloadExample.java
index 4e95295..31f8f02 100644
--- a/examples/src/main/java/org/apache/airavata/mft/examples/http/DownloadExample.java
+++ b/examples/src/main/java/org/apache/airavata/mft/examples/http/DownloadExample.java
@@ -28,7 +28,7 @@ public class DownloadExample {
     public static void main(String args[]) {
         AuthToken mftAuthorizationToken = AuthToken.newBuilder().setUserTokenAuth(UserTokenAuth.newBuilder().setToken("43ff79ac-e4f2-473c-9ea1-04eee9509a53").build()).build();
 
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = MFTApiClient.buildClient("localhost", 7004);
+        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = new MFTApiClient("localhost", 7004).get();
         HttpDownloadApiResponse httpDownloadApiResponse = client.submitHttpDownload(HttpDownloadApiRequest.newBuilder()
                 .setTargetAgent("agent0")
                 .setSourceResourceId("remote-ssh-resource")
diff --git a/examples/src/main/java/org/apache/airavata/mft/examples/metadata/SCPExample.java b/examples/src/main/java/org/apache/airavata/mft/examples/metadata/SCPExample.java
index 3cf2ab0..0a95ac1 100644
--- a/examples/src/main/java/org/apache/airavata/mft/examples/metadata/SCPExample.java
+++ b/examples/src/main/java/org/apache/airavata/mft/examples/metadata/SCPExample.java
@@ -8,7 +8,7 @@ import org.apache.airavata.mft.api.service.MFTApiServiceGrpc;
 
 public class SCPExample {
     public static void main(String args[]) throws Exception {
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = MFTApiClient.buildClient("localhost", 7004);
+        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = new MFTApiClient("localhost", 7004).get();
 
         // File metadata
         long startTime = System.currentTimeMillis();
diff --git a/examples/src/main/java/org/apache/airavata/mft/examples/transfer/LocalExample.java b/examples/src/main/java/org/apache/airavata/mft/examples/transfer/LocalExample.java
index 2869a38..096cba9 100644
--- a/examples/src/main/java/org/apache/airavata/mft/examples/transfer/LocalExample.java
+++ b/examples/src/main/java/org/apache/airavata/mft/examples/transfer/LocalExample.java
@@ -26,7 +26,7 @@ import java.util.Iterator;
 
 public class LocalExample {
     public static void main(String args[]) throws Exception {
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = MFTApiClient.buildClient("localhost", 7004);
+        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = new MFTApiClient("localhost", 7004).get();
 
         String sourceResourceId = "remote-ssh-resource";
         String sourceToken = "local-ssh-cred";
diff --git a/examples/src/main/java/org/apache/airavata/mft/examples/transfer/S3Example.java b/examples/src/main/java/org/apache/airavata/mft/examples/transfer/S3Example.java
index eeb48f9..6894b7e 100644
--- a/examples/src/main/java/org/apache/airavata/mft/examples/transfer/S3Example.java
+++ b/examples/src/main/java/org/apache/airavata/mft/examples/transfer/S3Example.java
@@ -26,7 +26,7 @@ import java.util.Iterator;
 
 public class S3Example {
     public static void main(String args[]) throws Exception {
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = MFTApiClient.buildClient("localhost", 7004);
+        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = new MFTApiClient("localhost", 7004).get();
 
         String sourceResourceId = "remote-ssh-storage";
         String sourceResourcePath = "/tmp/1mb.txt";
diff --git a/examples/src/main/java/org/apache/airavata/mft/examples/transfer/SCPExample.java b/examples/src/main/java/org/apache/airavata/mft/examples/transfer/SCPExample.java
index 6e740b9..c48119b 100644
--- a/examples/src/main/java/org/apache/airavata/mft/examples/transfer/SCPExample.java
+++ b/examples/src/main/java/org/apache/airavata/mft/examples/transfer/SCPExample.java
@@ -26,7 +26,7 @@ import java.util.Iterator;
 
 public class SCPExample {
     public static void main(String args[]) throws Exception {
-        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = MFTApiClient.buildClient("localhost", 7004);
+        MFTApiServiceGrpc.MFTApiServiceBlockingStub client = new MFTApiClient("localhost", 7004).get();
 
         String sourceResourceId = "remote-ssh-resource-1";
         String sourceResourcePath = "/tmp/1mb.txt";