@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof Message == false) return false; Message other = (Message) obj; if (other.getMessageId() == null ^ this.getMessageId() == null) return false; if (other.getMessageId() != null && other.getMessageId().equals(this.getMessageId()) == false) return false; if (other.getReceiptHandle() == null ^ this.getReceiptHandle() == null) return false; if (other.getReceiptHandle() != null && other.getReceiptHandle().equals(this.getReceiptHandle()) == false) return false; if (other.getMD5OfBody() == null ^ this.getMD5OfBody() == null) return false; if (other.getMD5OfBody() != null && other.getMD5OfBody().equals(this.getMD5OfBody()) == false) return false; if (other.getBody() == null ^ this.getBody() == null) return false; if (other.getBody() != null && other.getBody().equals(this.getBody()) == false) return false; if (other.getAttributes() == null ^ this.getAttributes() == null) return false; if (other.getAttributes() != null && other.getAttributes().equals(this.getAttributes()) == false) return false; return true; }
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { try { String myQueueUrl = "https://queue.amazonaws.com/034307772076/MyQueue"; AmazonSQS sqs = new AmazonSQSClient( new PropertiesCredentials( SimpleQueueServiceSample.class.getResourceAsStream("AwsCredentials.properties"))); System.out.println("Sending message 1."); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "Test message.")); System.out.println("Receiving messages:"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.print(message.getMessageId()); System.out.println(" : " + message.getBody()); } } 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()); } }
public static void main(String[] args) { // Get Credentials AWSCredentials credentials = Credentials.getCredentials(); // Get SQS client AmazonSQS sqs = new AmazonSQSClient(credentials); // Start processing the messages from inbox queue for (; ; ) { // Receive messages System.out.println("Receiving messages from Inbox Queue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(inboxQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); if (messages.isEmpty()) { continue; } else { for (Message message : messages) { // temp variable to handle the integers in the message // MessageAttributeValue a=null; // MessageAttributeValue b=null; String a = ""; String b = ""; System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); String receiptHandle = message.getReceiptHandle(); String[] numbers = message.getBody().split(":"); /*Map<String, MessageAttributeValue> e = message.getMessageAttributes(); //for (Entry<String, MessageAttributeValue> entry: e.entrySet()) { for(Entry<String, String> entry : message.getAttributes().entrySet()){ System.out.println(" Attribute"); String temp1="firstNumber"; String temp2="secondNumber"; if(temp1.compareTo(entry.getKey())==0){ a=entry.getValue().toString(); } if(temp2.compareTo(entry.getKey())==0){ b=entry.getValue().toString(); } System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } */ // process the numbers String sum = numbers[0] + "+" + numbers[1]; String diff = numbers[0] + "-" + numbers[1]; System.out.println(sum); System.out.println(diff); Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put( "sum", new MessageAttributeValue().withDataType("String").withStringValue(sum)); messageAttributes.put( "diff", new MessageAttributeValue().withDataType("String").withStringValue(diff)); SendMessageRequest request = new SendMessageRequest(); request.withMessageBody("A test message body."); request.withQueueUrl(outboxQueueUrl); request.withMessageAttributes(messageAttributes); sqs.sendMessage(request); // delete the message from inbox queue sqs.deleteMessage(new DeleteMessageRequest(inboxQueueUrl, receiptHandle)); } } System.out.println(); } }