Code Robo
Formatter
Comparator
Tester
Merger
Converter
Utility
Java Code Complience
Validator
EncoderDecoder
Virtual Service
Java Code To Connect Amazon Simple Queue Service(SQS) Service
       Talk to EasyAssistant

It provides the steps to connect AWS Glacier service and create vault, delete vault, upload zip file to vault and download files from Glacier valuts. One sample example with java code is shown for Java platform using AWS JAVA SDK library. Sample code uses aws-java-sdk-1.0.12 library provided by AWS. This java code is very sort and simple so that user can understand easily.


This java code connect AWS SQS(Simple Queue Service) service and get the list of the Queues and read the message from the queue as well as post to the message to the queue. Current program can be extended to asynchronous communication with the queue. From my experience what I noticed that people face the difficulties to coneect the queue of the AWS SQS service from Java client. But once the able to connect, they can easy do other operation with the queue as well as manage the queue . This program will guide you on how to connect SQS service from thrid part (external) program / service to manage the resources in SQS. This code can be copied, compiled and run on eclipse or any other Java IDE. You just need to have AWS java sdk jars added in your class path.

Sample Java Code To Connect queue in SQS service of AWS:

package easycodeforall.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.amazonaws.services.sqs.model.ListQueuesResult;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.SendMessageRequest;

public class ConnectAwsSQS {

	public static void main(String[] args) {

		System.out.println("Conneting AWS Amazon  SQS Service");
		String accessKeyID = "AKI*************37";// You get it form AWS Console /
		// You get it form AWS Console
		String secretAccessKey = "Mys*************************LSaV";
		AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyID, secretAccessKey);

		AmazonSQS awsSQS = AmazonSQSClientBuilder.standard()
				.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.US_EAST_1)
				.build();

		System.out.println("sqs.listQueues();=" + awsSQS.listQueues());

		ListQueuesResult results = awsSQS.listQueues();
		 

		postMessageToStandardQ(awsSQS);
		readMessageFromQ(awsSQS);

	}

	public static void readMessageFromQ(AmazonSQS awsSQS) {

		ReceiveMessageRequest readMessageRequestFromStdQueue = new ReceiveMessageRequest();
		readMessageRequestFromStdQueue.setQueueUrl("https://sqs.us-east-1.amazonaws.com/611554499274/DDR_easy");
		readMessageRequestFromStdQueue.setMaxNumberOfMessages(10);
		readMessageRequestFromStdQueue.setWaitTimeSeconds(10);

		List sqsMessages = awsSQS.receiveMessage(readMessageRequestFromStdQueue).getMessages();
		for (Message msg : sqsMessages) {

			System.out.println("Message Body=" + msg.getBody());
			System.out.println("Message Id=" + msg.getMessageId());
		}
		
		System.out.println("Message received from DDR_easy queue ");
	}

	public static void postMessageToStandardQ(AmazonSQS awsSQS) {

		SendMessageRequest messagePostRequestToStandardQueue = new SendMessageRequest();
		messagePostRequestToStandardQueue.setQueueUrl("https://sqs.us-east-1.amazonaws.com/611554499274/DDR_easy");
		messagePostRequestToStandardQueue.setMessageBody("Message Posted By Easycodeforall");

		MessageAttributeValue msgAttributeValue = new MessageAttributeValue();
		msgAttributeValue.setStringValue("DummyMessageAttribute");
		msgAttributeValue.setDataType("String");
		Map map = new HashMap();
		map.put("one", msgAttributeValue);
		messagePostRequestToStandardQueue.setMessageAttributes(map);
		awsSQS.sendMessage(messagePostRequestToStandardQueue);
		System.out.println("Message posted to DDR_easy queue ");

	}

}







Now question is how we can get the accessKeyID and secretAccessKey and URL of the Queue



1. Create a Policy for SQS service as shown below.

Create SQS Policy



2. Create a IAM User along with accessKey to access SQS service as shown below

Create user and access key, secret key



3.
You can create access key after creating the user. it will ask you to create a access key. Just click on that, it will create the access key and secret key.


Create user and access key, secret key


Create user and access key, secret key

Create user and access key, secret key


4. Assign the policy to the user which you created in the above step

Attach Policy With the User


Attach Policy With the User


Please provide your feedback here
It has been explained in this video (https://youtu.be/bxkNb0GuELY). Please watch it.
 



Post Your Comment:
Name :
Email ( Optional) :
Comments / Suggestion (* Required) It is required: :
: