Java tutorial
/* * Copyright 2015 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 com.amazonaws.services.kinesis.application.stocktrades.processor; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.regions.Region; import com.amazonaws.regions.RegionUtils; import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory; import com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration; import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker; import com.amazonaws.services.kinesis.application.stocktrades.utils.ConfigurationUtils; import com.amazonaws.services.kinesis.application.stocktrades.utils.CredentialUtils; /** * Uses the Kinesis Client Library (KCL) to continuously consume and process stock trade * records from the stock trades stream. KCL monitors the number of shards and creates * record processor instances to read and process records from each shard. KCL also * load balances shards across all the instances of this processor. * */ public class StockTradesProcessor { private static final Log LOG = LogFactory.getLog(StockTradesProcessor.class); private static final Logger ROOT_LOGGER = Logger.getLogger(""); private static final Logger PROCESSOR_LOGGER = Logger .getLogger("com.amazonaws.services.kinesis.application.stocktrades.processor"); private static void checkUsage(String[] args) { if (args.length != 3) { System.err.println("Usage: " + StockTradesProcessor.class.getSimpleName() + " <application name> <stream name> <region>"); System.exit(1); } } /** * Sets the global log level to WARNING and the log level for this package to INFO, * so that we only see INFO messages for this processor. This is just for the purpose * of this tutorial, and should not be considered as best practice. * */ private static void setLogLevels() { ROOT_LOGGER.setLevel(Level.WARNING); PROCESSOR_LOGGER.setLevel(Level.INFO); } public static void main(String[] args) throws Exception { checkUsage(args); String applicationName = args[0]; String streamName = args[1]; Region region = RegionUtils.getRegion(args[2]); if (region == null) { System.err.println(args[2] + " is not a valid AWS region."); System.exit(1); } setLogLevels(); AWSCredentialsProvider credentialsProvider = CredentialUtils.getCredentialsProvider(); String workerId = String.valueOf(UUID.randomUUID()); KinesisClientLibConfiguration kclConfig = new KinesisClientLibConfiguration(applicationName, streamName, credentialsProvider, workerId).withRegionName(region.getName()) .withCommonClientConfig(ConfigurationUtils.getClientConfigWithUserAgent()); IRecordProcessorFactory recordProcessorFactory = new StockTradeRecordProcessorFactory(); // Create the KCL worker with the stock trade record processor factory Worker worker = new Worker(recordProcessorFactory, kclConfig); int exitCode = 0; try { worker.run(); } catch (Throwable t) { LOG.error("Caught throwable while processing data.", t); exitCode = 1; } System.exit(exitCode); } }