io.radiowitness.kinesis.producer.KinesisClientFactory.java Source code

Java tutorial

Introduction

Here is the source code for io.radiowitness.kinesis.producer.KinesisClientFactory.java

Source

/*
 * Copyright (C) 2016 An Honest Effort LLC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.radiowitness.kinesis.producer;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.kinesis.AmazonKinesisAsyncClient;
import io.radiowitness.kinesis.KinesisConfig;

import java.util.concurrent.ExecutorService;

public class KinesisClientFactory {

    private final KinesisConfig config;
    private final ExecutorService executor;
    private final AWSCredentials credentials;
    private final ClientConfiguration clientConfig;

    public KinesisClientFactory(KinesisConfig config, ExecutorService executor) {
        this.config = config;
        this.executor = executor;
        credentials = new BasicAWSCredentials(config.getAccessKeyId(), config.getSecretKey());
        clientConfig = clientConfig();
    }

    private ClientConfiguration clientConfig() {
        ClientConfiguration clientConfig = new ClientConfiguration();
        StringBuilder userAgent = new StringBuilder(ClientConfiguration.DEFAULT_USER_AGENT);

        userAgent.append(" ");
        userAgent.append(config.getAppName());
        userAgent.append(" ");
        userAgent.append(config.getAppVersion());

        clientConfig.setUserAgent(userAgent.toString());
        clientConfig.setMaxConnections(1);

        /*
        todo:
          play with timeouts, retry policy, gzip, keep-alive, etc.
         */

        return clientConfig;
    }

    public AmazonKinesisAsyncClient create() {
        AmazonKinesisAsyncClient client = new AmazonKinesisAsyncClient(credentials, clientConfig, executor);
        client.setRegion(config.getRegion());
        return client;
    }

}