|
| 1 | +package com.obs.services; |
| 2 | + |
| 3 | +import com.obs.log.ILogger; |
| 4 | +import com.obs.log.LoggerBuilder; |
| 5 | +import com.obs.services.internal.task.UploadFileTask; |
| 6 | +import com.obs.services.model.CompleteMultipartUploadResult; |
| 7 | +import com.obs.services.model.TaskCallback; |
| 8 | +import com.obs.services.model.UploadFileRequest; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.concurrent.Callable; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | +import java.util.concurrent.Future; |
| 15 | + |
| 16 | +public class ObsClientAsync extends ObsClient implements IObsClientAsync { |
| 17 | + /** |
| 18 | + * Constructor |
| 19 | + * |
| 20 | + * @param endPoint OBS endpoint |
| 21 | + */ |
| 22 | + public ObsClientAsync(String endPoint) { |
| 23 | + super(endPoint); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructor |
| 28 | + * |
| 29 | + * @param config Configuration parameters of ObsClient |
| 30 | + */ |
| 31 | + public ObsClientAsync(ObsConfiguration config) { |
| 32 | + super(config); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructor |
| 37 | + * |
| 38 | + * @param accessKey AK in the access key |
| 39 | + * @param secretKey SK in the access key |
| 40 | + * @param endPoint OBS endpoint |
| 41 | + */ |
| 42 | + public ObsClientAsync(String accessKey, String secretKey, String endPoint) { |
| 43 | + super(accessKey, secretKey, endPoint); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructor |
| 48 | + * |
| 49 | + * @param accessKey AK in the access key |
| 50 | + * @param secretKey SK in the access key |
| 51 | + * @param config Configuration parameters of ObsClient |
| 52 | + */ |
| 53 | + public ObsClientAsync(String accessKey, String secretKey, ObsConfiguration config) { |
| 54 | + super(accessKey, secretKey, config); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructor |
| 59 | + * |
| 60 | + * @param accessKey AK in the temporary access key |
| 61 | + * @param secretKey SK in the temporary access key |
| 62 | + * @param securityToken Security token |
| 63 | + * @param endPoint OBS endpoint |
| 64 | + */ |
| 65 | + public ObsClientAsync(String accessKey, String secretKey, String securityToken, String endPoint) { |
| 66 | + super(accessKey, secretKey, securityToken, endPoint); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Constructor |
| 71 | + * |
| 72 | + * @param accessKey AK in the temporary access key |
| 73 | + * @param secretKey SK in the temporary access key |
| 74 | + * @param securityToken Security token |
| 75 | + * @param config Configuration parameters of ObsClient |
| 76 | + */ |
| 77 | + public ObsClientAsync(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { |
| 78 | + super(accessKey, secretKey, securityToken, config); |
| 79 | + } |
| 80 | + |
| 81 | + public ObsClientAsync(IObsCredentialsProvider provider, String endPoint) { |
| 82 | + super(provider, endPoint); |
| 83 | + } |
| 84 | + |
| 85 | + public ObsClientAsync(IObsCredentialsProvider provider, ObsConfiguration config) { |
| 86 | + super(provider, config); |
| 87 | + } |
| 88 | + |
| 89 | + private static final ILogger log = LoggerBuilder.getLogger(ObsClientAsync.class); |
| 90 | + private ExecutorService asyncClientExecutorService; |
| 91 | + private static final int DEFAULT_CLIENT_EXECUTOR_SERVICE_SIZE = 128; |
| 92 | + |
| 93 | + private int queryInterval = 1000; |
| 94 | + |
| 95 | + @Override |
| 96 | + public void close() throws IOException { |
| 97 | + log.warn("ObsClientAsync closing"); |
| 98 | + try { |
| 99 | + // finishing all task |
| 100 | + getExecutorService().shutdown(); |
| 101 | + log.warn("ObsClientAsync closed"); |
| 102 | + } catch (Exception e) { |
| 103 | + log.warn("ObsClientAsync close failed, detail:", e); |
| 104 | + } |
| 105 | + super.close(); |
| 106 | + } |
| 107 | + |
| 108 | + private static final String ASYNC_CLIENT_EXECUTOR_SERVICE_THREAD_NAME = "async-client-thread"; |
| 109 | + protected ExecutorService getExecutorService() { |
| 110 | + if (asyncClientExecutorService == null) { |
| 111 | + asyncClientExecutorService = Executors.newFixedThreadPool(DEFAULT_CLIENT_EXECUTOR_SERVICE_SIZE, |
| 112 | + r -> new Thread(r, ASYNC_CLIENT_EXECUTOR_SERVICE_THREAD_NAME)); |
| 113 | + } |
| 114 | + return asyncClientExecutorService; |
| 115 | + } |
| 116 | + |
| 117 | + public void setExecutorService(ExecutorService service) { |
| 118 | + if (asyncClientExecutorService != null) { // wait for all task finish |
| 119 | + asyncClientExecutorService.shutdown(); |
| 120 | + while (!asyncClientExecutorService.isTerminated()) { |
| 121 | + try { |
| 122 | + Thread.sleep(queryInterval); |
| 123 | + } catch (InterruptedException e) { |
| 124 | + log.warn("ObsClientAsync setExecutorService failed, detail:", e); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + asyncClientExecutorService = service; |
| 129 | + } |
| 130 | + |
| 131 | + public int getQueryInterval() { |
| 132 | + return queryInterval; |
| 133 | + } |
| 134 | + |
| 135 | + public void setQueryInterval(int queryInterval) { |
| 136 | + this.queryInterval = queryInterval; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param uploadFileRequest |
| 141 | + * @param completeCallback |
| 142 | + * @return |
| 143 | + */ |
| 144 | + @Override |
| 145 | + public UploadFileTask uploadFileAsync( |
| 146 | + UploadFileRequest uploadFileRequest, |
| 147 | + TaskCallback<CompleteMultipartUploadResult, UploadFileRequest> completeCallback) { |
| 148 | + log.debug("start uploadFileAsync"); |
| 149 | + if (uploadFileRequest.getCancelHandler() != null) { |
| 150 | + uploadFileRequest.getCancelHandler().resetCancelStatus(); |
| 151 | + } |
| 152 | + UploadFileTask uploadFileTask = |
| 153 | + new UploadFileTask(this, uploadFileRequest.getBucketName(), uploadFileRequest, completeCallback); |
| 154 | + Future<?> future = getExecutorService().submit((Callable<?>) uploadFileTask); |
| 155 | + |
| 156 | + uploadFileTask.setResultFuture(future); |
| 157 | + return uploadFileTask; |
| 158 | + } |
| 159 | +} |
0 commit comments