Git Product home page Git Product logo

duct-rabbitmq's Introduction

Duct RabbitMQ

Integrant methods for connecting to a RabbitMQ server via Langohr.

Installation

To install, add the following to your project :dependencies

[viebel/rabbitmq "0.1.2"]

Usage

This library provides:

  • a multimethod for :duct.amqp.rabbitmq/langohr that initiates the connection based on the settings.
  • a multimethod for :duct.amqp.rabbitmq/langohr-consumers that subscribes handlers on queues.
  • a multimethod for :duct.amqp.rabbitmq/langohr-producers that creates publish functions for producers.

Connection

URI

:duct.amqp.rabbitmq/langohr {:host "localhost"
                             :port 5672
                             :username "guest"
                             :password "guest"
                             :vhost "/"}

Consumers

:duct.amqp.rabbitmq/langohr-consumers {:consumers [{:queue "my-queue"
                                                    :declare-queue {:durable false
                                                                    :exclusive false
                                                                    :auto-delete true
                                                                    :arguments {}}
                                                     :handler #ig/ref :my-prj.handler/handle-msg}]
                                       :connection #ig/ref :duct.amqp.rabbitmq/langohr}
:my-prj.handler/handle-msg {}

Note: You can optionally pass a duct logger. If you do so, a message will be logged for each queue subscriber, when it subscribes.

:duct.amqp.rabbitmq/langohr-consumers {:consumers [{:queue "my-queue"
                                                    :declare-queue {:durable false
                                                                    :exclusive false
                                                                    :auto-delete true
                                                                    :arguments {}}
                                                     :handler #ig/ref :my-prj.handler/handle-msg}]
                                       :connection #ig/ref :duct.amqp.rabbitmq/langohr
                                       :logger #ig/reg :duct/logger}
:my-prj.handler/handle-msg {}
(defmethod ig/init-key :my-prj.handler/handle-msg [_ _]
  (fn  [ch {:keys [content-type delivery-tag type] :as meta} ^bytes payload]
    (println (format "[consumer] Received a message: %s, delivery tag: %d, content type: %s, type: %s" (String. payload "UTF-8") delivery-tag content-type type))))

Instead of :connection, you can have :connection-settings like this:

:duct.amqp.rabbitmq/langohr-consumers {:consumers [{:queue "my-queue"
                                                    :declare-queue {:durable false
                                                                    :exclusive false
                                                                    :auto-delete true
                                                                    :arguments {}}
                                                     :handler #ig/ref :my-prj.handler/handle-msg}]
                                       :connection-settings {:host "localhost"
                                       :port 5672
                                       :username "guest"
                                       :password "guest"
                                       :vhost "/"}
:my-prj.handler/handle-msg {}

Producers

:duct.amqp.rabbitmq/langohr-producers {:producers [{:name "my-producer"
                                                    :exchange "my-excahnge"
                                                    :routing-key "my-key"}]
                                       :connection #ig/ref :duct.amqp.rabbitmq/langohr}

The return value is a map whose keys are the name of the producers (left as strings) and hte values and the values are a map {:publish-fn fun :ch ch}, where fun is a function that publishes to the corresponding exchange and routing key according to langohr.basic/publish semantics, where the two first parameters are fixed according to the specified exchange and routing key in the settings.

(def publish (get-in system [:duct.amqp.rabbitmq/langohr-producers "my-producer" :publish-fn]))
(publish "Hello!" {:content-type "text/plain" :type "greetings.hi"})

Instead of :connection, you can have :connection-settings like we showed in the Consumers section.

Example

Consider an application that passes messages to itslef through RabbitMQ.

The duct config might look like this:

{:duct.amqp.rabbitmq/langohr {:host "localhost"
                             :port 5672
                             :username "guest"
                             :password "guest"
                             :vhost "/"}
:duct.amqp.rabbitmq/langohr-consumers {:consumers [{:queue "my-queue"
                                                    :declare-queue {:durable false
                                                                    :exclusive false
                                                                    :auto-delete true
                                                                    :arguments {}}
                                                     :handler #ig/ref :my-prj.handler/handle-msg}]
                                       :connection #ig/ref :duct.amqp.rabbitmq/langohr}
:my-prj.handler/handle-msg {}
:duct.amqp.rabbitmq/langohr-producers {:producers [{:name "my-producer"
                                                    :exchange "my-excahnge"
                                                    :routing-key "my-key"}]
                                       :connection #ig/ref :duct.amqp.rabbitmq/langohr}

}

And the producers and consumers might be initialized like this:

(defmethod ig/init-key :my-prj.handler/handle-msg [_ _]
  (fn  [ch {:keys [content-type delivery-tag type] :as meta} ^bytes payload]
    (println (format "[consumer] Received a message: %s, delivery tag: %d, content type: %s, type: %s" (String. payload "UTF-8") delivery-tag content-type type))))
    
(def publish (get-in system [:duct.amqp.rabbitmq/langohr-producers "my-producer" :publish-fn]))

(publish "Hello!" {:content-type "text/plain" :type "greetings.hi"})

For more information using Langohr, you can start with their getting started webpage.

License

Copyright © 2019 Yehonathan Sharvit

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

duct-rabbitmq's People

Contributors

viebel avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

duct-rabbitmq's Issues

Integrant Keys for Channels

Integrant keys are defined for a Langohr Connection, and the keys for Produces/Consumers create efficient Channels efficiently.

Simpler apps, e.g. that make use only of the "Pull API", would need Channels to be available for the lifecycle of the app.

Would you accept a PR that adds :duct.amqp.rabbitmq.langohr/channel keys? I'm thinking it could be defined to take a config map of either an existing Connection object, or the same config map as a Connection, depending on how many Connections a user anticipates they'll need in their app (many or one), i.e.

;; Create new Channel against existing Connection, halt closes the Channel
:duct.amqp.rabbitmq.langohr/channel 
{:connection conn}

or

;; Create new Channel against new Connection, halt closes Channel and Connection
:duct.amqp.rabbitmq.langohr/channel 
{:host "localhost"
 :port 5672
 :username "guest"
 :password "guest"
 :vhost "/"}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.