Example usage for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient

List of usage examples for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs AmazonSQSClient AmazonSQSClient.

Prototype

AmazonSQSClient(AwsSyncClientParams clientParams, boolean endpointDiscoveryEnabled) 

Source Link

Document

Constructs a new client to invoke service methods on Amazon SQS using the specified parameters.

Usage

From source file:com.amazon.sqs.javamessaging.SQSConnectionFactory.java

License:Open Source License

public SQSConnection createConnection(AWSCredentials awsCredentials) throws JMSException {
    AmazonSQSClient amazonSQSClient = new AmazonSQSClient(awsCredentials, clientConfig);
    configureClient(amazonSQSClient);// w ww .  jav  a  2s.  c o m
    AmazonSQSMessagingClientWrapper amazonSQSClientJMSWrapper = new AmazonSQSMessagingClientWrapper(
            amazonSQSClient);
    return new SQSConnection(amazonSQSClientJMSWrapper, numberOfMessagesToPrefetch);
}

From source file:com.amazon.sqs.javamessaging.SQSConnectionFactory.java

License:Open Source License

public SQSConnection createConnection(AWSCredentialsProvider awsCredentialsProvider) throws JMSException {
    AmazonSQSClient amazonSQSClient = new AmazonSQSClient(awsCredentialsProvider, clientConfig);
    configureClient(amazonSQSClient);/*from ww  w .  jav  a 2  s .  c  o  m*/
    AmazonSQSMessagingClientWrapper amazonSQSClientJMSWrapper = new AmazonSQSMessagingClientWrapper(
            amazonSQSClient);
    return new SQSConnection(amazonSQSClientJMSWrapper, numberOfMessagesToPrefetch);
}

From source file:com.dxc.temp.SimpleProducerConsumer.java

License:Open Source License

public static void main(String[] args) throws InterruptedException {
    int argIndex = 0;

    final String accessKey = args[argIndex++];
    final String secretKey = args[argIndex++];
    final AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

    final String endpoint = args[argIndex++];
    final String queueName = args[argIndex++];
    final int producerCount = Integer.parseInt(args[argIndex++]);
    final int consumerCount = Integer.parseInt(args[argIndex++]);
    final int batchSize = Integer.parseInt(args[argIndex++]);
    final int messageSizeByte = Integer.parseInt(args[argIndex++]);
    final int runTimeMinutes = Integer.parseInt(args[argIndex++]);

    // configure the SQS client with enough connections for all producer and
    // consumer threads
    AmazonSQS sqsClient = new AmazonSQSClient(credentials,
            new ClientConfiguration().withMaxConnections(producerCount + consumerCount));
    sqsClient.setEndpoint(endpoint);/*from www  .  j  a  v  a  2 s . c om*/
    String queueUrl = sqsClient.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl();

    // the flag to stop producer, consumer, and monitor threads
    AtomicBoolean stop = new AtomicBoolean(false);

    // start the producers
    final AtomicInteger producedCount = new AtomicInteger();
    Thread[] producers = new Thread[producerCount];
    for (int i = 0; i < producerCount; i++) {
        if (batchSize == 1)
            producers[i] = new Producer(sqsClient, queueUrl, messageSizeByte, producedCount, stop);
        else
            producers[i] = new BatchProducer(sqsClient, queueUrl, batchSize, messageSizeByte, producedCount,
                    stop);
        producers[i].start();
    }

    // start the consumers
    final AtomicInteger consumedCount = new AtomicInteger();
    Thread[] consumers = new Thread[consumerCount];
    for (int i = 0; i < consumerCount; i++) {
        if (batchSize == 1)
            consumers[i] = new Consumer(sqsClient, queueUrl, consumedCount, stop);
        else
            consumers[i] = new BatchConsumer(sqsClient, queueUrl, batchSize, consumedCount, stop);
        consumers[i].start();
    }

    // start the monitor (thread)
    Thread monitor = new Monitor(producedCount, consumedCount, stop);
    monitor.start();

    // wait for the specified amount of time then stop
    Thread.sleep(TimeUnit.MINUTES.toMillis(Math.min(runTimeMinutes, MAX_RUNTIME_MINUTES)));
    stop.set(true);

    // join all threads
    for (int i = 0; i < producerCount; i++)
        producers[i].join();

    for (int i = 0; i < consumerCount; i++)
        consumers[i].join();

    monitor.interrupt();
    monitor.join();
}

From source file:com.easarrive.aws.plugins.common.service.impl.SimpleProducerConsumer.java

License:Open Source License

public static void main(String[] args) throws InterruptedException {
    final AWSCredentials credentials = new BasicAWSCredentials("AKIAIDPJMKK4UHLE3OVA",
            "A+cn+TT3tUs6xbto5k1IKkWwPLBq995aOkqKxZNY");

    final String endpoint = "sqs.us-west-2.amazonaws.com";
    final String queueName = "image";
    final int producerCount = 10;
    final int consumerCount = 3;
    final int batchSize = 3;
    final int messageSizeByte = 10000;
    final int runTimeMinutes = 100;

    // configure the SQS client with enough connections for all producer and
    // consumer threads
    AmazonSQS sqsClient = new AmazonSQSClient(credentials,
            new ClientConfiguration().withMaxConnections(producerCount + consumerCount));
    sqsClient.setEndpoint(endpoint);//from  w w  w .j  a  v a 2s .  co m
    String queueUrl = sqsClient.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl();

    // the flag to stop producer, consumer, and monitor threads
    AtomicBoolean stop = new AtomicBoolean(false);

    // start the producers
    final AtomicInteger producedCount = new AtomicInteger();
    Thread[] producers = new Thread[producerCount];
    for (int i = 0; i < producerCount; i++) {
        producers[i] = new BatchProducer(sqsClient, queueUrl, batchSize, messageSizeByte, producedCount, stop);
        producers[i].start();
    }

    // start the consumers
    final AtomicInteger consumedCount = new AtomicInteger();
    Thread[] consumers = new Thread[consumerCount];
    for (int i = 0; i < consumerCount; i++) {
        consumers[i] = new BatchConsumer(sqsClient, queueUrl, batchSize, consumedCount, stop);
        consumers[i].start();
    }

    // start the monitor (thread)
    Thread monitor = new Monitor(producedCount, consumedCount, stop);
    monitor.start();

    // wait for the specified amount of time then stop
    Thread.sleep(TimeUnit.MINUTES.toMillis(Math.min(runTimeMinutes, MAX_RUNTIME_MINUTES)));
    stop.set(true);

    // join all threads
    for (int i = 0; i < producerCount; i++)
        producers[i].join();

    for (int i = 0; i < consumerCount; i++)
        consumers[i].join();

    monitor.interrupt();
    monitor.join();
}

From source file:com.eucalyptus.portal.SimpleQueueClientManager.java

License:Open Source License

private AmazonSQS buildClient(final Supplier<User> user, final String text) throws AuthException {
    final AWSCredentialsProvider credentialsProvider = new SecurityTokenAWSCredentialsProvider(user);
    final AmazonSQS client = new AmazonSQSClient(credentialsProvider, buildConfiguration(text));
    client.setEndpoint(ServiceUris.remote(Topology.lookup(SimpleQueue.class)).toString());
    return client;
}

From source file:com.netflix.suro.sink.notice.SQSNotice.java

License:Apache License

@Override
public void init() {
    if (sqsClient == null) { // not injected
        sqsClient = new AmazonSQSClient(credentialsProvider, clientConfig);
    }//from ww  w  .java2  s .c om
    String endpoint = "sqs." + this.region + ".amazonaws.com";
    sqsClient.setEndpoint(endpoint);

    for (String queueName : queues) {
        GetQueueUrlRequest request = new GetQueueUrlRequest();
        request.setQueueName(queueName);
        queueUrls.add(sqsClient.getQueueUrl(request).getQueueUrl());
    }

    log.info(String.format("SQSNotice initialized with the endpoint: %s, queue: %s", endpoint, queues));
}

From source file:com.plumbee.flume.source.sqs.SQSSource.java

License:Apache License

@Override
public void configure(Context context) {

    // Mandatory configuration parameters.
    queueURL = context.getString(ConfigurationConstants.CONFIG_QUEUE_URL);
    Preconditions.checkArgument(StringUtils.isNotBlank(queueURL), ErrorMessages.MISSING_MANDATORY_PARAMETER,
            ConfigurationConstants.CONFIG_QUEUE_URL);

    // Optional configuration parameters.
    queueRecvBatchSize = context.getInteger(ConfigurationConstants.CONFIG_RECV_BATCH_SIZE,
            ConfigurationConstants.DEFAULT_RECV_BATCH_SIZE);
    Preconditions.checkArgument(queueRecvBatchSize > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_RECV_BATCH_SIZE);

    queueDeleteBatchSize = context.getInteger(ConfigurationConstants.CONFIG_DELETE_BATCH_SIZE,
            ConfigurationConstants.DEFAULT_DELETE_BATCH_SIZE);
    Preconditions.checkArgument(queueDeleteBatchSize > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_DELETE_BATCH_SIZE);

    queueRecvPollingTimeout = context.getInteger(ConfigurationConstants.CONFIG_RECV_TIMEOUT,
            ConfigurationConstants.DEFAULT_RECV_TIMEOUT);
    Preconditions.checkArgument(queueRecvPollingTimeout > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_RECV_TIMEOUT);

    queueRecvVisabilityTimeout = context.getInteger(ConfigurationConstants.CONFIG_RECV_VISTIMEOUT,
            ConfigurationConstants.DEFAULT_RECV_VISTIMEOUT);
    Preconditions.checkArgument(queueRecvVisabilityTimeout > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_RECV_VISTIMEOUT);

    batchSize = context.getInteger(ConfigurationConstants.CONFIG_BATCH_SIZE,
            ConfigurationConstants.DEFAULT_BATCH_SIZE);
    Preconditions.checkArgument(batchSize > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_BATCH_SIZE);

    nbThreads = context.getInteger(ConfigurationConstants.CONFIG_NB_CONSUMER_THREADS,
            ConfigurationConstants.DEFAULT_NB_CONSUMER_THREADS);
    Preconditions.checkArgument(nbThreads > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_NB_CONSUMER_THREADS);
    Preconditions.checkArgument(nbThreads <= ClientConfiguration.DEFAULT_MAX_CONNECTIONS,
            "%s cannot cannot exceed %s " + "(Default Amazon client connection pool size)",
            ConfigurationConstants.CONFIG_NB_CONSUMER_THREADS, ClientConfiguration.DEFAULT_MAX_CONNECTIONS);

    // Don't let the number of messages to be polled from SQS using one
    // call exceed the transaction batchSize for the downstream channel.
    Preconditions.checkArgument(queueRecvBatchSize <= batchSize, "%s must be smaller than or equal to the %s",
            ConfigurationConstants.CONFIG_RECV_BATCH_SIZE, ConfigurationConstants.CONFIG_BATCH_SIZE);

    flushInterval = context.getLong(ConfigurationConstants.CONFIG_FLUSH_INTERVAL,
            ConfigurationConstants.DEFAULT_FLUSH_INTERVAL);
    Preconditions.checkArgument(flushInterval > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_FLUSH_INTERVAL);
    flushInterval = TimeUnit.SECONDS.toMillis(flushInterval);

    // Runner backoff configuration.
    maxBackOffSleep = context.getLong(ConfigurationConstants.CONFIG_MAX_BACKOFF_SLEEP,
            ConfigurationConstants.DEFAULT_MAX_BACKOFF_SLEEP);
    Preconditions.checkArgument(maxBackOffSleep > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_MAX_BACKOFF_SLEEP);

    backOffSleepIncrement = context.getLong(ConfigurationConstants.CONFIG_BACKOFF_SLEEP_INCREMENT,
            ConfigurationConstants.DEFAULT_BACKOFF_SLEEP_INCREMENT);
    Preconditions.checkArgument(backOffSleepIncrement > 0, ErrorMessages.NEGATIVE_PARAMETER_VALUE,
            ConfigurationConstants.CONFIG_BACKOFF_SLEEP_INCREMENT);

    Preconditions.checkArgument(flushInterval > maxBackOffSleep, "%s too high, %s cannot be respected",
            ConfigurationConstants.CONFIG_MAX_BACKOFF_SLEEP, ConfigurationConstants.CONFIG_FLUSH_INTERVAL);

    // Log a warning if the flushInterval plus maxBackOffSleep exceed
    // the queueRecvVisabilityTimeout of messages. On queues with
    // low levels of throughput this can cause message duplication!
    if ((flushInterval + maxBackOffSleep) > TimeUnit.SECONDS.toMillis(queueRecvVisabilityTimeout)) {
        LOGGER.warn("{} too low, potential for message duplication",
                ConfigurationConstants.CONFIG_FLUSH_INTERVAL);
    }//  w ww  .ja  v  a  2 s . com

    // The following configuration options allows credentials to be
    // provided via the configuration context.
    String awsAccessKeyId = context.getString(ConfigurationConstants.CONFIG_AWS_ACCESS_KEY_ID);
    String awsSecretKey = context.getString(ConfigurationConstants.CONFIG_AWS_SECRET_KEY);

    if (StringUtils.isNotBlank(awsAccessKeyId) && StringUtils.isNotBlank(awsSecretKey)) {
        if (client == null) {
            // Create the AmazonSQSClient using BasicAWSCredentials
            client = new AmazonSQSClient(new BasicAWSCredentials(awsAccessKeyId, awsSecretKey),
                    new ClientConfiguration().withMaxConnections(nbThreads));
        } else {
            LOGGER.warn("Cannot set AWS credentials for AmazonSQSClient, " + "client already initialized");
        }
    }

    // Default to the DefaultAWSCredentialsProviderChain.
    if (client == null) {
        client = new AmazonSQSClient(new ClientConfiguration().withMaxConnections(nbThreads));
    }
}

From source file:com.sitewhere.aws.SqsOutboundEventProcessor.java

License:Open Source License

@Override
public void start() throws SiteWhereException {
    super.start();

    ClientConfiguration config = new ClientConfiguration();
    config.setMaxConnections(250);/*from   w  w w. j  av a2  s.  c  om*/
    config.setMaxErrorRetry(5);

    if (getAccessKey() == null) {
        throw new SiteWhereException("Amazon access key not provided.");
    }

    if (getSecretKey() == null) {
        throw new SiteWhereException("Amazon secret key not provided.");
    }

    sqs = new AmazonSQSClient(new BasicAWSCredentials(getAccessKey(), getSecretKey()), config);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);
}

From source file:com.sitewhere.connectors.aws.sqs.SqsOutboundEventProcessor.java

License:Open Source License

@Override
public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException {
    super.start(monitor);

    ClientConfiguration config = new ClientConfiguration();
    config.setMaxConnections(250);/* ww  w  .j  a v a 2 s . com*/
    config.setMaxErrorRetry(5);

    if (getAccessKey() == null) {
        throw new SiteWhereException("Amazon access key not provided.");
    }

    if (getSecretKey() == null) {
        throw new SiteWhereException("Amazon secret key not provided.");
    }

    sqs = new AmazonSQSClient(new BasicAWSCredentials(getAccessKey(), getSecretKey()), config);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);
}

From source file:io.relution.jenkins.awssqs.factories.SQSFactoryImpl.java

License:Apache License

@Override
public AmazonSQS createSQS(final io.relution.jenkins.awssqs.interfaces.SQSQueue queue) {
    final ClientConfiguration clientConfiguration = this.getClientConfiguration(queue);
    boolean hasCredentials = isNotBlank(queue.getAWSAccessKeyId()) && isNotBlank(queue.getAWSSecretKey());
    io.relution.jenkins.awssqs.logging.Log.info("Creating AmazonSQS instance - hasCredentials='%s'",
            hasCredentials);//from  w  w w. j a  v  a2  s. c  om
    final AmazonSQS sqs = hasCredentials ? new AmazonSQSClient(queue, clientConfiguration)
            : new AmazonSQSClient(clientConfiguration);

    if (queue.getEndpoint() != null) {
        sqs.setEndpoint(queue.getEndpoint());
    }

    return sqs;
}