zoukankan      html  css  js  c++  java
  • rabbitmqmessage(C#)

    1.安装Erlang Windows Binary File

    2.安装rabbitmq-server(windows)rabbitmq-server-3.5.4.exe

     参考:http://www.rabbitmq.com/install-windows.html

    sendMessage code:

     1             var factory = new ConnectionFactory() { HostName = "localhost" };
     2             using (var connection = factory.CreateConnection())
     3             {
     4                 using (var channel = connection.CreateModel())
     5                 {
     6                     channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
     7                     string message = "你好 mq!";
     8                     var body = Encoding.UTF8.GetBytes(message);
     9                     channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
    10                     Console.WriteLine(string.Format("发送消息:{0}", message));
    11                     Console.ReadLine();
    12                 }
    13             }

    receiveMessage code:

     1             var factory = new ConnectionFactory() {  HostName="localhost"};
     2             using (var connection=factory.CreateConnection())
     3             {
     4                 using (var channel=connection.CreateModel())
     5                 {
     6                     channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
     7 
     8                     var consumer = new EventingBasicConsumer(channel);
     9                     consumer.Received += (model, ea) =>
    10                     {
    11                         var body = ea.Body;
    12                         string message = Encoding.UTF8.GetString(body);
    13                         Console.WriteLine(string.Format("接收消息:{0}", message));
    14                     };
    15                     channel.BasicConsume(queue: "hello", noAck: true, consumer: consumer);
    16 
    17                     Console.WriteLine("结束!");
    18                     Console.ReadLine();
    19                 }
    20             }

    具体项目中应用,都会用到开源的定义EasyNetQ(通过Nuget Packages获得http://easynetq.com/)

    参考用法:

       public class MqProvider
        {
            private static readonly MqProvider provider = new MqProvider();
            private static readonly string mqconnstr = "localhost";
            private static readonly IBus bus;
            private static readonly IExchange exchange;
            private static readonly int RedisCacheTimeSpan;
    
            private MqProvider() { }
    
            public static MqProvider Instance
            {
                get { return provider; }
            }
    
            static MqProvider()
            {
                bus = RabbitHutch.CreateBus(mqconnstr);
                exchange = bus.Advanced.ExchangeDeclare("my-exchange", "topic");
                IQueue queue = bus.Advanced.QueueDeclare("my-queue ");
                bus.Advanced.Bind(exchange, queue, "rote-key");
            }
    
            public bool Test()
            {
                return bus != null && exchange != null;
            }
    
            public void SendMessage(string message)
            {
                bus.Advanced.PublishAsync<string>(exchange, "rote-key", false, false, new Message<string>(message));
            }
        }
  • 相关阅读:
    在线支付模块小结
    Tomcat服务器热启动,修改项目源代码时不需要每次都重启Tomcat
    使用myeclipse进行hibernate快速开发
    hibernate的核心类和接口
    Hibernate手动配置
    Java的字符串md5加密和文件md5
    JDBC操作mysql数据库(SqlHelper类封装)
    yum报错[Errno 14] PYCURL ERROR 22(更换yum源)
    Ajax技术
    手动配置开发struts项目
  • 原文地址:https://www.cnblogs.com/lzzhang/p/4789428.html
Copyright © 2011-2022 走看看