Amazon Kinesis Client Library for Java's abnormal processing mechanism
Amazon Kinesis Client Library (KCL) is a software development tool for Java, which is used to process Amazon Kinesis streaming data.As a powerful framework, KCL provides an abnormal processing mechanism to deal with errors and abnormalities that may occur during data processing.
Abnormal processing is a way to deal with possible errors or abnormal conditions by writing code for specific situations.In KCL, abnormal processing is an important part, because it allows the application to restore the error state and continue to run normally without interrupting the data processing process.
In KCL, abnormal treatment is mainly implemented in two ways: abnormal notifications and error retry.
1. Abnormal notification: For abnormal conditions that occur during data processing, KCL provides an exception notification mechanism so that the application can receive and handle these abnormalities.The `ProcessRedsInputsinPut Input" method in the `iRcordProcessor` interface is used to achieve.This method receives an object of a `ProcessRecordsinput` as a parameter, which contains the records to be processed and the abnormalities that occur.
The following is an example code that handles abnormal conditions:
@Override
public void processRecords(ProcessRecordsInput input) {
List<Record> records = input.getRecords();
ShardInfo shardInfo = input.getShardInfo();
for (Record record : records) {
try {
// Treatment record
processRecord(record);
} catch (Exception e) {
// Treatment abnormalities
handleException(e);
}
}
// Confirm the record has been processed
checkpoint(shardInfo.getSequenceNumber());
}
In the above example, the method of `ProcessRecord (Record)` is used to process each record. If there is an abnormality, it will enter an abnormal processing block.
2. Error retry: In order to ensure the robustness of data processing, KCL provides an error retry mechanism in order to deal with errors caused by network failures.Repeat the method of using the `IRCORDPROCESSOR` interface to implement the error method.
The following is a sample code that is wronged:
@Override
public void shutdown(ShutdownInput shutdownInput) {
// Determine whether an error occurs
if (shutdownInput.getShutdownReason() == ShutdownReason.TERMINATE) {
// Get the error message
Throwable throwable = shutdownInput.getShutdownReason().getCause();
// Error occur, make errors and retry
if (isRetryableError(throwable)) {
throw new IllegalStateException("Encountered an unrecoverable error. Retrying...", throwable);
}
}
// Turn off the processor
doShutdown();
}
In the above examples, the method of `shutdown ()` is used to handle error logic.First of all, it obtains the cause of closure through the method of `GetshutdownReason ()` and obtain an abnormal information from it.Then, to determine whether the abnormalities can be retrieved through the method of the `isretryableerror (throwable)` method.If it is a trial error, throw out the abnormality of the `` iLlegalStateException`.
In summary, Amazon Kinesis Client Library for Java provides a powerful abnormal processing mechanism to cope with possible errors and abnormalities.Using abnormal notifications and error retry mechanisms, developers can ensure the stentability of the data processing process and continue to run the application normally by restoring the state of error.
Hope this article helps you help you in use the abnormal processing mechanism in using Amazon Kinesis Client Library for Java.