RabbitMQ RCP Simpleclient

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Concurrent;
using RabbitMQ.Client;
using RabbitMQ.Client.MessagePatterns;

using System.Threading;
using RabbitMQ.Client.Events;
using log4net;

//[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

namespace T1
{
   

    public partial class Form1 : Form
    {
        private static   ILog log = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
       

        private   IConnection connection;
        private   IModel channel;
        private   string replyQueueName;
        private   EventingBasicConsumer consumer;
        private  BlockingCollection<string> respQueue = new BlockingCollection<string>();
        private  IBasicProperties props;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendData();

        }

        private void SendData()
        {
             var factory = new ConnectionFactory() { HostName = "47.100.163.48", UserName = "admin", Password = "QMS@Adm06" };
           // var factory = new ConnectionFactory() { HostName = "192.168.246.1", UserName = "admin", Password = "QMS@Adm06" };
            factory.RequestedHeartbeat = 60000;
            factory.SocketReadTimeout = 60000;

            try
            {


                using (connection = factory.CreateConnection())
                {
                    using (channel = connection.CreateModel())
                    {
                        SimpleRpcClient clitent = new SimpleRpcClient(channel, "rpc_queue");
                        clitent.TimeoutMilliseconds = 90000;


                        clitent.TimedOut += RpcTimedOutHandler;
                        clitent.Disconnected += RpcDisconnectedHandler;

                        string str = "hehe world xxxxxxxxx";

                        byte[] by = System.Text.Encoding.Default.GetBytes(str);
                        byte[] rep = clitent.Call(by);
                        if (rep == null)
                        {
                            log.Error("Connection is fail");
                        }
                        else
                        {
                            string msg = System.Text.Encoding.Default.GetString(rep);
                            log.Info(msg);
                        }
                    }
                }

            }
            catch(Exception ex)
            {
                log.Error(ex.ToString());
            }
            finally
            {
                if(connection!=null)
                {
                    if (connection.IsOpen)
                    {
                        connection.Close();
                    }
                }
               
            }

 
        }


        private void RpcDisconnectedHandler(object sender, EventArgs e)
        {
            throw new Exception("RPC disconnect exception occured.");

        }

        private void RpcTimedOutHandler(object sender, EventArgs e)
        {
            connection.Close();

            log.Info("Close Connection");

        }



    }
}

猜你喜欢

转载自blog.csdn.net/farmwang/article/details/80295166
今日推荐