java - Amazon SQS setRegion方法-LMLPHP

请告诉我为什么会出现此错误。

    package com.akrantha.massemail;

    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;

import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.AmazonWebServiceClient;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSAsyncClient;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;

public class GetBouncesComplaints {

    private static final String BOUNCES_FILE = "src/main/resources/failedids.txt";
    private static final String COMPLAINTS2 = "complaints";
    private static final String BOUNCES = "bounces";
    private static final String COMPLAINTS_FILE = "src/main/resources/complaints.txt";

    static List CompaintMails = new ArrayList();
    static List TBouncedEMails = new ArrayList();
    static List OBouncedEMails = new ArrayList();
    static List PBouncedEMails = new ArrayList();

    static boolean bounces = true;
    static boolean complaints = true;

    public static void main(String[] args) throws Exception {

        AWSCredentials awsCredentials = new AWSCredentials() {

            @Override
            public String getAWSSecretKey() {
                // TODO Auto-generated method stub
                return "0UKtTN6O/grXguBB5q9U9hJDBQwy/pX0wLIkhZRd";
            }

            @Override
            public String getAWSAccessKeyId() {
                // TODO Auto-generated method stub
                return "AKIAJ3B6Y7Y4N2NB7PCA";
            }
        };
        AmazonSQS sqs = new AmazonSQSClient(awsCredentials);
        Region usEast1 = Region.getRegion(Regions.US_EAST_1);
        sqs.setRegion(usEast1);

        System.out.println("===========================================");
        System.out.println("Getting Started with Amazon SQS");
        System.out.println("===========================================\n");

        if (bounces) {
            receiveAndExtractEmail(sqs);
            writeBounces();
        }
        if (complaints) {
            receiveAndExtractComplaints(sqs);
            writeComplaints();
        }

    }

    private static void writeComplaints() {
        FileWriter fos = null;
        try {

            fos = new FileWriter(COMPLAINTS_FILE, true);
            // PrintWriter out = new PrintWriter(fos);

            for (int i = 0; i < CompaintMails.size(); i++) {
                fos.write(String.valueOf(CompaintMails.get(i)) + "\n");
            }
            if (fos != null)
                fos.close();

        } catch (Exception e) {
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }
    }

    private static void receiveAndExtractComplaints(AmazonSQS sqs) {
        try {
            String myQueueUrl = "https://sqs.us-east-1.amazonaws.com/728845337292/ComplaintsHandler";
            System.out.println("Receiving messages from Complaints MyQueue.\n");
            int totalCount = 0;
            while (true) {

                ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(
                        myQueueUrl);
                List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
                int Count = messages.size();
                if (Count == 0)
                    break;
                totalCount++;

                for (Message message : messages) {
                    // processMessage(message);
                    processJsonComplaints(message);
                }
                // Delete a message
                System.out.println("Deleting a message.\n");
                String messageRecieptHandle = messages.get(0)
                        .getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl,
                        messageRecieptHandle));
                System.out.println();
            }

            System.out.println("The Queue contains " + totalCount
                    + " messages and they are handled");

        } 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());
        }
        System.out
                .println("------------------------------------------------------------------------");
        System.out
                .println("------------------------------------------------------------------------");
        System.out.println("Final lists");
        System.out
                .println("------------------------------------------------------------------------");
        System.out.println("TBounces --->" + TBouncedEMails);
        System.out
                .println("------------------------------------------------------------------------");
        System.out.println("PBounces --->" + CompaintMails);

    }

    private static void writeBounces() {
        FileWriter fos = null;
        try {

            fos = new FileWriter(BOUNCES_FILE, true);
            // PrintWriter out = new PrintWriter(fos);

            for (int i = 0; i < TBouncedEMails.size(); i++) {
                fos.write(String.valueOf(TBouncedEMails.get(i)) + "\n");
            }
            if (fos != null)
                fos.close();

        } catch (Exception e) {
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }
    }

    private static void receiveAndExtractEmail(AmazonSQS sqs) {
        try {
            String myQueueUrl = "https://sqs.us-east-1.amazonaws.com/728845337292/BounceHandler";
            System.out.println("Receiving messages from MyQueue.\n");
            int totalCount = 0;
            while (true) {

                ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(
                        myQueueUrl);
                List<Message> messages = sqs.receiveMessage(
                        receiveMessageRequest).getMessages();
                int Count = messages.size();
                if (Count == 0)
                    break;
                totalCount++;

                for (Message message : messages) {
                    // processMessage(message);
                        processJsonBounces(message);
                }
                // Delete a message
                System.out.println("Deleting a message.\n");
                String messageRecieptHandle = messages.get(0)
                        .getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl,
                        messageRecieptHandle));
                System.out.println();
            }

            System.out.println("The Queue contains " + totalCount
                    + " messages and they are handled");

        } 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());
        }
        System.out
                .println("------------------------------------------------------------------------");
        System.out
                .println("------------------------------------------------------------------------");
        System.out.println("Final lists");
        System.out
                .println("------------------------------------------------------------------------");
        System.out.println("TBounces --->" + TBouncedEMails);
        System.out
                .println("------------------------------------------------------------------------");
    }

    private static void processJsonComplaints(Message message) {
        String MessageExtract = message.getBody();
        // String jsonText = "[[null, 123.45, \"a\\tb c\"]}, true";
        JSONParser parser = new JSONParser();

        try {
            Object obj = parser.parse(MessageExtract);
            JSONObject obj1 = (JSONObject) obj;
            String messsage = (String) obj1.get("Message");
            JSONObject message2 = (JSONObject) JSONValue.parse(messsage);
            JSONObject bounce = (JSONObject) message2.get("complaint");
            // JSONObject message3 =(JSONObject)JSONValue.parse(bounce);
            JSONArray receipients = (JSONArray) bounce.get("complainedRecipients");
            if (receipients.isEmpty())
                return;
            for(int i=0; i< receipients.size(); i++) {
                JSONObject failedMessage = (JSONObject) receipients.get(i);
                // JSONObject obj3= (JSONObject)obj2.get("Message");
                // JSONArray obj3=(JSONArray)obj1.get("bouncedRecipients");
                // JSONObject obj4=(JSONObject)obj3.get(0);
                // JSONObject obj5=(JSONObject)obj1.get("emailAddress");
                // JSONObject bounce= (JSONObject)obj2.get("bounce");
                System.out.println("array: " + failedMessage.toString());
                String obj3 = (String) failedMessage.get("emailAddress");
                System.out.println("array: " + obj3.toString());
                CompaintMails.add(obj3);
            }
            //JSONObject failedMessage = (JSONObject) receipients.get(0);
            // JSONObject obj3= (JSONObject)obj2.get("Message");
            // JSONArray obj3=(JSONArray)obj1.get("bouncedRecipients");
            // JSONObject obj4=(JSONObject)obj3.get(0);
            // JSONObject obj5=(JSONObject)obj1.get("emailAddress");
            // JSONObject bounce= (JSONObject)obj2.get("bounce");
            //System.out.println("array: " + failedMessage.toString());
            //String obj3 = (String) failedMessage.get("emailAddress");
            //System.out.println("array: " + obj3.toString());
            //CompaintMails.add(obj3);

        } catch (ParseException pe) {
            System.out.println("position: " + pe.getPosition());
            System.out.println(pe);
        }

    }

    /**
     * Assume json configuration for receiving the messages
     * @param message
     */
    private static void processJsonBounces(Message message) {
        String MessageExtract = message.getBody();
        // String jsonText = "[[null, 123.45, \"a\\tb c\"]}, true";
        JSONParser parser = new JSONParser();

        try {
            Object obj = parser.parse(MessageExtract);
            JSONObject obj1 = (JSONObject) obj;
            String messsage = (String) obj1.get("Message");
            JSONObject message2 = (JSONObject) JSONValue.parse(messsage);
            JSONObject bounce = (JSONObject) message2.get("bounce");
            // JSONObject message3 =(JSONObject)JSONValue.parse(bounce);
            JSONArray receipients = (JSONArray) bounce.get("bouncedRecipients");
            if (receipients.isEmpty())
                return;

            for(int i=0; i< receipients.size(); i++) {
                JSONObject failedMessage = (JSONObject) receipients.get(i);
                System.out.println("array: " + failedMessage.toString());
                String obj3 = (String) failedMessage.get("emailAddress");
                System.out.println("array: " + obj3.toString());
                TBouncedEMails.add(obj3);
            }
            //JSONObject failedMessage = (JSONObject) receipients.get(0);
            // JSONObject obj3= (JSONObject)obj2.get("Message");
            // JSONArray obj3=(JSONArray)obj1.get("bouncedRecipients");
            // JSONObject obj4=(JSONObject)obj3.get(0);
            // JSONObject obj5=(JSONObject)obj1.get("emailAddress");
            // JSONObject bounce= (JSONObject)obj2.get("bounce");
            //System.out.println("array: " + failedMessage.toString());
            //String obj3 = (String) failedMessage.get("emailAddress");
            //System.out.println("array: " + obj3.toString());
            //BouncedEMails.add(obj3);

        } catch (ParseException pe) {
            System.out.println("position: " + pe.getPosition());
            System.out.println(pe);
        }

    }

    private static void processMessage(Message message) {
        String MessageExtract = message.getBody();
        // System.out.println(message);
        int startIndex = MessageExtract.indexOf("emailAddress") + 17;
        int end1Index = MessageExtract.indexOf("diagnosticCode") - 26;
        int end2Index = MessageExtract.indexOf("action") - 5;
        System.out.println(startIndex + "," + end1Index + "," + end2Index);
        String email = null;
        if (startIndex < end1Index) {
            email = message.getBody().substring(startIndex, end1Index);
        } else {
            if (startIndex < end2Index) {
                email = message.getBody().substring(startIndex, end2Index);
            } else {
                System.out.println("cannot handle the follwing email-->");
                email = message.getBody()
                        .substring(startIndex, startIndex + 30);
            }
        }
        int TypeSIndex = MessageExtract.indexOf("notificationType") + 21;
        int TypeEIndex = MessageExtract.indexOf("notificationType") + 27;
        System.out.println("*******************" + email);

        if (message.getBody().substring(TypeSIndex, TypeEIndex) == "Bounce")
            ;
        {

            int SevSIndex = MessageExtract.indexOf("bounceType") + 15;
            String decider = message.getBody().substring(SevSIndex,
                    SevSIndex + 1);
            // System.out.println(decider);
            int slashindex = email.indexOf("\\");
            if (slashindex > 0) {
                email = email.substring(0, slashindex);
            }
            if (decider.equals("T")) {
                System.out.println("Transient Bounced--->" + email);
                TBouncedEMails.add(email);
            } else {
                if (decider.equals("P")) {
                    System.out.println("Permenent Bounced--->" + email);
                    PBouncedEMails.add(email);
                } else {
                    System.out.println("Other Bounced--->" + email);
                    OBouncedEMails.add(email);
                }
            }
        }
    }
}


我添加了外部jar文件
apache-velocity-velocity-1.5.jar
freemarker-2.3.16.jar
javax.mail-1.4.5.jar
aws-android-sdk-0.2.1-debug.jar
json-simple-1.1.jar
aws-java-sdk-1.5.4.jar
org.springframework.beans.jar
spring-context-support.jar

还是我有错误


  未为类型AmazonSQS定义方法setRegion(Region)

最佳答案

要获得使用SQS的旧版AWS开发工具包,请尝试使用setEndpoint而不是setRegion。

例如

AmazonSQS sqs = new AmazonSQSClient(credentials);
//sqs.setRegion(region);
sqs.setEndpoint(regionStr);

09-28 05:03