项目作者: ashiksp

项目描述 :
RabbitMQ Queue Publisher & Consumer
高级语言: Ruby
项目地址: git://github.com/ashiksp/smart_que.git
创建时间: 2017-08-01T08:54:24Z
项目社区:https://github.com/ashiksp/smart_que

开源协议:MIT License

下载


SmartQue

Welcome to SmartQue gem! This gem uses bunny library to connect with RabbitMq message broker
to publish and consume messages with defined queues.

Installation

Add this line to your application’s Gemfile:

  1. gem 'smart-que', '~> 0.2.6'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install smart-que -v 0.2.6

Usage

  1. Setup RabbitMq
  2. Add smart-que gem to your Gemfile and perform bundle install
  3. Require smart_que in the application.rb file
  4. Create Publisher/Consumer classes & start publish/consume messages

SmartQue Publisher

SmartQue publisher is used to publish messages to the previously setup RabbitMq
server. All messages are converted to JSON format before publishing to the queue.
RabbitMq server details and queue lists can be configured as follows

  1. # File: config/initializers/smart_que.rb
  2. require 'smart_que'
  3. SmartQue.configure do |f|
  4. f.host = ENV[:rabbit_mq][:host']
  5. f.port = ENV[:rabbit_mq][:port']
  6. f.vhost = ENV[:rabbit_mq][:vhost']
  7. f.username = ENV[:rabbit_mq][:username']
  8. f.password = ENV[:rabbit_mq][:password']
  9. f.logger = Rails.logger
  10. f.queues = [
  11. 'default',
  12. 'sms_otp',
  13. 'fcm_push'
  14. ]
  15. end
  16. $publisher = SmartQue::Publisher.new
  17. # Add this below mentioned line to your application.rb
  18. # File : config/application.rb
  19. require 'smart_que'

After initializing SmartQue publisher, it can be accessed anywhere in the rails application
to publish messages to the RabbitMq server.

  1. $publisher.publish('sms_otp', { number: '+919898123123', message: 'Test Message' })

SmartQue unicast

  1. $publisher.unicast(queue_name, payload)

The unicast method will provide an option to publish message to any queue which are
not predefined with initialization configuration. The queue_name parameter can be any queue
name, which will be created and bound to direct exchange instantly if doesn’t exist.

  1. $publisher.unicast('sms_alert', { number: '+919898123123', message: 'Test Message' })

SmartQue multicast

  1. $publisher.multicast(topic_name, payload)

The multicast method will provide an option to multicast message to queues which are
not predefined with initialization configuration. The topic_name parameter can be any
topic name and message will be consumable by any queues which subscribe to this topic
name.

  1. $publisher.multicast('weather', { message: 'Test Message' })

SmartQue broadcast

  1. $publisher.broadcast(payload)

The broadcast method will provide an option to broadcast message to queues which are
not predefined with initialization configuration. The broadcst message will be consumable by
all queues which are bound to rabbitmq fanout exchange.

  1. $publisher.broadcast({ message: 'Broadcast Test Message' })

SmartQue Consumer

The consumer class should be inherited from SmartQue::Consumer, and each consumer will be
listening to a defined Queue. The queue name and run method should be defined properly
in each consumer class.

  1. class OtpSmsConsumer < SmartQue::Consumer
  2. QUEUE_NAME = 'sms.otp'
  3. def run(payload)
  4. # Payload: { number: '+919898123123', message: 'Test Message' }
  5. # Method implementation goes here
  6. puts payload
  7. end
  8. end

Note that, queue name words are separated by . while defining it with consumer class.
Consumer can be started by calling start method available for consumer instance as follows

  1. c = OtpSmsConsumer.new
  2. c.start

All consumer processes can be placed in a rake file, so that it can be started individually
with rake tasks.

  1. File : lib/tasks/consumer.rake
  2. # Tasks which related to rabbitmq broker.
  3. namespace :rabbitmq do
  4. desc "Run OTP sms worker"
  5. task :send_otp_sms => [:environment] do
  6. c = OtpSmsConsumer.new
  7. c.start
  8. end
  9. end
  10. # Task can be initiated by rake
  11. RAILS_ENV=staging bundle exec rake rabbitmq:send_otp_sms

SmartQue Exceptions

SmartQue will not retry automatic connection recovery after rabbitmq connection failure. It raises
SmartQue::ConnectionError when tcp connection failure happens. You can catch this exception to
identify connection failures and can perform necessary actions upon it.

RabbitMq Web Interface

You can enable rabbitmq_management plugin so that rabbitmq server informations
are viewed and managed via web console interface. Management plugin can be enabled
by running this command and can be accessed through web browser

  1. rabbitmq-plugins enable rabbitmq_management

More informations are available on RabbitMq management plugin documentations.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smart_que. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the SmartQue project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.