Skip to content

Instantly share code, notes, and snippets.

@sadok-f
Last active September 16, 2016 12:44
Show Gist options
  • Select an option

  • Save sadok-f/0810c73d025c101a78bdf475f4fc3b33 to your computer and use it in GitHub Desktop.

Select an option

Save sadok-f/0810c73d025c101a78bdf475f4fc3b33 to your computer and use it in GitHub Desktop.
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.CreateQueueRequest
import com.amazonaws.services.sqs.model.DeleteMessageRequest
import com.amazonaws.services.sqs.model.DeleteQueueRequest
import com.amazonaws.services.sqs.model.Message
import com.amazonaws.services.sqs.model.ReceiveMessageRequest
import com.amazonaws.services.sqs.model.SendMessageRequest
import scala.collection.JavaConverters._
import scala.collection.mutable
/**
* Created by s.ferjani on 9/16/16.
*/
object SimpleQueueServiceSample extends App {
/*
* The ProfileCredentialsProvider will return your [default]
* credential profile by reading from the credentials file located at
* (~/.aws/credentials).
*/
var credentials: AWSCredentials = _
try {
credentials = new ProfileCredentialsProvider().getCredentials
} catch {
case e: Exception =>
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)
}
val sqs: AmazonSQS = new AmazonSQSClient(credentials)
val usWest2: Region = Region.getRegion(Regions.US_WEST_2)
sqs.setRegion(usWest2)
println("===========================================")
println("Getting Started with Amazon SQS")
println("===========================================\n")
try {
// Create a queue
println("Creating a new SQS queue called MyQueue.\n")
val createQueueRequest: CreateQueueRequest = new CreateQueueRequest("MyQueue")
val myQueueUrl: String = sqs.createQueue(createQueueRequest).getQueueUrl
// List queues
println("Listing all queues in your account.\n")
sqs.listQueues().getQueueUrls.toArray.foreach(queueUrl => {
println(" QueueUrl: " + queueUrl)
})
println()
// Send a message
println("Sending a message to MyQueue.\n")
sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text."))
// Receive messages
println("Receiving messages from MyQueue.\n")
val receiveMessageRequest: ReceiveMessageRequest = new ReceiveMessageRequest(myQueueUrl)
val messages: mutable.Buffer[Message] = sqs.receiveMessage(receiveMessageRequest).getMessages.asScala
messages.foreach(message => {
println(" Message")
println(" MessageId: " + message.getMessageId)
println(" ReceiptHandle: " + message.getReceiptHandle)
println(" MD5OfBody: " + message.getMD5OfBody)
println(" Body: " + message.getBody)
val entrySets = message.getAttributes.entrySet.asScala
for (entry <- entrySets) {
println(" Attribute")
println(" Name: " + entry.getKey)
println(" Value: " + entry.getValue)
}
})
println()
// Delete a message
println("Deleting a message.\n")
val messageReceiptHandle: String = messages.head.getReceiptHandle
sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle))
// Delete a queue
println("Deleting the test queue.\n")
sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl))
} catch {
case ase: AmazonServiceException =>
println("Caught an AmazonServiceException, which means your request made it " +
"to Amazon SQS, but was rejected with an error response for some reason.")
println("Error Message: " + ase.getMessage)
println("HTTP Status Code: " + ase.getStatusCode)
println("AWS Error Code: " + ase.getErrorCode)
println("Error Type: " + ase.getErrorType)
println("Request ID: " + ase.getRequestId)
case ace: AmazonClientException =>
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.")
println("Error Message: " + ace.getMessage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment