Ejemplo n.º 1
0
 /// <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()}");
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 ///     消费订阅
 /// </summary>
 /// <param name="listen">消息监听处理</param>
 /// <param name="subExpression">标签</param>
 public void Start(MessageListener listen, string subExpression = "*")
 {
     if (_consumer != null)
     {
         throw new DcpException("当前已开启过该消费,无法重新开启,需先关闭上一次的消费(调用Close())。");
     }
     _consumer = ONSFactory.getInstance().createPushConsumer(_factoryInfo);
     _consumer.subscribe(_factoryInfo.getPublishTopics(), subExpression, listen);
     _consumer.start();
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     发送消息
        /// </summary>
        /// <param name="message">消息主体</param>
        /// <param name="shardingKey"></param>
        /// <param name="tag">消息标签</param>
        /// <param name="key">每条消息的唯一标识</param>
        public SendResultONS Send(string message, string shardingKey, string tag = null, string key = null)
        {
            if (string.IsNullOrWhiteSpace(tag))
            {
                tag = "";
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                key = Guid.NewGuid().ToString();
            }
            var msg = new Message(_factoryInfo.getPublishTopics(), tag, key, message);

            return(_producer.send(msg, shardingKey));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     发送消息
        /// </summary>
        /// <param name="message">消息主体</param>
        /// <param name="deliver">延迟消费ms</param>
        /// <param name="tag">消息标签</param>
        /// <param name="key">每条消息的唯一标识</param>
        public SendResultONS Send(string message, long deliver, string tag = null, string key = null)
        {
            if (string.IsNullOrWhiteSpace(tag))
            {
                tag = "";
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                key = Guid.NewGuid().ToString();
            }
            var msg = new Message(_factoryInfo.getPublishTopics(), tag, key, message);

            if (deliver > 0)
            {
                msg.setStartDeliverTime(deliver);
            }
            return(_producer.send(msg));
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        private void sendButton_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.FormClosing += ActiveForm_FormClosing;

            /*
             * Message msg = new Message(
             * //Message Topic
             *  factoryInfo.getPublishTopics(),
             *  //Message Tag
             *  "TagA",
             *  //Message Body
             *  factoryInfo.getMessageContent()
             * );
             * //*/

            Message msg = new Message(
                //Message Topic
                factoryInfo.getPublishTopics(),
                //Message Tag
                "TagA",
                //Message Body
                messageTextBox.Text.Trim()
                );

            // 设置代表消息的业务关键属性,请尽可能全局唯一
            // 以方便您在无法正常收到消息情况下,可通过 MQ 控制台查询消息并补发。
            // 注意:不设置也不会影响消息正常收发
            //msg.setKey("ORDERID_100");

            // 发送消息,只要不抛出异常,就代表发送成功
            try
            {
                LocalTransactionExecuter myExecuter = new MyLocalTransactionExecuter();
                SendResultONS            sendResult = producer.send(msg, myExecuter);
            }
            catch (Exception exception)
            {
                Console.WriteLine("\nexception of sendmsg:{0}", exception.ToString());
            }
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();

            factoryInfo.setFactoryProperty(factoryInfo.getConsumerIdName(), "CID_5678");

            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();

            OrderConsumer pConsumer = onsfactory.getInstance().createOrderConsumer(factoryInfo);

            MessageOrderListener msgListener = new MyMsgListener();

            pConsumer.subscribe("Ram_Topic_2", "*", ref msgListener);

            pConsumer.start();

            Thread.Sleep(10000 * 100);

            pConsumer.shutdown();
        }
Ejemplo n.º 9
0
 /// <summary>
 ///     消费订阅
 /// </summary>
 /// <param name="listen">消息监听处理</param>
 /// <param name="subExpression">标签</param>
 public void Start(MessageOrderListener listen, string subExpression = "*")
 {
     _consumer = ONSFactory.getInstance().createOrderConsumer(_factoryInfo);
     _consumer.subscribe(_factoryInfo.getPublishTopics(), subExpression, listen);
     _consumer.start();
 }