amazon-kinesis-client/amazon-kinesis-client-multilang/src/main/java/software/amazon/kinesis/multilang/MessageReader.java

124 lines
5.3 KiB
Java
Raw Normal View History

/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (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/asl/
*
* 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.kinesis.multilang;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import software.amazon.kinesis.multilang.messages.Message;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Provides methods for interacting with the child process's STDOUT.
*
* {@link #getNextMessageFromSTDOUT()} reads lines from the child process's STDOUT and attempts to decode a
* {@link Message} object from each line. A child process's STDOUT could have lines that don't contain data related to
* the multi-language protocol, such as when the child process prints debugging information to its STDOUT (instead of
* logging to a file), also when a child processes writes a Message it is expected to prepend and append a new line
* character to their message to help ensure that it is isolated on a line all by itself which results in empty lines
* being present in STDOUT. Lines which cannot be decoded to a Message object are ignored.
*
* {@link #drainSTDOUT()} simply reads all data from the child process's STDOUT until the stream is closed.
*/
class MessageReader {
private BufferedReader reader;
private String shardId;
private ObjectMapper objectMapper;
private ExecutorService executorService;
/**
* Use the initialize methods after construction.
*/
MessageReader() {
}
/**
* Returns a future which represents an attempt to read the next message in the child process's STDOUT. If the task
* is successful, the result of the future will be the next message found in the child process's STDOUT, if the task
* is unable to find a message before the child process's STDOUT is closed, or reading from STDOUT causes an
* IOException, then an execution exception will be generated by this future.
*
* The task employed by this method reads from the child process's STDOUT line by line. The task attempts to decode
* each line into a {@link Message} object. Lines that fail to decode to a Message are ignored and the task
* continues to the next line until it finds a Message.
*
* @return
*/
Future<Message> getNextMessageFromSTDOUT() {
GetNextMessageTask getNextMessageTask = new GetNextMessageTask(objectMapper);
getNextMessageTask.initialize(reader, shardId);
return executorService.submit(getNextMessageTask);
}
/**
* Returns a future that represents a computation that drains the STDOUT of the child process. That future's result
* is true if the end of the child's STDOUT is reached, its result is false if there was an error while reading from
* the stream. This task will log all the lines it drains to permit debugging.
*
* @return
*/
Future<Boolean> drainSTDOUT() {
DrainChildSTDOUTTask drainTask = new DrainChildSTDOUTTask();
drainTask.initialize(reader, shardId);
return this.executorService.submit(drainTask);
}
/**
* An initialization method allows us to delay setting the attributes of this class. Some of the attributes,
* stream and shardId, are not known to the {@link MultiLangRecordProcessorFactory} when it constructs a
Release 2.0.0 of the Amazon Kinesis Client for Java * Added support for Enhanced Fan Out. Enhanced Fan Out provides for lower end to end latency, and increased number of consumers per stream. * Records are now delivered via streaming, reducing end-to-end latency. * The Amazon Kinesis Client will automatically register a new consumer if required. When registering a new consumer, the Kinesis Client will default to the application name unless configured otherwise. * New configuration options are available to configure Enhanced Fan Out. * `SubscribeToShard` maintains long lived connections with Kinesis, which in the AWS Java SDK 2.0 is limited by default. The `KinesisClientUtil` has been added to assist configuring the `maxConcurrency` of the `KinesisAsyncClient`. __WARNING: The Amazon Kinesis Client may see significantly increased latency, unless the `KinesisAsyncClient` is configured to have a `maxConcurrency` high enough to allow all leases plus additional usages of the `KinesisAsyncClient`.__ | Name | Default | Description | |-----------------|---------|---------------------------------------------------------------------------------------------------------------------| | consumerArn | Unset | The ARN for an already created consumer. If this is set, the Kinesis Client will not attempt to create a consumer. | | streamName | Unset | The name of the stream that a consumer should be create for if necessary | | consumerName | Unset | The name of the consumer to create. If this is not set the applicationName will be used instead. | | applicationName | Unset | The name of the application. This is used as the name of the consumer unless consumerName is set. | * Modular Configuration of the Kinesis Client The Kinesis Client has migrated to a modular configuration system, and the `KinesisClientLibConfiguration` class has been removed. Configuration has been split into 7 classes. Default versions of the configuration can be created from the `ConfigsBuilder`. Please see the migration guide for more information * `CheckpointConfig` * `CoordinatorConfig` * `LeaseManagementConfig` * `LifecycleConfig` * `MetricsConfig` * `ProcessorConfig` * `RetrievalConfig` * Upgraded to AWS Java SDK 2.0 The Kinesis Client now uses the AWS Java SDK 2.0. The dependency on AWS Java SDK 1.11 has been removed. All configurations will only accept 2.0 clients. * When configuring the `KinesisAsyncClient` the `KinesisClientUtil#createKinesisAsyncClient` can be used to configure the Kinesis Client * __If you need support for AWS Java SDK 1.11 you will need to add a direct dependency.__ __When adding a dependency you must ensure that the 1.11 versions of Jackson dependencies are excluded__ Please see the migration guide for more information * MultiLangDaemon is now a separate module The MultiLangDaemon has been separated to its own Maven module and is no longer available in `amazon-kinesis-client`. To include the MultiLangDaemon, add a dependency on `amazon-kinesis-client-multilang`.
2018-08-02 17:57:11 +00:00
* {@link MultiLangShardRecordProcessor} but are later determined when
* {@link MultiLangShardRecordProcessor#initialize(String)} is called. So we follow a pattern where the attributes are
* set inside this method instead of the constructor so that this object will be initialized when all its attributes
* are known to the record processor.
*
* @param stream Used to read messages from the subprocess.
* @param shardId The shard we're working on.
* @param objectMapper The object mapper to decode messages.
* @param executorService An executor service to run tasks in.
*/
MessageReader initialize(InputStream stream,
String shardId,
ObjectMapper objectMapper,
ExecutorService executorService) {
return this.initialize(new BufferedReader(new InputStreamReader(stream)), shardId, objectMapper,
executorService);
}
/**
* @param reader Used to read messages from the subprocess.
* @param shardId The shard we're working on.
* @param objectMapper The object mapper to decode messages.
* @param executorService An executor service to run tasks in.
*/
MessageReader initialize(BufferedReader reader,
String shardId,
ObjectMapper objectMapper,
ExecutorService executorService) {
this.reader = reader;
this.shardId = shardId;
this.objectMapper = objectMapper;
this.executorService = executorService;
return this;
}
}