use of software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler in project flink by apache.
the class KinesisProxyV2Test method testSubscribeToShard.
@Test
public void testSubscribeToShard() {
KinesisAsyncClient kinesis = mock(KinesisAsyncClient.class);
KinesisProxyV2 proxy = new KinesisProxyV2(kinesis, mock(SdkAsyncHttpClient.class), createConfiguration(), mock(FullJitterBackoff.class));
SubscribeToShardRequest request = SubscribeToShardRequest.builder().build();
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().subscriber(event -> {
}).build();
proxy.subscribeToShard(request, responseHandler);
verify(kinesis).subscribeToShard(eq(request), eq(responseHandler));
}
use of software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler in project aws-doc-sdk-examples by awsdocs.
the class KinesisStreamEx method responseHandlerBuilderVisitor.
// snippet-end:[kinesis.java2.stream_example.visitor]
/**
* Subscribes to the stream of events by implementing the SubscribeToShardResponseHandler.Visitor interface.
*/
private static CompletableFuture<Void> responseHandlerBuilderVisitor(KinesisAsyncClient client, SubscribeToShardRequest request) {
SubscribeToShardResponseHandler.Visitor visitor = new SubscribeToShardResponseHandler.Visitor() {
@Override
public void visit(SubscribeToShardEvent event) {
System.out.println("Received subscribe to shard event " + event);
}
};
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(t -> System.err.println("Error during stream - " + t.getMessage())).subscriber(visitor).build();
return client.subscribeToShard(request, responseHandler);
}
use of software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler in project aws-doc-sdk-examples by awsdocs.
the class KinesisStreamEx method main.
public static void main(String[] args) {
// snippet-start:[kinesis.java2.stream_example.setup]
Region region = Region.US_EAST_1;
KinesisAsyncClient client = KinesisAsyncClient.builder().region(region).build();
SubscribeToShardRequest request = SubscribeToShardRequest.builder().consumerARN(CONSUMER_ARN).shardId("arn:aws:kinesis:us-east-1:111122223333:stream/StockTradeStream").startingPosition(s -> s.type(ShardIteratorType.LATEST)).build();
// snippet-end:[kinesis.java2.stream_example.setup]
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(t -> System.err.println("Error during stream - " + t.getMessage())).subscriber(MySubscriber::new).build();
client.subscribeToShard(request, responseHandler);
client.close();
}
use of software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler in project aws-doc-sdk-examples by awsdocs.
the class KinesisStreamEx method responseHandlerBuilderVisitorBuilder.
// snippet-end:[kinesis.java2.stream_example.publish_transformer]
/**
* Creates a SubscribeToShardResponseHandler.Visitor using the builder which lets you register an event handler for
* all events you're interested in rather than implementing the interface.
*/
// snippet-start:[kinesis.java2.stream_example.visitor]
private static CompletableFuture<Void> responseHandlerBuilderVisitorBuilder(KinesisAsyncClient client, SubscribeToShardRequest request) {
SubscribeToShardResponseHandler.Visitor visitor = SubscribeToShardResponseHandler.Visitor.builder().onSubscribeToShardEvent(e -> System.out.println("Received subscribe to shard event " + e)).build();
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(t -> System.err.println("Error during stream - " + t.getMessage())).subscriber(visitor).build();
return client.subscribeToShard(request, responseHandler);
}
use of software.amazon.awssdk.services.kinesis.model.SubscribeToShardResponseHandler in project flink by apache.
the class FanOutShardSubscriber method openSubscriptionToShard.
/**
* Calls {@link KinesisProxyV2#subscribeToShard} and waits to acquire a subscription. In the
* event a non-recoverable error occurs this method will rethrow the exception. Once the
* subscription is acquired the client signals to the producer that we are ready to receive
* records.
*
* @param startingPosition the position in which to start consuming from
* @throws FanOutSubscriberException when an exception is propagated from the networking stack
*/
private FanOutShardSubscription openSubscriptionToShard(final StartingPosition startingPosition) throws FanOutSubscriberException, InterruptedException {
SubscribeToShardRequest request = SubscribeToShardRequest.builder().consumerARN(consumerArn).shardId(shardId).startingPosition(startingPosition).build();
AtomicReference<Throwable> exception = new AtomicReference<>();
CountDownLatch waitForSubscriptionLatch = new CountDownLatch(1);
FanOutShardSubscription subscription = new FanOutShardSubscription(waitForSubscriptionLatch);
SubscribeToShardResponseHandler responseHandler = SubscribeToShardResponseHandler.builder().onError(e -> {
// (errors are ignored here once the subscription is open)
if (waitForSubscriptionLatch.getCount() > 0) {
exception.set(e);
waitForSubscriptionLatch.countDown();
}
}).subscriber(() -> subscription).build();
kinesis.subscribeToShard(request, responseHandler);
boolean subscriptionEstablished = waitForSubscriptionLatch.await(subscribeToShardTimeout.toMillis(), TimeUnit.MILLISECONDS);
if (!subscriptionEstablished) {
final String errorMessage = "Timed out acquiring subscription - " + shardId + " (" + consumerArn + ")";
LOG.error(errorMessage);
subscription.cancelSubscription();
handleError(new RecoverableFanOutSubscriberException(new TimeoutException(errorMessage)));
}
Throwable throwable = exception.get();
if (throwable != null) {
handleError(throwable);
}
LOG.debug("Acquired subscription - {} ({})", shardId, consumerArn);
// Request the first record to kick off consumption
// Following requests are made by the FanOutShardSubscription on the netty thread
subscription.requestRecord();
return subscription;
}
Aggregations