org.hashbang.util.AutoDiscoverQueue.java Source code

Java tutorial

Introduction

Here is the source code for org.hashbang.util.AutoDiscoverQueue.java

Source

/*
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (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/apache2.0
 *
 * 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 org.hashbang.util;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.*;

public class AutoDiscoverQueue {

    private static final String QUEUE_URL = "https://sqs.us-west-2.amazonaws.com/596515083820/TrackerQ";

    private static AmazonSQS init() {

        /*
         * The ProfileCredentialsProvider will return your [default]
         * credential profile by reading from the credentials file located at
         * (~/.aws/credentials).
         */

        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider().getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                    + "Please make sure that your credentials file is at the correct "
                    + "location (~/.aws/credentials), and is in valid format.", e);
        }

        AmazonSQS sqs = new AmazonSQSClient(credentials);
        Region usWest2 = Region.getRegion(Regions.US_WEST_2);
        sqs.setRegion(usWest2);
        return sqs;
    }

    /**
     * Clear the AWS Q everytime the cluster starts.
     * We can only call it once every 60s. It is an API limitation.
     */
    public static void clearQ() {
        AmazonSQS sqs = init();
        sqs.purgeQueue(new PurgeQueueRequest(QUEUE_URL));
    }

    public static boolean sendInfo(String msg) {

        try {
            AmazonSQS sqs = init();
            //"To be filled by JobTracker"
            sqs.sendMessage(new SendMessageRequest(QUEUE_URL, msg));
            return true;

        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon SQS, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with SQS, such as not "
                    + "being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        } finally {
            return false;
        }

    }

    public static String getInfo() {

        Message message = null;
        String msg = null;

        try {
            // Receive messages
            AmazonSQS sqs = init();
            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(QUEUE_URL);
            sqs.receiveMessage(receiveMessageRequest);
            message = sqs.receiveMessage(receiveMessageRequest).getMessages().get(0);

            msg = message.getBody();
            sqs.deleteMessage(new DeleteMessageRequest(QUEUE_URL, message.getReceiptHandle()));

        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon SQS, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with SQS, such as not "
                    + "being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        } finally {
            return msg;
        }
    }
}