Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Create an account on Amazon Web Services
- // 2. Create your new SQS Service App
- // 3. There are "Standard Queue" and "FIFO Queue" (see details on AWS Web page). We gonna create FIFO one now
- // Choose "FIFO Queue" and name it "trix.fifo" (please note that .fifo prefix is necessary)
- // 4. Go to "AWS Security Credentials" page and generate your own Credentials Pair (Access Key ID, Secret Access Key)
- // 5. There are several ways to specify AWS credentials; we use the simplest one: via ENV variables
- // Copy your security credntials to your Environment, e.g.:
- export AWS_ACCESS_KEY_ID=BVOPJ7UNNRP53EXTJQ2A
- export AWS_SECRET_ACCESS_KEY=4jz12FxGUyqpPOWTa/r8hwlpx8QskxT/KVA8JwdB
- // 6. Preliminary work is done... Let's add some code:
- // build.sbt:
- libraryDependencies += "com.github.seratch" %% "awscala" % "0.6.+"
- // SqsTrix.scala
- import awscala.sqs.SQS
- import awscala._
- import sqs._
- import com.amazonaws.regions.{Region, Regions}
- import com.amazonaws.services.sqs.model.SendMessageRequest
- class SqsTrix {
- private lazy implicit val sqs: SQS = SQS.at(Region.getRegion(Regions.US_EAST_2)) // specify your AWS.Region here
- private lazy implicit val queue: Queue = sqs.queue("trix.fifo") getOrElse {
- throw new IllegalAccessException("Cannot open queues")
- }
- /**
- * Sends a single message to the FIFO queue
- * @param msg message body
- */
- def sendMsg(msg: String): Unit = {
- val request = new SendMessageRequest(queue.url, msg)
- .withMessageGroupId("AAA") // this is necessary for FIFO queues
- .withMessageDeduplicationId(s"${msg.hashCode}${System.nanoTime()}") // you must either check Content-Based
- // Deduplication in Queue settings, or specify this setting manually
- sqs.sendMessage(request)
- }
- /**
- * Receives messages and removes(!) them from the queue
- * @return list of messages
- */
- def receiveMsg(): Seq[Message] = {
- val messages = sqs.receiveMessage(queue)
- if (messages.nonEmpty) // this check is necessary (otherwise you'll get `NoSuchElementException`)
- sqs.deleteMessages(messages)
- messages
- }
- }
- // TestController.scala:
- import javax.inject.Inject
- import services._
- import scala.concurrent.{ExecutionContext, Future}
- class TestController @Inject()(
- cc: ControllerComponents,
- sqsTrix: SqsTrix)
- (implicit ec: ExecutionContext) extends AbstractController(cc) {
- // add to routes: POST /send controllers.TestController.sendToQueue
- def sendToQueue = Action { request =>
- val msg = request.body.asText getOrElse "<empty message>"
- sqsTrix.sendMsg(msg)
- Ok(s"'$msg' sent...")
- }
- // add to routes: GET /recv controllers.TestController.recvFromQueue
- def recvFromQueue = Action {
- val messages = sqsTrix.receiveMsg().mkString("\n\n")
- Ok(s"Result: $messages")
- }
- }
- // 7. Now open curl, Postman or another tool you wish and call:
- POST http://localhost:9000/send with body "Hello World!"
- POST http://localhost:9000/send with body "Good bye World!"
- // Output:
- // 'Hello World!' sent...
- // 'Good bye World!' sent...
- // 8. Open AWS SQS Console and ensure it contains 2 messages
- // 9. Let's receive them:
- GET http://localhost:9000/recv
- // Output:
- // Result: {MessageId: 293b241f-4d20-4abc-ade1-e9c95fd1b71e,ReceiptHandle: AQEBMzLkijnzgQxLbXw7FTSHiJ26XCL37AsqgJ+EKm+q+GnwlltDo0zIc84+2EL5lvcV86j3ayCitZMRTwuhyfauvqFyqM+YV2498FBlAskufoJ3/v/6S5c3UlB8+Quhpj6rwendCyJjVfOCrUKiHlhjUuas8FDT54yMHhqOl33lc/IQEnw84ZolUWS1lDyge8EN81Y+v53L/8kN2vhkTajM97nAI9shwJXBeiLNCJBatpoJLd0NXVUbHAReuf26w3KJIdDwo8dwxJjg6DhW5GHhMQ==,Body: Hello World!,Attributes: {},MessageAttributes: {}}
- // {MessageId: 048cdf0a-27d4-4da5-99b4-0443366a42f3,ReceiptHandle: AQEBtE9pz8dZcsHpozDath/AAkWcnT1bC3AXDbCAG3+IbX2h4dZ++IWbIdXRHIt7GYw0Bfv/Pi8jgF4x+Mxj5fed/o7mlc8ETmr2WR7qkoTGJ69ZR+62WxrZvuZto3oA9u1Mj67KX9J9S6kI7DVblQJmMbxmP5lnXCCzHQEqPpJDNKUCvAYaUVojHrnbJiWUGD6zVphJyEOSDwMUpFD2yyoBIOiA1vpQNy2fod7vVhInYNix5bIVo4w/HJuAbH/JYcbiLzWbehUX8S6vg9WCBTiBIA==,Body: Good bye World!,Attributes: {},MessageAttributes: {}}
- // 10. Open AWS SQS Console once again and ensure the queue is empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement