项目作者: renanpalmeira

项目描述 :
Pedestal interface for AWS SQS.
高级语言: Clojure
项目地址: git://github.com/renanpalmeira/pedestal.sqs.git
创建时间: 2019-07-12T04:24:59Z
项目社区:https://github.com/renanpalmeira/pedestal.sqs

开源协议:MIT License

下载


pedestal.sqs

Build Status
Clojars Project

A simple Pedestal interface for AWS SQS.

Requires Clojure 1.10.*, Java 1.8+ and Servlet 3.1

Usage

In your service map is necessary put ::sqs/client and ::sqs/listeners , optional ::sqs/configurations

SQS Client

Here is a shortcut to aws-api, to use a local sqs server follow https://github.com/cognitect-labs/aws-api#endpoint-override to configure AWS credentials https://github.com/cognitect-labs/aws-api#credentials

  1. {
  2. ;; read more in https://github.com/cognitect-labs/aws-api
  3. ::sqs/client {:region "us-east-1"
  4. :endpoint-override {:protocol :http
  5. :hostname "localhost"
  6. :port 9324}}
  7. }

SQS Listener

Time to be happy, here is just pass name of queue, a listener function to receive messages and configurations of queue (like response type, deletion policy)

  1. {
  2. ::sqs/listeners #{["foo-queue" foo-listener {:WaitTimeSeconds 20
  3. ::sqs/deletion-policy :always
  4. ::sqs/response-type :json}]
  5. ["bar-queue" bar-listener {::sqs/deletion-policy :on-success
  6. ::sqs/response-interceptors [sqs.interceptors/json-parser]}]
  7. ["egg-queue" egg-listener {:WaitTimeSeconds 10}]}
  8. }

Example listener function

  1. (defn foo-listener
  2. [{:keys [message]}]
  3. (prn message))
  4. ;; => {:MessageId "29d942a3-fd85-4f47-bf5d-d966f102364f"
  5. :ReceiptHandle "29d942a3-fd85-4f47-bf5d-d966f102364f#60b168b7-3a33-482e-a0e3-a9dd77b679b7"
  6. :MD5OfBody "9d822b36a135bc8d94a09b67128cb63b"
  7. :Body {:test "test"}}

Follow available queue configurations write by pedestal.sqs

  1. {
  2. ::sqs/deletion-policy :on-success ;; options :never, :on-success and :always, default is :never
  3. ::sqs/response-interceptors ``[sqs.interceptors/json-parser] ;; here we have access a put interceptors to manage received messages
  4. ::sqs/response-type :json ;; built-in :json, :transit-json, :transit-mgspack, default is string
  5. }

Another configurations come from aws-api, using (aws/doc client :ReceiveMessage) we have this configurations

  1. {:AttributeNames [:seq-of string],
  2. :MessageAttributeNames [:seq-of string],
  3. :MaxNumberOfMessages integer,
  4. :VisibilityTimeout integer,
  5. :WaitTimeSeconds integer,
  6. :ReceiveRequestAttemptId string}

SQS Listener - Deletion Policy

By default, aws-api don’t delete the messages when pass in for listener function, but inspired by spring aws deletion policy pedestal.sqs implement this feature, follow options:

  • :always when pedestal.sqs receive message before call your listener function, the message is deleted
  • :on-success when pedestal.sqs receive message after call your listener function, the message is deleted
  • :never your message will never delete by pedestal.sqs

SQS Listener - Built-in Interceptors

pedestal.sqs provides some interceptors that help process messages.

  • pedestal.sqs.interceptors/json-parser
  • pedestal.sqs.interceptors/transit-json-value
  • pedestal.sqs.interceptors/transit-msgpack-value
  • pedestal.sqs.interceptors/string-parser

A shortcut to built-in interceptors is ::sqs/response-type :json ;; or :transit-json, :transit-mgspack, default is string

Is something missing?

Use ::sqs/response-interceptors, just set a list of interceptors

  1. {
  2. ::sqs/listeners #{["bar-queue" bar-listener {::sqs/response-interceptors [sqs.interceptors/transit-json-parser]}]
  3. }

SQS Global configuration

This library provide configurations to manage all queues, follow options available:

  • :auto-create-queue? create queue if not found in your startup application, default is false
  • :auto-startup? start listen SQS messages in startup application, default is true

Full configuration example

Example of service map

  1. {
  2. ;; read more in https://github.com/cognitect-labs/aws-api
  3. ::sqs/client {:region "us-east-1"
  4. :endpoint-override {:protocol :http
  5. :hostname "localhost"
  6. :port 9324}}
  7. ::sqs/configurations {:auto-create-queue? true}
  8. ;; Arguments
  9. ;; queue-name (e.g. foo-queue)
  10. ;; listener function (e.g. foo-listener)
  11. ;; queue/listener configurations of library and aws-api (here a shortcut to (aws/doc :ReceiveMessage))
  12. ;;
  13. ;; Comments about listeners
  14. ;; reference of ::sqs/deletion-policy https://github.com/spring-cloud/spring-cloud-aws/blob/v2.1.2.RELEASE/spring-cloud-aws-messaging/src/main/java/org/springframework/cloud/aws/messaging/listener/SqsMessageDeletionPolicy.java#L45
  15. ::sqs/listeners #{["foo-queue" foo-listener {:WaitTimeSeconds 20
  16. ::sqs/deletion-policy :always
  17. ::sqs/response-type :json}]
  18. ["bar-queue" bar-listener {::sqs/deletion-policy :on-success
  19. ::sqs/response-interceptors [sqs.interceptors/json-parser]}]
  20. ["egg-queue" egg-listener {:WaitTimeSeconds 10}]}
  21. }

Example to valid sqs configurations and start sqs listeners

  1. (-> service/service
  2. sqs-listener/sqs-server ;; check sqs configurations
  3. sqs-listener/start ;; start sqs listeners
  4. server/create-server
  5. server/start)

Publish in a queue from pedestal route

If you want publish in a queue from a pedestal route request argument have :sqs-client and :queues (configured in your project, if is a external queue use (queue/get-queue-id sqs-client "queue-name"))

  1. (defn home-page
  2. [request]
  3. (let [sqs-client (:sqs-client request)
  4. queues (:queues request)]
  5. (messaging/send-message!
  6. sqs-client ;; same client configured by you and used internally of pedestal.sqs
  7. (get queues "bar-queue") ;; here a shortcut to queues configured in your project
  8. (messaging/to-json {:example "example"}))
  9. (ring-resp/response "Hello from pedestal.sqs!")))

Read more in https://github.com/RenanPalmeira/basic-pedestal-sqs-example

The messaging/send-message! has the arguments:

  • client is a aws/client
  • queue-urls string or list string of queue urls
  • message string
  • addons-payload a optional argument to add some attributes in AWS SQS payload

Namespaces

  • pedestal.sqs
  • pedestal.sqs.listener
  • pedestal.sqs.queue
  • pedestal.sqs.messaging
  • pedestal.sqs.interceptors