software.amazon.awssdk.services.kinesis.model.SubscribeToShardEventStream Java Examples

The following examples show how to use software.amazon.awssdk.services.kinesis.model.SubscribeToShardEventStream. 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: FanOutRecordsPublisher.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Override
public void onNext(SubscribeToShardEventStream recordBatchEvent) {
    synchronized (parent.lockObject) {
        if (flow.shouldSubscriptionCancel()) {
            log.debug(
                    "{}: [SubscriptionLifetime]: (RecordSubscription#onNext) @ {} id: {} -- RecordFlow requires cancelling",
                    parent.shardId, connectionStartedAt, subscribeToShardId);
            cancel();
            return;
        }
        recordBatchEvent.accept(new SubscribeToShardResponseHandler.Visitor() {
            @Override
            public void visit(SubscribeToShardEvent event) {
                flow.recordsReceived(event);
            }
        });
    }
}
 
Example #2
Source File: SubscribeToShardUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void eventWithRecords_UnmarshalledCorrectly() throws Throwable {
    String data = BinaryUtils.toBase64("foobar".getBytes(StandardCharsets.UTF_8));
    AbortableInputStream content = new MessageWriter()
        .writeInitialResponse(new byte[0])
        .writeEvent("SubscribeToShardEvent",
                    String.format("{\"ContinuationSequenceNumber\": \"1234\","
                                  + "\"MillisBehindLatest\": 0,"
                                  + "\"Records\": [{\"Data\": \"%s\"}]"
                                  + "}", data))
        .toInputStream();
    SubscribeToShardEvent event = SubscribeToShardEvent.builder()
                                                       .continuationSequenceNumber("1234")
                                                       .millisBehindLatest(0L)
                                                       .records(Record.builder()
                                                                      .data(SdkBytes.fromUtf8String("foobar"))
                                                                      .build())
                                                       .build();

    stubResponse(SdkHttpFullResponse.builder()
                                    .statusCode(200)
                                    .content(content)
                                    .build());

    List<SubscribeToShardEventStream> events = subscribeToShard();
    assertThat(events).containsOnly(event);
}
 
Example #3
Source File: SubscribeToShardUnmarshallingTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private List<SubscribeToShardEventStream> subscribeToShard() throws Throwable {
    try {
        List<SubscribeToShardEventStream> events = new ArrayList<>();
        client.subscribeToShard(SubscribeToShardRequest.builder().build(),
                                SubscribeToShardResponseHandler.builder()
                                                               .subscriber(events::add)
                                                               .build())
              .join();
        return events;
    } catch (CompletionException e) {
        throw e.getCause();
    }
}
 
Example #4
Source File: KinesisStabilityTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public void onEventStream(SdkPublisher<SubscribeToShardEventStream> publisher) {
    publisher.filter(SubscribeToShardEvent.class)
             .subscribe(b -> {
                 log.debug(() -> "sequenceNumber " + b.records() + "_" + id);
                 receivedData.addAll(b.records().stream().map(Record::data).collect(Collectors.toList()));
             });
}
 
Example #5
Source File: KinesisStreamEx.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a SubscribeToShardResponseHandler the classic way by implementing the interface
 */
// snippet-start:[kinesis.java2.stream_example.custom_handler]
private static CompletableFuture<Void> responseHandlerBuilderClassic(KinesisAsyncClient client, SubscribeToShardRequest request) {
    SubscribeToShardResponseHandler responseHandler = new SubscribeToShardResponseHandler() {

        @Override
        public void responseReceived(SubscribeToShardResponse response) {
            System.out.println("Receieved initial response");
        }

        @Override
        public void onEventStream(SdkPublisher<SubscribeToShardEventStream> publisher) {
            publisher
                    // Filter to only SubscribeToShardEvents
                    .filter(SubscribeToShardEvent.class)
                    // Flat map into a publisher of just records
                    .flatMapIterable(SubscribeToShardEvent::records)
                    // Limit to 1000 total records
                    .limit(1000)
                    // Batch records into lists of 25
                    .buffer(25)
                    // Print out each record batch
                    .subscribe(batch -> System.out.println("Record Batch - " + batch));
        }

        @Override
        public void complete() {
            System.out.println("All records stream successfully");
        }

        @Override
        public void exceptionOccurred(Throwable throwable) {
            System.err.println("Error during stream - " + throwable.getMessage());
        }
    };
    return client.subscribeToShard(request, responseHandler);
}
 
Example #6
Source File: KinesisStreamEx.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(SubscribeToShardEventStream shardSubscriptionEventStream) {
    System.out.println("Received event " + shardSubscriptionEventStream);
    if (eventCount.incrementAndGet() >= 100) {
        // Cancel the subscription at any time to stop receiving events
        subscription.cancel();
    }
    subscription.request(1);
}
 
Example #7
Source File: FanOutRecordsPublisher.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private void rejectSubscription(SdkPublisher<SubscribeToShardEventStream> publisher) {
    publisher.subscribe(new Subscriber<SubscribeToShardEventStream>() {
        Subscription localSub;

        @Override
        public void onSubscribe(Subscription s) {
            localSub = s;
            localSub.cancel();
        }

        @Override
        public void onNext(SubscribeToShardEventStream subscribeToShardEventStream) {
            localSub.cancel();
        }

        @Override
        public void onError(Throwable t) {
            localSub.cancel();
        }

        @Override
        public void onComplete() {
            localSub.cancel();
        }
    });
}
 
Example #8
Source File: FanOutRecordsPublisher.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Override
public void onEventStream(SdkPublisher<SubscribeToShardEventStream> publisher) {
    synchronized (parent.lockObject) {
        log.debug("{}: [SubscriptionLifetime]: (RecordFlow#onEventStream)  @ {} id: {} -- Subscribe",
                parent.shardId, connectionStartedAt, subscribeToShardId);
        if (!parent.isActiveFlow(this)) {
            this.isDisposed = true;
            log.debug(
                    "{}: [SubscriptionLifetime]: (RecordFlow#onEventStream) @ {} id: {} -- parent is disposed",
                    parent.shardId, connectionStartedAt, subscribeToShardId);
            parent.rejectSubscription(publisher);
            return;
        }

        try {
            log.debug(
                    "{}: [SubscriptionLifetime]: (RecordFlow#onEventStream) @ {} id: {} -- creating record subscription",
                    parent.shardId, connectionStartedAt, subscribeToShardId);
            subscription = new RecordSubscription(parent, this, connectionStartedAt, subscribeToShardId);
            publisher.subscribe(subscription);

            //
            // Only flip this once we succeed
            //
            parent.isFirstConnection = false;
        } catch (Throwable t) {
            log.debug(
                    "{}: [SubscriptionLifetime]: (RecordFlow#onEventStream) @ {} id: {} -- throwable during record subscription: {}",
                    parent.shardId, connectionStartedAt, subscribeToShardId, t.getMessage());
            parent.errorOccurred(this, t);
        }
    }
}