Advertisement
vitareinforce

RMQ Class

Jan 31st, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.68 KB | None | 0 0
  1. using RabbitMQ.Client;
  2. using RabbitMQ.Client.Events;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace HMD_Holo
  11. {
  12.     class RabbitMQServices
  13.     {
  14.         private string data = "";
  15.  
  16.         /* KFX GPS Tracker Params */
  17.         private string user = "ARmachine";
  18.         private string pass = "12345";
  19.         private string vhost = "/ARX";
  20.         private string exchange = "kfx_exchanges";
  21.         private string queue = "kfx.plane";
  22.         private string routingkey = "kfx.plane";
  23.  
  24.  
  25.         private string hostname = "localhost";
  26.        
  27.         private int port = 5672;
  28.  
  29.         private ConnectionFactory connectionFactory;
  30.         private IConnection connection;
  31.         private IModel channel;
  32.  
  33.         private EventingBasicConsumer consumer;
  34.  
  35.         public RabbitMQServices(string queue_name = null, string routing_key = null)
  36.         {
  37.             Debug.WriteLine("Begin");
  38.             connectionFactory = new ConnectionFactory();
  39.             connectionFactory.HostName = hostname;
  40.             connectionFactory.Port = port;
  41.             connectionFactory.UserName = user;
  42.             connectionFactory.Password = pass;
  43.             connectionFactory.VirtualHost = vhost;
  44.  
  45.             if(queue_name != null)
  46.             {
  47.                 queue = queue_name;
  48.             }
  49.             if(routing_key != null)
  50.             {
  51.                 routingkey = routing_key;
  52.             }
  53.  
  54.             connect();
  55.         }
  56.  
  57.         ~RabbitMQServices()
  58.         {
  59.             disconnect();
  60.         }
  61.  
  62.         public async void connect()
  63.         {
  64.             connection = connectionFactory.CreateConnection();
  65.             Debug.WriteLine("Connection : " + connection.IsOpen);
  66.         }
  67.  
  68.         public bool connectionStatus()
  69.         {
  70.             return connection.IsOpen;
  71.         }
  72.  
  73.         public async void createChannel()
  74.         {
  75.            
  76.             if (connection.IsOpen)
  77.             {
  78.                 channel = connection.CreateModel();
  79.                 Debug.WriteLine("Channel : " + channel.IsOpen);
  80.             }
  81.  
  82.             if (channel.IsOpen)
  83.             {
  84.                 Debug.WriteLine("Declare Exchange");
  85.                 channel.ExchangeDeclare(exchange: exchange,
  86.                                         type: "topic",
  87.                                         durable: true);
  88.  
  89.                 Debug.WriteLine("Declare Queue");
  90.                 channel.QueueDeclare(queue: queue,
  91.                                      durable: false,
  92.                                      exclusive: false,
  93.                                      autoDelete: false,
  94.                                      arguments: null);
  95.  
  96.                 Debug.WriteLine("Bind Queue");
  97.                 channel.QueueBind(queue: queue,
  98.                                   exchange: exchange,
  99.                                   routingKey: routingkey);
  100.  
  101.                 Debug.WriteLine("Declare Consuming Queue Process");
  102.                 consumer = new EventingBasicConsumer(channel);
  103.  
  104.                 //debugging here
  105.                 Debug.WriteLine("Begin Reveiving data");
  106.  
  107.                 consumer.Received += (model, ea) =>
  108.                 {
  109.                     Debug.WriteLine("Retriving......");
  110.                     var body = ea.Body;
  111.                     data = Encoding.UTF8.GetString(body);
  112.                     Debug.WriteLine("Data : " + data);
  113.                 };
  114.  
  115.                 channel.BasicConsume(queue: queue,
  116.                                     noAck: true,
  117.                                     consumer: consumer);
  118.  
  119.             }
  120.            
  121.         }
  122.  
  123.         public string getData()
  124.         {
  125.             /*
  126.             using (channel = connection.CreateModel())
  127.             {
  128.                 Debug.WriteLine("Declare Exchange");
  129.                 channel.ExchangeDeclare(exchange: exchange,
  130.                                         type: "topic",
  131.                                         durable: true);
  132.  
  133.                 Debug.WriteLine("Declare Queue");
  134.                 channel.QueueDeclare(queue: queue,
  135.                                      durable: false,
  136.                                      exclusive: false,
  137.                                      autoDelete: false,
  138.                                      arguments: null);
  139.  
  140.                 Debug.WriteLine("Bind Queue");
  141.                 channel.QueueBind(queue: queue,
  142.                                   exchange: exchange,
  143.                                   routingKey: routingkey);
  144.  
  145.                 Debug.WriteLine("Declare Consuming Queue Process");
  146.                 consumer = new EventingBasicConsumer(channel);
  147.  
  148.                 //debugging here
  149.                 Debug.WriteLine("Begin Reveiving data");
  150.  
  151.                 consumer.Received += (model, ea) =>
  152.                 {
  153.                     Debug.WriteLine("Retriving......");
  154.                     var body = ea.Body;
  155.                     data = Encoding.UTF8.GetString(body);
  156.                     Debug.WriteLine("Data : " + data);
  157.                 };
  158.  
  159.                 channel.BasicConsume(queue: queue,
  160.                                     noAck: true,
  161.                                     consumer: consumer);
  162.  
  163.             }
  164.             */
  165.             return data;
  166.         }
  167.  
  168.         public async void disconnect()
  169.         {
  170.             channel = null;
  171.  
  172.             if (connection.IsOpen)
  173.             {
  174.                 Debug.WriteLine("Disconnect");
  175.                 connection.Close();
  176.             }
  177.  
  178.             Debug.WriteLine("Dispose Connection");
  179.             connection.Dispose();
  180.             connection = null;
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement