云主机

  • 云主机 > 使用指南 > SDK参考(TCP版本) >.NET >收发顺序消息

    收发顺序消息

    最近更新时间: 2019-09-05 19:45:20

    发送顺序消息

    发送顺序消息的示例代码如下:

    using System;
    using ons;
    public class OrderProducerExampleForEx
    {
        public OrderProducerExampleForEx()
        {
        }
        static void Main(string[] args) {
            // 配置您的账号,以下设置均可从控制台获取
            ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
            // AccessKey 身份验证,在消息队列管理控制台创建
            factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "Your access key");
            // SecretKey 身份验证,在消息队列管理控制台创建
            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 的消息。基本上,您每一个线程
            // 只需要一个生产者实例
            OrderProducer producer = ONSFactory.getInstance().createOrderProducer(factoryInfo);
            // 启动客户端实例
            producer.start();
            // 创建消息对象
            Message msg = new Message(factoryInfo.getPublishTopics(), "tagA", "Example message body");
            string shardingKey = "App-Test";
            for (int i = 0; i < 32; i++) {
                try
                {
                    SendResultONS sendResult = producer.send(msg, shardingKey);
                    Console.WriteLine("send success {0}", sendResult.getMessageId());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("send failure{0}", ex.ToString());
                }
            }
            // 在您的线程即将退出时,关闭生产者实例
            producer.shutdown();
        }
    }
    

    消费顺序消息的示例代码如下:

    using System;
    using System.Text;
    using System.Threading;
    using ons;
    namespace demo
    {
        public class MyMsgOrderListener : MessageOrderListener
        {
            public MyMsgOrderListener()
            {
            }
            ~MyMsgOrderListener()
            {
            }
            public override ons.OrderAction consume(Message value, ConsumeOrderContext context)
            {
                Byte[] text = Encoding.Default.GetBytes(value.getBody());
                Console.WriteLine(Encoding.UTF8.GetString(text));
                return ons.OrderAction.Success;
            }
        }
        class OrderConsumerExampleForEx
        {
            static void Main(string[] args)
            {
                // 配置您的账号,以下设置均可从控制台获取
                ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
                // AccessKey 身份验证,在消息队列管理控制台创建
                factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "Your access key");
                // SecretKey 身份验证,在消息队列管理控制台创建
                factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, "Your access secret");
                // 您在控制台创建的 Group ID
                factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, "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");
                // 创建生产者实例
                OrderConsumer consumer = ONSFactory.getInstance().createOrderConsumer(factoryInfo);
                // 订阅 Topic
                consumer.subscribe(factoryInfo.getPublishTopics(), "*",new MyMsgOrderListener());
                // 启动消费者实例
                consumer.start();
                // 让主线程睡眠一段时间
                Thread.Sleep(30000);
                // 不再使用时,关闭消费者实例
                consumer.shutdown();
            }
        }
    }
    
    以上内容是否对您有帮助?
  • Qvm free helper
    Close