如何使用scala将消息正确发送到amazon sqs队列?

时间:2017-01-26 12:10:43

标签: java scala amazon-web-services message-queue amazon-sqs

我看到了这个common-aws on github如何使用它,这是他们的例子(仅针对发送者,因为这是我需要的):

import com.amazonaws.services.sqs.AmazonSQSAsyncClient 
import com.pellucid.wrap.sqs.AmazonSQSScalaClient
import com.mfglabs.commons.aws.sqs._

val sqs = new AmazonSQSScalaClient(new AmazonSQSAsyncClient(), ec)
val builder = SQSStreamBuilder(sqs)

val sender: Flow[String, SendMessageResult, Unit] =
  Flow[String].map { body =>
    val req = new SendMessageRequest()
    req.setMessageBody(body)
    req.setQueueUrl(queueUrl)
    req
  }
  .via(builder.sendMessageAsStream())

但我得到了一些错误,我真的不明白这个例子,我需要的是创建一个func,它接受一个case类列表,将每个列表序列化为json并将其发送到sqs队列......就是这样,所以这就是我到目前为止所做的:

val queueUrl = "the url to my queue"

//the objects here are of case class type ObjectUploadRequest
val listOfObjects = List(Obj1, Obj2, Obj3, Obj4, Obj5)

def pushListToSQS(listOfObjectsRequests: List[ObjectUploadRequest]): Future[SendMessageRequest] = {
  listOfObjectsRequests.map(objReq => {
    val ser = swrite(objReq)
    val sender: Flow[String, SendMessageResult, Unit] =
      Flow[String].map { body =>
        val req = new SendMessageRequest()
        req.setMessageBody(body)
        req.setQueueUrl(queueUrl)
        req
      }.via(builder.sendMessageAsStream())
  })
}

我发现了这个错误:

enter image description here

如果有人可以提供帮助,

会很感激,谢谢

1 个答案:

答案 0 :(得分:0)

如果您不介意使用优秀的旧AWS Java SDK及其同步SQS客户端,那么这对我有用:

import com.amazonaws.services.sqs.AmazonSQSClient
import com.amazonaws.services.sqs.model.SendMessageRequest  

val sqs = new AmazonSQSClient()

listOfObjects.foreach { obj =>
    val json = // convert obj to json

    sqs.sendMessage(new SendMessageRequest()
        .withQueueUrl("your queue url")
        .withMessageBody(json))
}