software.amazon.awssdk.http.async.SdkAsyncHttpClient Java Examples
The following examples show how to use
software.amazon.awssdk.http.async.SdkAsyncHttpClient.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: S3ClientConfiguration.java From tutorials with MIT License | 6 votes |
@Bean public S3AsyncClient s3client(S3ClientConfigurarionProperties s3props, AwsCredentialsProvider credentialsProvider) { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .writeTimeout(Duration.ZERO) .maxConcurrency(64) .build(); S3Configuration serviceConfiguration = S3Configuration.builder() .checksumValidationEnabled(false) .chunkedEncodingEnabled(true) .build(); S3AsyncClientBuilder b = S3AsyncClient.builder() .httpClient(httpClient) .region(s3props.getRegion()) .credentialsProvider(credentialsProvider) .serviceConfiguration(serviceConfiguration); if (s3props.getEndpoint() != null) { b = b.endpointOverride(s3props.getEndpoint()); } return b.build(); }
Example #2
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void closeClient_shouldCloseUnderlyingResources() { SdkEventLoopGroup eventLoopGroup = SdkEventLoopGroup.builder().build(); ChannelPool channelPool = mock(ChannelPool.class); SdkChannelPoolMap<URI, ChannelPool> sdkChannelPoolMap = new SdkChannelPoolMap<URI, ChannelPool>() { @Override protected ChannelPool newPool(URI key) { return channelPool; } }; sdkChannelPoolMap.get(URI.create("http://blah")); NettyConfiguration nettyConfiguration = new NettyConfiguration(AttributeMap.empty()); SdkAsyncHttpClient customerClient = new NettyNioAsyncHttpClient(eventLoopGroup, sdkChannelPoolMap, nettyConfiguration); customerClient.close(); assertThat(eventLoopGroup.eventLoopGroup().isShuttingDown()).isTrue(); assertThat(eventLoopGroup.eventLoopGroup().isTerminated()).isTrue(); assertThat(sdkChannelPoolMap).isEmpty(); Mockito.verify(channelPool).close(); }
Example #3
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void builderUsesProvidedTrustManagersProvider() throws Exception { WireMockServer selfSignedServer = HttpTestUtils.createSelfSignedServer(); TrustManagerFactory managerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); managerFactory.init(HttpTestUtils.getSelfSignedKeyStore()); try (SdkAsyncHttpClient netty = NettyNioAsyncHttpClient.builder() .tlsTrustManagersProvider(managerFactory::getTrustManagers) .build()) { selfSignedServer.start(); URI uri = URI.create("https://localhost:" + selfSignedServer.httpsPort()); SdkHttpRequest request = createRequest(uri); RecordingResponseHandler recorder = new RecordingResponseHandler(); client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(createProvider("")).responseHandler(recorder).build()); recorder.completeFuture.get(5, TimeUnit.SECONDS); } finally { selfSignedServer.stop(); } }
Example #4
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void customChannelFactoryIsUsed() throws Exception { ChannelFactory channelFactory = mock(ChannelFactory.class); when(channelFactory.newChannel()).thenAnswer((Answer<NioSocketChannel>) invocationOnMock -> new NioSocketChannel()); EventLoopGroup customEventLoopGroup = new NioEventLoopGroup(); SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .eventLoopGroup(SdkEventLoopGroup.create(customEventLoopGroup, channelFactory)) .build(); makeSimpleRequest(customClient); customClient.close(); Mockito.verify(channelFactory, atLeastOnce()).newChannel(); assertThat(customEventLoopGroup.isShuttingDown()).isFalse(); customEventLoopGroup.shutdownGracefully().awaitUninterruptibly(); }
Example #5
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void customEventLoopGroup_NotClosedWhenClientIsClosed() throws Exception { ThreadFactory threadFactory = spy(new CustomThreadFactory()); // Cannot use DefaultEventLoopGroupFactory because the concrete // implementation it creates is platform-dependent and could be a final // (i.e. non-spyable) class. EventLoopGroup eventLoopGroup = spy(new NioEventLoopGroup(0, threadFactory)); SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .eventLoopGroup(SdkEventLoopGroup.create(eventLoopGroup, NioSocketChannel::new)) .build(); makeSimpleRequest(customClient); customClient.close(); Mockito.verify(threadFactory, atLeastOnce()).newThread(Mockito.any()); Mockito.verify(eventLoopGroup, never()).shutdownGracefully(); }
Example #6
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void testExceptionMessageChanged_WhenPendingAcquireQueueIsFull() throws Exception { String expectedErrorMsg = "Maximum pending connection acquisitions exceeded."; SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(1) .maxPendingConnectionAcquires(1) .build(); List<CompletableFuture<Void>> futures = new ArrayList<>(); for (int i = 0; i < 10; i++) { futures.add(makeSimpleRequestAndReturnResponseHandler(customClient).completeFuture); } assertThatThrownBy(() -> CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join()) .hasMessageContaining(expectedErrorMsg); customClient.close(); }
Example #7
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void testExceptionMessageChanged_WhenConnectionTimeoutErrorEncountered() throws Exception { String expectedErrorMsg = "Acquire operation took longer than the configured maximum time. This indicates that a request " + "cannot get a connection from the pool within the specified maximum time."; SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(1) .connectionTimeout(Duration.ofMillis(1)) .connectionAcquisitionTimeout(Duration.ofMillis(1)) .build(); List<CompletableFuture<Void>> futures = new ArrayList<>(); for (int i = 0; i < 2; i++) { futures.add(makeSimpleRequestAndReturnResponseHandler(customClient).completeFuture); } assertThatThrownBy(() -> CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join()) .hasMessageContaining(expectedErrorMsg); customClient.close(); }
Example #8
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void customThreadCountIsRespected() throws Exception { final int threadCount = 10; ThreadFactory threadFactory = spy(new CustomThreadFactory()); SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .eventLoopGroupBuilder(SdkEventLoopGroup.builder() .threadFactory(threadFactory) .numberOfThreads(threadCount)) .build(); // Have to make enough requests to prime the threads for (int i = 0; i < threadCount + 1; i++) { makeSimpleRequest(customClient); } customClient.close(); Mockito.verify(threadFactory, times(threadCount)).newThread(Mockito.any()); }
Example #9
Source File: AsyncClientHandlerTransformerVerificationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Before public void testSetup() throws Exception { mockClient = mock(SdkAsyncHttpClient.class); executionParams = new ClientExecutionParams<SdkRequest, SdkResponse>() .withInput(request) .withMarshaller(marshaller) .withResponseHandler(responseHandler) .withErrorResponseHandler(errorResponseHandler); SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder() .option(SdkClientOption.ASYNC_HTTP_CLIENT, mockClient) .option(SdkClientOption.RETRY_POLICY, RETRY_POLICY) .option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, Runnable::run) .build(); clientHandler = new SdkAsyncClientHandler(config); when(request.overrideConfiguration()).thenReturn(Optional.empty()); when(marshaller.marshall(eq(request))).thenReturn(ValidSdkObjects.sdkHttpFullRequest().build()); when(responseHandler.handle(any(SdkHttpFullResponse.class), any(ExecutionAttributes.class))) .thenReturn(VoidSdkResponse.builder().build()); }
Example #10
Source File: ConnectionReaperTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
private void makeRequest(SdkAsyncHttpClient client) { stubFor(WireMock.any(urlPathEqualTo("/")).willReturn(aResponse().withBody(randomAlphabetic(10)))); URI uri = URI.create("http://localhost:" + mockServer.port()); client.execute(AsyncExecuteRequest.builder() .request(SdkHttpRequest.builder() .uri(uri) .method(SdkHttpMethod.GET) .encodedPath("/") .putHeader("Host", uri.getHost()) .build()) .requestContentPublisher(new EmptyPublisher()) .responseHandler(new RecordingResponseHandler()) .build()) .join(); }
Example #11
Source File: ConnectionReaperTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void oldConnectionReaperReapsActiveConnections() throws InterruptedException { Duration connectionTtl = Duration.ofMillis(200); try (SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder() .connectionTimeToLive(connectionTtl) .buildWithDefaults(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)) { Instant end = Instant.now().plus(Duration.ofSeconds(5)); verify(TRAFFIC_LISTENER, new Times(0)).closed(any()); // Send requests frequently, validating that connections are still being closed. while (Instant.now().isBefore(end)) { makeRequest(client); Thread.sleep(100); } verify(TRAFFIC_LISTENER, new AtLeast(20)).closed(any()); } }
Example #12
Source File: ConnectionReaperTest.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Test public void idleConnectionReaperDoesNotReapActiveConnections() throws InterruptedException { Duration maxIdleTime = Duration.ofSeconds(2); try(SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder() .connectionMaxIdleTime(maxIdleTime) .buildWithDefaults(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)) { Instant end = Instant.now().plus(maxIdleTime.plusSeconds(1)); // Send requests for longer than the max-idle time, ensuring no connections are closed. while (Instant.now().isBefore(end)) { makeRequest(client); Thread.sleep(100); verify(TRAFFIC_LISTENER, new Times(0)).closed(any()); } // Do nothing for longer than the max-idle time, ensuring connections are closed. Thread.sleep(maxIdleTime.plusSeconds(1).toMillis()); verify(TRAFFIC_LISTENER, new AtLeast(1)).closed(any()); } }
Example #13
Source File: AbstractAmazonServiceProcessor.java From quarkus with Apache License 2.0 | 6 votes |
protected void createClientBuilders(List<AmazonClientTransportsBuildItem> clients, BuildProducer<AmazonClientBuilderBuildItem> builderProducer, Function<RuntimeValue<SdkHttpClient.Builder>, RuntimeValue<AwsClientBuilder>> syncFunc, Function<RuntimeValue<SdkAsyncHttpClient.Builder>, RuntimeValue<AwsClientBuilder>> asyncFunc) { for (AmazonClientTransportsBuildItem client : clients) { if (configName().equals(client.getAwsClientName())) { RuntimeValue<AwsClientBuilder> syncBuilder = null; RuntimeValue<AwsClientBuilder> asyncBuilder = null; if (client.getSyncClassName().isPresent()) { syncBuilder = syncFunc.apply(client.getSyncTransport()); } if (client.getAsyncClassName().isPresent()) { asyncBuilder = asyncFunc.apply(client.getAsyncTransport()); } builderProducer.produce(new AmazonClientBuilderBuildItem(client.getAwsClientName(), syncBuilder, asyncBuilder)); } } }
Example #14
Source File: DefaultSdkAsyncHttpClientBuilder.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Override public SdkAsyncHttpClient buildWithDefaults(AttributeMap serviceDefaults) { // TODO We create and build every time. Do we want to cache it instead of the service binding? return DEFAULT_CHAIN .loadService() .map(SdkAsyncHttpService::createAsyncHttpClientFactory) .map(f -> f.buildWithDefaults(serviceDefaults)) .orElseThrow( () -> SdkClientException.builder() .message("Unable to load an HTTP implementation from any provider in the" + "chain. You must declare a dependency on an appropriate HTTP" + "implementation or pass in an SdkHttpClient explicitly to the" + "client builder.") .build()); }
Example #15
Source File: ResourceManagementTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void asyncHttpClientFromBuilderShutdown() { SdkAsyncHttpClient httpClient = mock(SdkAsyncHttpClient.class); SdkAsyncHttpClient.Builder httpClientBuilder = mock(SdkAsyncHttpClient.Builder.class); when(httpClientBuilder.buildWithDefaults(any())).thenReturn(httpClient); asyncClientBuilder().httpClientBuilder(httpClientBuilder).build().close(); verify(httpClient).close(); }
Example #16
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
/** * Make a simple async request and wait for it to fiish. * * @param client Client to make request with. */ private void makeSimpleRequest(SdkAsyncHttpClient client) throws Exception { String body = randomAlphabetic(10); URI uri = URI.create("http://localhost:" + mockServer.port()); stubFor(any(urlPathEqualTo("/")).willReturn(aResponse().withBody(body))); SdkHttpRequest request = createRequest(uri); RecordingResponseHandler recorder = new RecordingResponseHandler(); client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(createProvider("")).responseHandler(recorder).build()); recorder.completeFuture.get(5, TimeUnit.SECONDS); }
Example #17
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void createNettyClient_ReadWriteTimeoutCanBeZero() throws Exception { SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .readTimeout(Duration.ZERO) .writeTimeout(Duration.ZERO) .build(); makeSimpleRequest(customClient); customClient.close(); }
Example #18
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void closeMethodClosesOpenedChannels() throws InterruptedException, TimeoutException, ExecutionException { String body = randomAlphabetic(10); URI uri = URI.create("https://localhost:" + mockServer.httpsPort()); stubFor(any(urlPathEqualTo("/")).willReturn(aResponse().withHeader("Some-Header", "With Value").withBody(body))); SdkHttpFullRequest request = createRequest(uri, "/", body, SdkHttpMethod.POST, Collections.emptyMap()); RecordingResponseHandler recorder = new RecordingResponseHandler(); CompletableFuture<Boolean> channelClosedFuture = new CompletableFuture<>(); ChannelFactory<NioSocketChannel> channelFactory = new ChannelFactory<NioSocketChannel>() { @Override public NioSocketChannel newChannel() { return new NioSocketChannel() { @Override public ChannelFuture close() { ChannelFuture cf = super.close(); channelClosedFuture.complete(true); return cf; } }; } }; SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .eventLoopGroup(new SdkEventLoopGroup(new NioEventLoopGroup(1), channelFactory)) .buildWithDefaults(mapWithTrustAllCerts()); try { customClient.execute(AsyncExecuteRequest.builder() .request(request) .requestContentPublisher(createProvider(body)) .responseHandler(recorder).build()) .join(); } finally { customClient.close(); } assertThat(channelClosedFuture.get(5, TimeUnit.SECONDS)).isTrue(); }
Example #19
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private RecordingResponseHandler makeSimpleRequestAndReturnResponseHandler(SdkAsyncHttpClient client) throws Exception { String body = randomAlphabetic(10); URI uri = URI.create("http://localhost:" + mockServer.port()); stubFor(any(urlPathEqualTo("/")).willReturn(aResponse().withBody(body).withFixedDelay(1000))); SdkHttpRequest request = createRequest(uri); RecordingResponseHandler recorder = new RecordingResponseHandler(); client.execute(AsyncExecuteRequest.builder() .request(request) .requestContentPublisher(createProvider("")) .responseHandler(recorder) .build()); return recorder; }
Example #20
Source File: HttpTestUtils.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
public AmazonAsyncHttpClient build() { SdkAsyncHttpClient asyncHttpClient = this.asyncHttpClient != null ? this.asyncHttpClient : testSdkAsyncHttpClient(); return new AmazonAsyncHttpClient(testClientConfiguration().toBuilder() .option(SdkClientOption.ASYNC_HTTP_CLIENT, asyncHttpClient) .option(SdkClientOption.API_CALL_TIMEOUT, apiCallTimeout) .option(SdkClientOption.API_CALL_ATTEMPT_TIMEOUT, apiCallAttemptTimeout) .applyMutation(this::configureRetryPolicy) .applyMutation(this::configureAdditionalHeaders) .build()); }
Example #21
Source File: DefaultClientBuilderTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void asyncHttpClientFactoryProvided_ClientIsManagedBySdk() { TestAsyncClient client = testAsyncClientBuilder() .httpClientBuilder((SdkAsyncHttpClient.Builder) serviceDefaults -> { assertThat(serviceDefaults).isEqualTo(MOCK_DEFAULTS); return mock(SdkAsyncHttpClient.class); }) .build(); assertThat(client.clientConfiguration.option(SdkClientOption.ASYNC_HTTP_CLIENT)) .isNotInstanceOf(SdkDefaultClientBuilder.NonManagedSdkAsyncHttpClient.class); verify(defaultAsyncHttpClientFactory, never()).buildWithDefaults(any()); }
Example #22
Source File: DefaultClientBuilderTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void explicitAsyncHttpClientProvided_ClientIsNotManagedBySdk() { TestAsyncClient client = testAsyncClientBuilder() .httpClient(mock(SdkAsyncHttpClient.class)) .build(); assertThat(client.clientConfiguration.option(SdkClientOption.ASYNC_HTTP_CLIENT)) .isInstanceOf(SdkDefaultClientBuilder.NonManagedSdkAsyncHttpClient.class); verify(defaultAsyncHttpClientFactory, never()).buildWithDefaults(any()); }
Example #23
Source File: DefaultAwsClientBuilderTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void asyncHttpClientFactoryProvided_ClientIsManagedBySdk() { TestAsyncClient client = testAsyncClientBuilder() .region(Region.US_WEST_2) .httpClientBuilder((SdkAsyncHttpClient.Builder) serviceDefaults -> { assertThat(serviceDefaults).isEqualTo(MOCK_DEFAULTS); return mock(SdkAsyncHttpClient.class); }) .build(); assertThat(client.clientConfiguration.option(SdkClientOption.ASYNC_HTTP_CLIENT)) .isNotInstanceOf(AwsDefaultClientBuilder.NonManagedSdkAsyncHttpClient.class); verify(defaultAsyncHttpClientFactory, never()).buildWithDefaults(any()); }
Example #24
Source File: DefaultAwsClientBuilderTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void explicitAsyncHttpClientProvided_ClientIsNotManagedBySdk() { TestAsyncClient client = testAsyncClientBuilder() .region(Region.US_WEST_2) .httpClient(mock(SdkAsyncHttpClient.class)) .build(); assertThat(client.clientConfiguration.option(SdkClientOption.ASYNC_HTTP_CLIENT)) .isInstanceOf(AwsDefaultClientBuilder.NonManagedSdkAsyncHttpClient.class); verify(defaultAsyncHttpClientFactory, never()).buildWithDefaults(any()); }
Example #25
Source File: NettyNioAsyncHttpClient.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Override public SdkAsyncHttpClient buildWithDefaults(AttributeMap serviceDefaults) { return new NettyNioAsyncHttpClient(this, standardOptions.build() .merge(serviceDefaults) .merge(NETTY_HTTP_DEFAULTS) .merge(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS)); }
Example #26
Source File: NettyClientTlsAuthTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private void sendRequest(SdkAsyncHttpClient client, SdkAsyncHttpResponseHandler responseHandler) { AsyncExecuteRequest req = AsyncExecuteRequest.builder() .request(testSdkRequest()) .requestContentPublisher(new EmptyPublisher()) .responseHandler(responseHandler) .build(); client.execute(req).join(); }
Example #27
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void invalidMaxPendingConnectionAcquireConfig_shouldPropagateException() { try (SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(1) .maxPendingConnectionAcquires(0) .build()) { assertThatThrownBy(() -> makeSimpleRequest(customClient)).hasMessageContaining("java.lang.IllegalArgumentException: maxPendingAcquires: 0 (expected: >= 1)"); } }
Example #28
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void defaultJdkSslProvider() throws Exception { try (SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .sslProvider(SslProvider.JDK) .build()) { makeSimpleRequest(customClient); customClient.close(); } }
Example #29
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void openSslBeingUsed() throws Exception { try (SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .sslProvider(SslProvider.OPENSSL) .build()) { makeSimpleRequest(customClient); } }
Example #30
Source File: NettyNioAsyncHttpClientWireMockTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void customFactoryIsUsed() throws Exception { ThreadFactory threadFactory = spy(new CustomThreadFactory()); SdkAsyncHttpClient customClient = NettyNioAsyncHttpClient.builder() .eventLoopGroupBuilder(SdkEventLoopGroup.builder() .threadFactory(threadFactory)) .build(); makeSimpleRequest(customClient); customClient.close(); Mockito.verify(threadFactory, atLeastOnce()).newThread(Mockito.any()); }