`
terryfeng
  • 浏览: 492317 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

RabbitMQ 安装配置和简单实例

阅读更多

安装ErLang运行环境

image

配置运行环境变量

image

启动服务

地址在:complete-rabbitmq-bundle-1.7.0\rabbitmq-server-windows-1.7.0\rabbitmq_server-1.7.0\sbin

image

image

一段实例代码

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RabbitMQ.Client;

namespace rabbits
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IConnection conn;
        IModel channel;
        private void button1_Click(object sender, EventArgs e)
        {
            conn = new ConnectionFactory().CreateConnection(Protocols.DefaultProtocol, this.textBox1.Text);
            channel = conn.CreateModel();
            channel.ExchangeDeclare(this.textBox2.Text, ExchangeType.Direct);
            channel.QueueDeclare(this.textBox3.Text);
            channel.QueueBind(this.textBox3.Text, this.textBox2.Text, "", false, null);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] message = System.Text.Encoding.UTF8.GetBytes(this.richTextBox1.Text);
            IBasicProperties props = channel.CreateBasicProperties();
            props.ContentType = "text/plain";
            props.DeliveryMode = 2;

            channel.BasicPublish(this.textBox2.Text, "", props, message);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool noask = false;
            BasicGetResult result = channel.BasicGet(this.textBox3.Text, noask);
            if (result == null)
            {
                
            }
            else
            {
                IBasicProperties props = result.BasicProperties;
                byte[] body = result.Body;
                this.richTextBox2.Text = System.Text.Encoding.UTF8.GetString(body);
                channel.BasicAck(result.DeliveryTag, false);
                    
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.channel != null && this.channel.IsOpen)
            {
                this.channel.Dispose();
            }
            if (this.conn != null && this.conn.IsOpen)
            {
                this.conn.Dispose();
            }
        }

        
    }
}

分享到:
评论
1 楼 luedipiaofeng 2010-11-02  
很有帮助的 

相关推荐

Global site tag (gtag.js) - Google Analytics