shitou's blog 
Home About Feed | MIX BOY 塌客

Tags "AMQP"

21
Jan

Ruby + AMQP + RabbitMQ Example by shitou

0

这里记录下ruby下AMQP的使用,支持AMQP的开源broker比较多,比较有名的是ActiveMQ, 是Apache下的一个项目,这里采用的是RabbitMQ, RabbitMQ是Erlang写的,因为Erlang对高并发的支持非常擅长,因此RabbitMQ的表现自然也不逊色,平台这里采用的是Debian, 在安装Rabbit之前首先要安装Erlang, 之前有些过一片关于Erlang安装的文章, Debian上使用apt安装就OK了,这里就不多说了, 安装完后RabbitMQ将会自动建立一个域,测试账户和密码test,然后通过

/etc/init.d/rabbitmq-server start

启动就行了, 可以随时查看/var/log/rabbitmq下的日志查看运行状况

 

 

这里采用的ruby接口库就是amqp, 可以通过gem直接安装


发送脚本, producer.rb:

%w{rubygems amqp mq}.each { |b| require b }

MQ_SERVER = '192.168.0.113'
MQ_PORT = 5672
MQ_USER = 'guest'
MQ_PWD = 'guest'

begin
EM.run {
  conn = AMQP.connect(:host => MQ_SERVER, :port => MQ_PORT, :user => MQ_USER, :pass => MQ_PWD)
  channel = mp_conn = MQ.new(conn)
  channel.queue('mp_queue').publish(Marshal.dump(['mp', '', '13718781273', 'hello, shitou']))
  conn.close
}
rescue AMQP::Error
end


接收的脚本,consumer.rb:

%w{rubygems amqp mq net/http uri active_record}.each { |b| require b }

MQ_SERVER = '192.168.0.113'
MQ_PORT = 5672
MQ_USER = 'guest'
MQ_PWD = 'guest'

EM.run {
  conn = AMQP.connect(:host => MQ_SERVER, :port => MQ_PORT, :user => MQ_USER, :pass => MQ_PWD)
  channel = mp_conn = MQ.new(conn)
  queue = MQ::Queue.new(channel, 'mp_queue') #mp_queue is the queue we defined

  queue.subscribe do |header, msg|
    data = Marshal.load(msg)
    mobilephone, text = data[2], data[3]
    puts "send to NO.: #{mobilephone}, #{text}"
  end
 
  #mp.publish(Marshal.dump('hello shitou'))
  #conn.close { EM.stop_event_loop }
  #AMQP.stop { EM.stop }
}


Tags: AMQP

2009-01-21 17:28:03, 912 reviews

send to mailbox

Your email:

Tags

U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • iPhone[17]
  • Erlang[4]
  • google[8]
  • 生活[38]
  • 音乐[11]
  • 电影[11]
  • linux[20]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[5]
  • ruby[18]
  • ruby on rails[27]
  • 开源[3]

Episode

  • iPhoneException
  • shell
  • thread
  • memcache

Recent Comments

  • comment6, viagra bez receptu, revelation, ambie...
  • comment5, http://www.freecodesource.com/user/pr...
  • comment4, meridia medicine, :-<>], plavix clopi...
  • comment2, viagra calgary, resin, paxil vs wellb...
  • comment2, codeine promethazine, conclusion, buy...
  • comment2, clomid ovarian cysts, fronted, oxycod...
  • comment2, http://www.freecodesource.com/user/pr...
  • comment3, cipro allergy, :-E, weed and hydrocod...
  • comment2, http://www.freecodesource.com/user/pr...
  • comment4, vicodin cr, chocolate, premarin cance...

Popular Posts

  • MySQL Innodb备份
  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Lighttpd配置参数
  • iPhone上的HelloWorld终于跑起来了
  • Etag和Expire

Recommended Posts

  • Mai Kuraki -永远より ながく
  • U-ka saegusa IN db Final Best
  • Heaven Can Wait - Charlotte Gainsbou
  • Ruby遍历MemCached的key
  • Norah Jones - The fall
  • 请记得仰望梦想的姿势
  • Shell: 统计MySQL InnoDB表的大小
  • Rails Benchmark
  • 发送异常到邮箱
  • I Miss Nobody
  • Music4u, Vol. 1
  • my macbook
  • Mai Kuraki-Beautiful
  • 10首最伤情英文歌曲精选
  • Mai Kuraki - PUZZLE/Revive

Friends' blogs

  • levy
  • sphance
  • andreas

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 51144
  • 今天访问: 8
  • 日志: 172
  • 评论: 124
  • 音乐: 9
  • 用户: 148


 

just DO NOT support IE

close