Search in sources :

Example 6 with ClientConfiguration

use of com.aliyun.oss.ClientConfiguration in project alluxio by Alluxio.

the class OSSUnderFileSystem method initializeOSSClientConfig.

/**
   * Creates an OSS {@code ClientConfiguration} using an Alluxio Configuration.
   *
   * @return the OSS {@link ClientConfiguration}
   */
private static ClientConfiguration initializeOSSClientConfig() {
    ClientConfiguration ossClientConf = new ClientConfiguration();
    ossClientConf.setConnectionTimeout(Configuration.getInt(PropertyKey.UNDERFS_OSS_CONNECT_TIMEOUT));
    ossClientConf.setSocketTimeout(Configuration.getInt(PropertyKey.UNDERFS_OSS_SOCKET_TIMEOUT));
    ossClientConf.setConnectionTTL(Configuration.getLong(PropertyKey.UNDERFS_OSS_CONNECT_TTL));
    ossClientConf.setMaxConnections(Configuration.getInt(PropertyKey.UNDERFS_OSS_CONNECT_MAX));
    return ossClientConf;
}
Also used : ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 7 with ClientConfiguration

use of com.aliyun.oss.ClientConfiguration in project aliyun-oss-java-sdk by aliyun.

the class RequestTimeoutTest method setUp.

@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
    long ticks = new Date().getTime() / 1000 + new Random().nextInt(5000);
    bucketName = BUCKET_NAME_PREFIX + ticks;
    if (ossClient == null) {
        ClientConfiguration config = new ClientConfiguration();
        config.setRequestTimeout(requestTimeout);
        config.setRequestTimeoutEnabled(true);
        // config.setMaxConnections(1);
        ossClient = new OSSClient(endpoint, accessId, accessKey, config);
        ossClient.createBucket(bucketName);
    }
}
Also used : Random(java.util.Random) OSSClient(com.aliyun.oss.OSSClient) Date(java.util.Date) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Before(org.junit.Before)

Example 8 with ClientConfiguration

use of com.aliyun.oss.ClientConfiguration in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method testRetryWithServiceException.

@Test
public void testRetryWithServiceException() throws Exception {
    // This request will fail after 1 retries
    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(1);
    // It should be always successful.
    int maxFailures = 0;
    String content = "Let's retry!";
    byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(contentBytes.length);
    ExecutionContext context = new ExecutionContext();
    context.getResponseHandlers().add(new ResponseHandler() {

        @Override
        public void handle(ResponseMessage responseData) throws ServiceException, ClientException {
            throw new ServiceException();
        }
    });
    // This request will succeed after 2 retries
    ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, null, 500, content);
    // Fix the max error retry count to 3
    try {
        client.sendRequest(request, context);
        fail("ServiceException has not been thrown.");
    } catch (ServiceException e) {
        assertEquals(2, client.getRequestAttempts());
    }
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) URI(java.net.URI) ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ServiceException(com.aliyun.oss.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Test(org.junit.Test)

Example 9 with ClientConfiguration

use of com.aliyun.oss.ClientConfiguration in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method testRetryWillSucceed.

@Test
public void testRetryWillSucceed() throws Exception {
    // Fix the max error retry count to 3
    final int MAX_RETRIES = 3;
    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(MAX_RETRIES);
    int maxFailures = 3;
    final long skipBeforeSend = 3;
    String content = "Let's retry!";
    byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
    contentStream.skip(skipBeforeSend);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(contentBytes.length - skipBeforeSend);
    ExecutionContext context = new ExecutionContext();
    ClientException exceptionToThrow = createRetryableException();
    // This request will succeed after 2 retries
    ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, exceptionToThrow, 200, content.substring((int) skipBeforeSend));
    client.sendRequest(request, context);
    assertEquals(MAX_RETRIES + 1, client.getRequestAttempts());
}
Also used : ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) URI(java.net.URI) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Test(org.junit.Test)

Example 10 with ClientConfiguration

use of com.aliyun.oss.ClientConfiguration in project aliyun-oss-java-sdk by aliyun.

the class ServiceClientTest method retryButFail.

private void retryButFail(final int maxRetries) throws UnsupportedEncodingException, URISyntaxException, ServiceException {
    // This request will fail after 3 retries
    ClientConfiguration config = new ClientConfiguration();
    config.setMaxErrorRetry(maxRetries);
    int maxFailures = 4;
    String content = "Let's retry!";
    byte[] contentBytes = content.getBytes(OSSConstants.DEFAULT_CHARSET_NAME);
    ByteArrayInputStream contentStream = new ByteArrayInputStream(contentBytes);
    RequestMessage request = new RequestMessage(null, null);
    request.setEndpoint(new URI("http://localhost"));
    request.setMethod(HttpMethod.GET);
    request.setContent(contentStream);
    request.setContentLength(contentBytes.length);
    ExecutionContext context = new ExecutionContext();
    ClientException exceptionToThrown = createRetryableException();
    ServiceClientImpl client = new ServiceClientImpl(config, maxFailures, exceptionToThrown, 200, content);
    try {
        client.sendRequest(request, context);
        fail("ClientException has not been thrown.");
    } catch (ClientException e) {
        assertEquals(exceptionToThrown, e);
        assertEquals(maxRetries + 1, client.getRequestAttempts());
    }
}
Also used : ExecutionContext(com.aliyun.oss.common.comm.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) URI(java.net.URI) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Aggregations

ClientConfiguration (com.aliyun.oss.ClientConfiguration)13 OSSClient (com.aliyun.oss.OSSClient)6 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)6 ClientException (com.aliyun.oss.ClientException)4 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 URI (java.net.URI)4 Test (org.junit.Test)3 Credentials (com.aliyun.oss.common.auth.Credentials)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 ServiceException (com.aliyun.oss.ServiceException)1 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)1 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)1 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)1 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)1 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)1 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)1 File (java.io.File)1