Example #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();
        }
Example #2
0
 /// <summary>
 ///     开启生产消息
 /// </summary>
 public void Start()
 {
     if (_producer == null)
     {
         // 获取实产者实例
         _producer = ONSFactory.getInstance().createProducer(_factoryInfo);
         _producer.start();
     }
 }
        protected override IONSProducer InitilizeProducer(ONSFactoryProperty onsProducerFactoryProperty)
        {
            //实例化Producer
            ons.Producer baseProducer = ONSFactory.getInstance().createProducer(onsProducerFactoryProperty);
            //实例化代理类ONSProducer
            IONSProducer producer = new ONSBaseProducer(this.Topic, this.Pid, baseProducer);

            return(producer);
        }
Example #4
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();
 }
Example #5
0
        public override void Start()
        {
            if (this.listener == null)
            {
                throw new NotFoundMessageListenerException();
            }

            consumer = ONSFactory.getInstance().createOrderConsumer(this.FactoryProperty);
            consumer.subscribe(Topic, SubExpression, listener);
            consumer.start();
        }
        protected override IONSProducer InitilizeProducer(ONSFactoryProperty onsProducerFactoryProperty)
        {
            //实例化ONSLocalTransactionChecker
            ONSLocalTransactionChecker checker = new ONSLocalTransactionChecker();
            //实例化TransactionProducer
            TransactionProducer transactionProducer = ONSFactory.getInstance().createTransactionProducer(onsProducerFactoryProperty, checker);
            //实例化代理类ONSTransactionProducer
            IONSProducer producer = new ONSTranProducer(this.Topic, this.Pid, transactionProducer);

            return(producer);
        }
Example #7
0
        private void Form1_Load(object sender, EventArgs e)
        {
            factoryInfo = new ONSFactoryProperty();
            factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerId);
            //factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
            factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "D://log/rocketmq/producer");

            LocalTransactionChecker myChecker = new MyLocalTransactionChecker();

            producer = ONSFactory.getInstance().createTransactionProducer(factoryInfo, myChecker);

            producer.start();
        }
Example #8
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();
        }
Example #9
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();
        }
Example #10
0
        /// <summary>
        /// 启动消息消费客户端
        /// </summary>
        public static void StartPushConsumer(string regionID, string accessKey, string secretKey, string topicID, string consumerID)
        {
            PushConsumer _consumer = ONSFactory.getInstance().createPushConsumer(getFactoryProperty(accessKey, secretKey, topicID, consumerID));

            _consumer.subscribe(topicID, "*", new ConsumerListener(consumerID));
            _consumer.start();

            ConsumerParam consumerParam = new ConsumerParam()
            {
                Ons_RegionID  = regionID,
                Ons_AccessKey = accessKey,
                Ons_SecretKey = secretKey,
                TopicID       = topicID,
                ConsumerID    = consumerID
            };

            _Consumer_TopicDict.Add(consumerID + String_Splite_Char + topicID, _consumer);

            //MyTimer watchConsumerAccumulate = new MyTimer();
            //watchConsumerAccumulate.Elapsed += ConsumerAccumulateEvent;
            //watchConsumerAccumulate.OnsConsumerParam = consumerParam;
            //watchConsumerAccumulate.Interval = 3000;
            //watchConsumerAccumulate.Start();
        }
 public AliyunMessageQueueFactory(AliyunMessageQueueFactoryOptions options)
 {
     _factory         = new ONSFactory();
     _factoryInstance = _factory.getInstance();
     _factoryProperty = GetOnsFactoryProperty(options);
 }
Example #12
0
 public void InitializeOrderProducer()
 {
     orderProducer = ONSFactory.getInstance().createOrderProducer(factoryInfo);
     orderProducer.start();
 }
Example #13
0
 public void InitializeNormalConsumer()
 {
     consumer = ONSFactory.getInstance().createPushConsumer(factoryInfo);
 }
Example #14
0
 public static void CreateOrderProducer()
 {
     _orderproducer = ONSFactory.getInstance().createOrderProducer(getFactoryProperty());
 }
Example #15
0
 public static void  CreatePushConsumer()
 {
     _consumer = ONSFactory.getInstance().createPushConsumer(getFactoryProperty());
 }
Example #16
0
 public override void Start()
 {
     producer = ONSFactory.getInstance().createProducer(this.FactoryProperty);
     producer.start();
 }
Example #17
0
 public void InitializeNormalProducer()
 {
     producer = ONSFactory.getInstance().createProducer(factoryInfo);
     producer.start();
 }
 /// <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();
 }
Example #19
0
 public TransactionProducerClient(string accessKeyId, string accessKeySecret, string producerId, Func <Message, TransactionStatus> checkFunc)
     : base(accessKeyId, accessKeySecret, producerId)
 {
     producer = ONSFactory.getInstance().createTransactionProducer(this.FactoryProperty, new ExtendedLocalTransactionChecker(checkFunc));
 }
Example #20
0
 /// <summary>
 /// 创建生产者
 /// <para>作    者:蔡亚康</para>
 /// <para>创建时间:2019-03-14</para>
 /// </summary>
 private void CreateProducer()
 {
     _producer = ONSFactory.getInstance().createProducer(GetFactoryProperty());
 }
Example #21
0
        /// <summary>
        /// 初始化属性
        /// </summary>
        static void InitializeProperties()
        {
            //实例化生产者的服务类列表
            ONSProducerServiceList = new List <object>();
            //实例化消费者的服务类列表
            ONSConsumerServiceList = new List <object>();
            //实例化生产者列表
            ONSProducerList = new List <IONSProducer>();
            //实例化消费者列表
            ONSConsumerList = new List <IONSConsumer>();
            //实例化Executer方法对应的委托实例字典
            ExecuterMethodDictionary = new ConcurrentDictionary <string, MethodInfo>();
            //实例化Checker方法对应的委托实例字典
            CheckerMethodDictionary = new ConcurrentDictionary <string, MethodInfo>();



            //*
            //获取当前应用域下的所有程序集
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            //找出所有程序集下带有[ONSConsumer]特性的所有方法,并放入自定义消费者方法列表中
            if (assemblies != null)
            {
                foreach (Assembly assembly in assemblies)
                {
                    Type[] types = null;
                    try
                    {
                        types = assembly.GetTypes();
                    }
                    catch (Exception e)
                    {
                        //通过vs2017编译后反射会报错,需要捕捉错误。忽略此处错误即可
                        //Microsoft.Build.Tasks.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
                        //尝试获取所有类时发生异常

                        /*
                         * string body = assembly.FullName + "尝试获取所有类时发生异常:" + e.ToString();
                         * SendDebugMail("ONSHelper.InitializeProperties方法反射时捕获异常", body);
                         * //*/
                    }

                    if (types != null)
                    {
                        //*
                        foreach (Type type in types)
                        {
                            if (type.BaseType != null)
                            {
                                if (type.BaseType.FullName != null)
                                {
                                    //原本事务消息是需要自动注册,因为ProducerService必须开发者自己实现。但是在2019年01月23日大更新后,老SDK在生成Producer时不再需要输入ProducerId了,但是TranProducer实例化时会报错。因此这里禁用事务消息功能。

                                    //获取当前应用作为"事务"类型生产者的相关信息
                                    CreateProducer <AbstractTranProducerService <object> >(assembly, type, ONSMessageType.TRAN, (onsProducerFactoryProperty, topic, producerId) =>
                                    {
                                        //实例化ONSLocalTransactionChecker
                                        ONSLocalTransactionChecker checker = new ONSLocalTransactionChecker();
                                        //实例化TransactionProducer
                                        TransactionProducer transactionProducer = ONSFactory.getInstance().createTransactionProducer(onsProducerFactoryProperty, checker);
                                        //实例化代理类ONSTransactionProducer
                                        return(new ONSTranProducer(topic, producerId, transactionProducer));
                                    });

                                    ////获取当前应用作为"顺序"类型生产者的相关信息
                                    //CreateProducer<AbstractOrderProducerService<object>>(assembly, type, ONSMessageType.ORDER, (onsProducerFactoryProperty, topic, producerId) =>
                                    //{
                                    //    //实例化OrderProducer
                                    //    OrderProducer orderProducer = ONSFactory.getInstance().createOrderProducer(onsProducerFactoryProperty);
                                    //    //实例化代理类ONSOrderProducer
                                    //    return new ONSOrderProducer(topic, producerId, orderProducer);
                                    //});

                                    ////获取当前应用作为"普通"类型生产者的相关信息
                                    //CreateProducer<AbstractBaseProducerService<object>>(assembly, type, ONSMessageType.BASE, (onsProducerFactoryProperty, topic, producerId) =>
                                    //{
                                    //    //实例化Producer
                                    //    Producer baseProducer = ONSFactory.getInstance().createProducer(onsProducerFactoryProperty);
                                    //    //实例化代理类ONSProducer
                                    //    return new ONSBaseProducer(topic, producerId, baseProducer);
                                    //});

                                    //获取当前应用作为"事务"类型消费者的相关信息
                                    CreateConsumer <AbstractTranConsumerService <object> >(assembly, type, ONSMessageType.TRAN, (onsConsumerFactoryProperty, topic, consumerId, classType) =>
                                    {
                                        //实例化PushConsumer
                                        PushConsumer pushConsumer = ONSFactory.getInstance().createPushConsumer(onsConsumerFactoryProperty);
                                        //实例化代理类ONSTransactionConsumer
                                        return(new ONSTranConsumer(topic, consumerId, pushConsumer, classType));
                                    });

                                    //获取当前应用作为"顺序"类型消费者的相关信息
                                    CreateConsumer <AbstractOrderConsumerService <object> >(assembly, type, ONSMessageType.ORDER, (onsConsumerFactoryProperty, topic, consumerId, classType) =>
                                    {
                                        //实例化OrderConsumer
                                        OrderConsumer orderConsumer = ONSFactory.getInstance().createOrderConsumer(onsConsumerFactoryProperty);
                                        //实例化代理类ONSOrderConsumer
                                        return(new ONSOrderConsumer(topic, consumerId, orderConsumer, classType));
                                    });

                                    //获取当前应用作为"普通"类型消费者的相关信息
                                    CreateConsumer <AbstractBaseConsumerService <object> >(assembly, type, ONSMessageType.BASE, (onsConsumerFactoryProperty, topic, consumerId, classType) =>
                                    {
                                        //实例化PushConsumer
                                        PushConsumer pushConsumer = ONSFactory.getInstance().createPushConsumer(onsConsumerFactoryProperty);
                                        //实例化代理类ONSTransactionConsumer
                                        return(new ONSBaseConsumer(topic, consumerId, pushConsumer, classType));
                                    });
                                }
                            }
                        }
                        //*/
                    }
                }
            }
            //*/
        }
Example #22
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ONSFactory obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Example #23
0
 public override void Start()
 {
     consumer = ONSFactory.getInstance().createPullConsumer(this.FactoryProperty);
     consumer.start();
 }
Example #24
0
 public static void CreateOrderConsumer()
 {
     _orderconsumer = ONSFactory.getInstance().createOrderConsumer(getFactoryProperty());
 }
Example #25
0
        /// <summary>
        /// 初始化属性
        /// </summary>
        static void InitializeProperties()
        {
            //实例化生产者的服务类列表
            ONSProducerServiceList = new List <object>();
            //实例化消费者的服务类列表
            ONSConsumerServiceList = new List <object>();
            //实例化生产者列表
            ONSProducerList = new List <IONSProducer>();
            //实例化消费者列表
            ONSConsumerList = new List <IONSConsumer>();
            //实例化Executer方法对应的委托实例字典
            ExecuterMethodDictionary = new ConcurrentDictionary <string, MethodInfo>();
            //实例化Checker方法对应的委托实例字典
            CheckerMethodDictionary = new ConcurrentDictionary <string, MethodInfo>();



            //*
            //获取当前应用域下的所有程序集
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            //找出所有程序集下带有[ONSConsumer]特性的所有方法,并放入自定义消费者方法列表中
            if (assemblies != null)
            {
                foreach (Assembly assembly in assemblies)
                {
                    Type[] types = assembly.GetTypes();
                    if (types != null)
                    {
                        foreach (Type type in types)
                        {
                            if (type.BaseType != null)
                            {
                                if (type.BaseType.FullName != null)
                                {
                                    //获取当前应用作为"事务"类型生产者的相关信息
                                    CreateProducer <AbstractTranProducerService <object> >(assembly, type, ONSMessageType.TRAN, (onsProducerFactoryProperty, topic, producerId) =>
                                    {
                                        //实例化ONSLocalTransactionChecker
                                        ONSLocalTransactionChecker checker = new ONSLocalTransactionChecker();
                                        //实例化TransactionProducer
                                        TransactionProducer transactionProducer = ONSFactory.getInstance().createTransactionProducer(onsProducerFactoryProperty, checker);
                                        //实例化代理类ONSTransactionProducer
                                        return(new ONSTranProducer(topic, producerId, transactionProducer));
                                    });

                                    ////获取当前应用作为"顺序"类型生产者的相关信息
                                    //CreateProducer<AbstractOrderProducerService<object>>(assembly, type, ONSMessageType.ORDER, (onsProducerFactoryProperty, topic, producerId) =>
                                    //{
                                    //    //实例化OrderProducer
                                    //    OrderProducer orderProducer = ONSFactory.getInstance().createOrderProducer(onsProducerFactoryProperty);
                                    //    //实例化代理类ONSOrderProducer
                                    //    return new ONSOrderProducer(topic, producerId, orderProducer);
                                    //});

                                    ////获取当前应用作为"普通"类型生产者的相关信息
                                    //CreateProducer<AbstractBaseProducerService<object>>(assembly, type, ONSMessageType.BASE, (onsProducerFactoryProperty, topic, producerId) =>
                                    //{
                                    //    //实例化Producer
                                    //    Producer baseProducer = ONSFactory.getInstance().createProducer(onsProducerFactoryProperty);
                                    //    //实例化代理类ONSProducer
                                    //    return new ONSBaseProducer(topic, producerId, baseProducer);
                                    //});

                                    //获取当前应用作为"事务"类型消费者的相关信息
                                    CreateConsumer <AbstractTranConsumerService <object> >(assembly, type, ONSMessageType.TRAN, (onsConsumerFactoryProperty, topic, consumerId, classType) =>
                                    {
                                        //实例化PushConsumer
                                        PushConsumer pushConsumer = ONSFactory.getInstance().createPushConsumer(onsConsumerFactoryProperty);
                                        //实例化代理类ONSTransactionConsumer
                                        return(new ONSTranConsumer(topic, consumerId, pushConsumer, classType));
                                    });

                                    //获取当前应用作为"顺序"类型消费者的相关信息
                                    CreateConsumer <AbstractOrderConsumerService <object> >(assembly, type, ONSMessageType.ORDER, (onsConsumerFactoryProperty, topic, consumerId, classType) =>
                                    {
                                        //实例化OrderConsumer
                                        OrderConsumer orderConsumer = ONSFactory.getInstance().createOrderConsumer(onsConsumerFactoryProperty);
                                        //实例化代理类ONSOrderConsumer
                                        return(new ONSOrderConsumer(topic, consumerId, orderConsumer, classType));
                                    });

                                    //获取当前应用作为"普通"类型消费者的相关信息
                                    CreateConsumer <AbstractBaseConsumerService <object> >(assembly, type, ONSMessageType.BASE, (onsConsumerFactoryProperty, topic, consumerId, classType) =>
                                    {
                                        //实例化PushConsumer
                                        PushConsumer pushConsumer = ONSFactory.getInstance().createPushConsumer(onsConsumerFactoryProperty);
                                        //实例化代理类ONSTransactionConsumer
                                        return(new ONSBaseConsumer(topic, consumerId, pushConsumer, classType));
                                    });
                                }
                            }
                        }
                    }
                }
            }
            //*/
        }
Example #26
0
 public void InitializeOrderConsumer()
 {
     orderConsumer = ONSFactory.getInstance().createOrderConsumer(factoryInfo);
 }
Example #27
0
 public void InitializeOrderProducer()
 {
     myChecker           = new MyLocalTransactionChecker();
     transactionProducer = ONSFactory.getInstance().createTransactionProducer(factoryInfo, myChecker);
     transactionProducer.start();
 }