コード例 #1
0
        static void Main(string[] args)
        {
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();

            factoryInfo.setFactoryProperty(factoryInfo.getProducerIdName(), "PID_1234");
            Console.WriteLine("ProducerId:{0}, \nConsumerId:{1},\nPublishTopics:{2},\nMsgContent:{3},\nAccessKey::{4},\nSecretKey::{5} ",
                              factoryInfo.getProducerId(), factoryInfo.getConsumerId(), factoryInfo.getPublishTopics(),
                              factoryInfo.getMessageContent(), factoryInfo.getAccessKey(), factoryInfo.getSecretKey());

            ONSFactory onsfactory = new ONSFactory();

            OrderProducer pProducer = onsfactory.getInstance().createOrderProducer(factoryInfo);

            pProducer.start();

            string key = "abc";

            for (int i = 0; i < 20; ++i)
            {
                Message msg = new Message("Ram_Topic_2", "TagA", "msg from for loop => " + i);
                try
                {
                    SendResultONS sendResult = pProducer.send(msg, key);
                    Console.WriteLine("=> send success : {0} ", sendResult.getMessageId());
                }
                catch (ONSClientException e)
                {
                    Console.WriteLine("\nexception of sendmsg:{0}", e.what());
                }
            }

            Thread.Sleep(1000 * 100);
            pProducer.shutdown();
        }
コード例 #2
0
ファイル: ProducerMessage.cs プロジェクト: omcdev/blockchain
        public void SendNormalMessage(T byteBody, string tags)
        {
            Message msg = new Message(factoryInfo.getPublishTopics(), tags, Newtonsoft.Json.JsonConvert.SerializeObject(byteBody));

            msg.setKey(Guid.NewGuid().ToString());
            SendResultONS sendResult = producer.send(msg);

            LogHelper.Info($"send success {sendResult.getMessageId()}");
        }
コード例 #3
0
 public void SendOrderMessage(string tag, string byteBody, string shardingKey)
 {
     try
     {
         Message       msg        = new Message(factoryInfo.getPublishTopics(), tag, byteBody);
         SendResultONS sendResult = orderProducer.send(msg, shardingKey);
         Console.WriteLine("send success {0}", sendResult.getMessageId());
     }
     catch (Exception ex)
     {
         Console.WriteLine("send failure{0}", ex.ToString());
     }
 }
コード例 #4
0
ファイル: ProducerMessage.cs プロジェクト: omcdev/blockchain
 /// <summary>
 /// 发送普通消息
 /// </summary>
 /// <param name="byteBody"></param>
 /// <param name="tags"></param>
 public void SendNormalMessage(string byteBody, string tags)
 {
     try
     {
         Message msg = new Message(factoryInfo.getPublishTopics(), tags, byteBody);
         msg.setKey(Guid.NewGuid().ToString());
         SendResultONS sendResult = producer.send(msg);
         Console.WriteLine("send success {0}", sendResult.getMessageId());
     }
     catch (Exception ex)
     {
         LogHelper.Error($"send failure {ex.ToString()}");
     }
 }
コード例 #5
0
        public static void SendMessage(byte[] msgBody)
        {
            Message msg = new Message(Ons_Topic, "", "");

            msg.setBody(msgBody, msgBody.Length);
            try
            {
                SendResultONS sendResult = _producer.send(msg);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }
コード例 #6
0
        public static void SendMessage(string msgBody, String tag = "RegisterLog")
        {
            // Message msg = new Message(Ons_Topic, tag, msgBody);
            Message msg = new Message(Ons_Topic, tag, msgBody);

            msg.setKey(Guid.NewGuid().ToString());
            try
            {
                SendResultONS sendResult = _producer.send(msg);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }
コード例 #7
0
 public void SendOrderMessage(string tag, List <T> byteBody, string shardingKey)
 {
     try
     {
         foreach (var item in byteBody)
         {
             Message       msg        = new Message(factoryInfo.getPublishTopics(), tag, Newtonsoft.Json.JsonConvert.SerializeObject(item));
             SendResultONS sendResult = orderProducer.send(msg, shardingKey);
             Console.WriteLine("send success {0}", sendResult.getMessageId());
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("send failure{0}", ex.ToString());
     }
 }
コード例 #8
0
        public static void SendOrderMessage(string msgBody, String tag = "RegisterLog", String key = "test")
        {
            Message msg = new Message(Ons_Topic, tag, msgBody);

            byte[] data = new byte[10];
            msg.setBody(data, 10);
            msg.setKey(Guid.NewGuid().ToString());
            try
            {
                SendResultONS sendResult = _orderproducer.send(msg, key);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }
コード例 #9
0
ファイル: ProducerMessage.cs プロジェクト: omcdev/blockchain
 /// <summary>
 /// 发送普通消息
 /// </summary>
 public void SendNormalMessage(List <T> byteBody, string tags)
 {
     try
     {
         foreach (var item in byteBody)
         {
             Message msg = new Message(factoryInfo.getPublishTopics(), tags, Newtonsoft.Json.JsonConvert.SerializeObject(item));
             msg.setKey(Guid.NewGuid().ToString());
             SendResultONS sendResult = producer.send(msg);
             LogHelper.Info($"send success {sendResult.getMessageId()}");
         }
     }
     catch (Exception ex)
     {
         LogHelper.Error($"send failure {ex.ToString()}");
     }
 }
コード例 #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            // 配置账号, 从控制台获取设置
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();

            // AccessKeyId 阿里云身份验证,在阿里云服务器管理控制台创建
            factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "Your access key");
            // AccessKeySecret 阿里云身份验证,在阿里云服务器管理控制台创建
            factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, "Your access secret");
            // 您在控制台创建的 Group ID
            factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, "GID_example");
            // 您在控制台创建的 Topic
            factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "T_example_topic_name");
            // 设置 TCP 接入域名,进入控制台的实例管理页面的“获取接入点信息”区域查看
            factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "NameSrv_Addr");
            // 设置日志路径
            factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "C://log");
            // 创建生产者实例
            // 说明:生产者实例是线程安全的,可用于发送不同 Topic 的消息。基本上,您每一个线程
            // 只需要一个生产者实例
            Producer producer = ONSFactory.getInstance().createProducer(factoryInfo);

            // 启动客户端实例
            producer.start();
            // 创建消息对象
            Message msg = new Message(factoryInfo.getPublishTopics(), "tagA", "Examplemessage body");

            msg.setKey(Guid.NewGuid().ToString());
            for (int i = 0; i < 32; i++)
            {
                try
                {
                    SendResultONS sendResult = producer.send(msg);
                    Console.WriteLine("send success {0}", sendResult.getMessageId());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("send failure{0}", ex.ToString());
                }
            }
            // 在您的线程即将退出时,关闭生产者实例
            producer.shutdown();
        }