software.amazon.awssdk.services.databasemigration.paginators.DescribeEndpointsPublisher Maven / Gradle / Ivy
Show all versions of dms Show documentation
/*
* Copyright 2013-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package software.amazon.awssdk.services.databasemigration.paginators;
import java.util.concurrent.CompletableFuture;
import org.reactivestreams.Subscriber;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.core.pagination.async.AsyncPageFetcher;
import software.amazon.awssdk.core.pagination.async.EmptySubscription;
import software.amazon.awssdk.core.pagination.async.ResponsesSubscription;
import software.amazon.awssdk.core.pagination.async.SdkPublisher;
import software.amazon.awssdk.core.util.SdkAutoConstructList;
import software.amazon.awssdk.core.util.SdkAutoConstructMap;
import software.amazon.awssdk.services.databasemigration.DatabaseMigrationAsyncClient;
import software.amazon.awssdk.services.databasemigration.model.DescribeEndpointsRequest;
import software.amazon.awssdk.services.databasemigration.model.DescribeEndpointsResponse;
/**
*
* Represents the output for the
* {@link software.amazon.awssdk.services.databasemigration.DatabaseMigrationAsyncClient#describeEndpointsPaginator(software.amazon.awssdk.services.databasemigration.model.DescribeEndpointsRequest)}
* operation which is a paginated operation. This class is a type of {@link org.reactivestreams.Publisher} which can be
* used to provide a sequence of
* {@link software.amazon.awssdk.services.databasemigration.model.DescribeEndpointsResponse} response pages as per
* demand from the subscriber.
*
*
* When the operation is called, an instance of this class is returned. At this point, no service calls are made yet and
* so there is no guarantee that the request is valid. If there are errors in your request, you will see the failures
* only after you start streaming the data. The subscribe method should be called as a request to start streaming data.
* For more info, see {@link org.reactivestreams.Publisher#subscribe(org.reactivestreams.Subscriber)}. Each call to the
* subscribe method will result in a new {@link org.reactivestreams.Subscription} i.e., a new contract to stream data
* from the starting request.
*
*
*
* The following are few ways to use the response class:
*
* 1) Using the forEach helper method
*
*
* {@code
* software.amazon.awssdk.services.databasemigration.paginators.DescribeEndpointsPublisher publisher = client.describeEndpointsPaginator(request);
* CompletableFuture future = publisher.forEach(res -> { // Do something with the response });
* future.get();
* }
*
*
* 2) Using a custom subscriber
*
*
* {@code
* software.amazon.awssdk.services.databasemigration.paginators.DescribeEndpointsPublisher publisher = client.describeEndpointsPaginator(request);
* publisher.subscribe(new Subscriber() {
*
* public void onSubscribe(org.reactivestreams.Subscriber subscription) { //... };
*
*
* public void onNext(software.amazon.awssdk.services.databasemigration.model.DescribeEndpointsResponse response) { //... };
* });}
*
*
* As the response is a publisher, it can work well with third party reactive streams implementations like RxJava2.
*
* Note: If you prefer to have control on service calls, use the
* {@link #describeEndpoints(software.amazon.awssdk.services.databasemigration.model.DescribeEndpointsRequest)}
* operation.
*
*/
@Generated("software.amazon.awssdk:codegen")
public class DescribeEndpointsPublisher implements SdkPublisher {
private final DatabaseMigrationAsyncClient client;
private final DescribeEndpointsRequest firstRequest;
private final AsyncPageFetcher nextPageFetcher;
private boolean isLastPage;
public DescribeEndpointsPublisher(DatabaseMigrationAsyncClient client, DescribeEndpointsRequest firstRequest) {
this(client, firstRequest, false);
}
private DescribeEndpointsPublisher(DatabaseMigrationAsyncClient client, DescribeEndpointsRequest firstRequest,
boolean isLastPage) {
this.client = client;
this.firstRequest = firstRequest;
this.isLastPage = isLastPage;
this.nextPageFetcher = new DescribeEndpointsResponseFetcher();
}
@Override
public void subscribe(Subscriber super DescribeEndpointsResponse> subscriber) {
subscriber.onSubscribe(ResponsesSubscription.builder().subscriber(subscriber).nextPageFetcher(nextPageFetcher).build());
}
/**
*
* A helper method to resume the pages in case of unexpected failures. The method takes the last successful response
* page as input and returns an instance of {@link DescribeEndpointsPublisher} that can be used to retrieve the
* consecutive pages that follows the input page.
*
*/
private final DescribeEndpointsPublisher resume(DescribeEndpointsResponse lastSuccessfulPage) {
if (nextPageFetcher.hasNextPage(lastSuccessfulPage)) {
return new DescribeEndpointsPublisher(client, firstRequest.toBuilder().marker(lastSuccessfulPage.marker()).build());
}
return new DescribeEndpointsPublisher(client, firstRequest, true) {
@Override
public void subscribe(Subscriber super DescribeEndpointsResponse> subscriber) {
subscriber.onSubscribe(new EmptySubscription(subscriber));
}
};
}
private class DescribeEndpointsResponseFetcher implements AsyncPageFetcher {
@Override
public boolean hasNextPage(final DescribeEndpointsResponse previousPage) {
return previousPage.marker() != null && !SdkAutoConstructList.class.isInstance(previousPage.marker())
&& !SdkAutoConstructMap.class.isInstance(previousPage.marker());
}
@Override
public CompletableFuture nextPage(final DescribeEndpointsResponse previousPage) {
if (previousPage == null) {
return client.describeEndpoints(firstRequest);
}
return client.describeEndpoints(firstRequest.toBuilder().marker(previousPage.marker()).build());
}
}
}