rabbitmq中python和ruby通信

来源:互联网 发布:心动网络官网电脑版 编辑:程序博客网 时间:2024/06/05 21:18

      前面一章已经介绍了rabbitmq在python中传递消息,那么作为比较强大的rabbitmq中间件,他如何实现在ruby上传递消息以及和python中间传递消息呢?

      1.安装rabbtmq的服务器,(前一篇已经介绍过了)

      2. gem install bunny   --安装bunny的gem包,这样就可以在客户端用ruby访问了

      我们写一个send.rb

   require "bunny"     --引入bunny
   conn = Bunny.new(:hostname => "localhost")   conn.start                        --链接服务器
   ch = conn.create_channel          --创建通道   
   q    = ch.queue("hello")         --创建队列hello   ch.default_exchange.publish("Hello World!", :routing_key => q.name)   puts " [x] Sent 'Hello World!'"
conn.close

然后在写一个 receive.rb
   require "bunny"
   conn = Bunny.new   conn.start   ch   = conn.create_channel   q    = ch.queue("hello")
   puts " [*] Waiting for messages in #{q.name}. To exit press CTRL+C"   q.subscribe(:block => true) do |delivery_info, properties, body|      puts " [x] Received #{body}"      # cancel the consumer to exit      delivery_info.consumer.cancel   end 好了,运行下就好了!
$ ruby send.rb
" [x] Sent 'Hello World!'"

$ ruby receive.rb 
”[x] Received Hello World!"这是ruby和ruby之间通信,那么我们在加入python的代码

send.py

import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters(        host='localhost'))channel = connection.channel()channel.queue_declare(queue='hello')channel.basic_publish(exchange='',                      routing_key='hello',                      body='Hello World!')print " [x] Sent 'Hello World!'"connection.close()
我们在运行下
python send.py
" [x] Sent 'Hello World!'"
ruby receive.rb
" [x] Receive 'Hello World!'"





 







原创粉丝点击