/// <summary> /// 获取生产者实例并启动它,它和生产者服务类实例不是一个东西,请勿混淆 /// </summary> /// <returns></returns> protected IONSProducer GetProducer() { IONSProducer producer = ONSHelper.ONSProducerList.Where(p => (p.Type == this.MessageType.ToString().ToUpper()) && (p.ProducerId == this.Pid)).FirstOrDefault(); if (producer == null) { //生产者对象不存在,则新建 lock (_lockHelper) { producer = ONSHelper.ONSProducerList.Where(p => (p.Type == this.MessageType.ToString().ToUpper()) && (p.ProducerId == this.Pid)).FirstOrDefault(); if (producer == null) { ONSFactoryProperty onsProducerFactoryProperty = new ONSFactoryProperty(); onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.AccessKey, _AliyunOnsAccessKey); onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.SecretKey, _AliyunOnsSecretKey); //用老的.net SDK按最新的RocketMQ架构,引入GroupId代替ProducerId和ConsumerId onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.ProducerId, this.Pid); onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.PublishTopics, this.Topic); producer = InitilizeProducer(onsProducerFactoryProperty); ONSHelper.ONSProducerList.Add(producer); producer.start(); } } } return(producer); }
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(); }
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); }
private static ONSFactoryProperty GetOnsFactoryProperty(AliyunMessageQueueFactoryOptions options) { var factoryInfo = new ONSFactoryProperty(); factoryInfo.setFactoryProperty(factoryInfo.getPublishTopicsName(), "null"); factoryInfo.setFactoryProperty(factoryInfo.getAccessKeyName(), options.AccessKey); factoryInfo.setFactoryProperty(factoryInfo.getSecretKeyName(), options.SecretKey); return(factoryInfo); }
/// <summary> /// 创建默认的客户端工厂属性 /// </summary> /// <returns></returns> private ONSFactoryProperty CreateDefaultFactoryProperty() { ONSFactoryProperty factoryProperty = new ONSFactoryProperty(); factoryProperty.setFactoryProperty(ONSFactoryProperty.AccessKey, AccessKeyId); factoryProperty.setFactoryProperty(ONSFactoryProperty.SecretKey, AccessKeySecret); factoryProperty.setFactoryProperty(ONSFactoryProperty.LogPath, this.LogPath); return(factoryProperty); }
//public OnsConsumer(string regionID, string accessKey, string secretKey, string topicID, string consumerID) //{ // Ons_RegionID = regionID; // Ons_AccessKey = accessKey; // Ons_SecretKey = secretKey; // TopicID = topicID; // ConsumerID = consumerID; //} private static ONSFactoryProperty getFactoryProperty(string accessKey, string secretKey, string topicID, string consumerID) { ONSFactoryProperty factoryInfo = new ONSFactoryProperty(); factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, accessKey); factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, secretKey); factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, topicID); factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, consumerID); factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "D://log"); return(factoryInfo); }
private static ONSFactoryProperty getFactoryProperty() { ONSFactoryProperty factoryInfo = new ONSFactoryProperty(); factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey); factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey); factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId); factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID); factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic); return(factoryInfo); }
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); }
public virtual PullConsumer createPullConsumer(ONSFactoryProperty factoryProperty) { global::System.IntPtr cPtr = ONSClient4CPPPINVOKE.ONSFactoryAPI_createPullConsumer(swigCPtr, ONSFactoryProperty.getCPtr(factoryProperty)); PullConsumer ret = (cPtr == global::System.IntPtr.Zero) ? null : new PullConsumer(cPtr, false); if (ONSClient4CPPPINVOKE.SWIGPendingException.Pending) { throw ONSClient4CPPPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public virtual TransactionProducer createTransactionProducer(ONSFactoryProperty factoryProperty, LocalTransactionChecker checker) { global::System.IntPtr cPtr = ONSClient4CPPPINVOKE.ONSFactoryAPI_createTransactionProducer(swigCPtr, ONSFactoryProperty.getCPtr(factoryProperty), LocalTransactionChecker.getCPtr(checker)); TransactionProducer ret = (cPtr == global::System.IntPtr.Zero) ? null : new TransactionProducer(cPtr, false); if (ONSClient4CPPPINVOKE.SWIGPendingException.Pending) { throw ONSClient4CPPPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
/// <summary> /// 获取配置 /// <para>作 者:蔡亚康</para> /// <para>创建时间:2019-03-14</para> /// </summary> /// <returns></returns> private ONSFactoryProperty GetFactoryProperty() { ONSFactoryProperty factoryInfo = new ONSFactoryProperty(); factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey); factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey); //factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_GroupId); factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_GroupId); factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, _topicId); factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, Ons_NameSrv); factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, GetLogPath()); return(factoryInfo); }
/// <summary> /// 创建生产者实例 /// </summary> /// <typeparam name="T">生产者服务基类类型</typeparam> /// <param name="assembly">生产者服务类所在程序集</param> /// <param name="type">生产者服务类的类型</param> /// <param name="messageType">消息类型BASE,ORDER,TRAN</param> /// <param name="func">生成生产者实例的委托代码</param> internal static void CreateProducer <T>(Assembly assembly, Type type, ONSMessageType messageType, Func <ONSFactoryProperty, string, string, IONSProducer> func) { if (type.BaseType.FullName.IndexOf(typeof(T).Name) >= 0) { //添加到生产者服务类实例列表 //ONSProducerServiceList.Add(type); object service = assembly.CreateInstance(type.FullName); ONSProducerServiceList.Add(service); //获取服务接口 IAbstractProducerService iservice = (IAbstractProducerService)service; //获取枚举对象 Enum topicTag = iservice.TopicTag; if (topicTag != null) { string serviceTopic = topicTag.GetType().Name; string topic = (_Environment + "_" + serviceTopic).ToUpper(); //string producerId = ("PID_" + topic).ToUpper(); string producerId = ("GID_" + topic).ToUpper(); IONSProducer producer = ONSProducerList.Where(p => (p.Type == messageType.ToString()) && (p.ProducerId == producerId)).FirstOrDefault(); if (producer == null) { //实例化ONSFactoryProperty ONSFactoryProperty onsProducerFactoryProperty = new ONSFactoryProperty(); onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.AccessKey, _AliyunOnsAccessKey); onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.SecretKey, _AliyunOnsSecretKey); //用老的.net SDK按最新的RocketMQ架构,引入GroupId代替ProducerId和ConsumerId onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.ProducerId, producerId); onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.PublishTopics, topic); if (_AliyunOnsProducerLogPath != "") { if (Directory.Exists(_AliyunOnsProducerLogPath)) { onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.LogPath, _AliyunOnsProducerLogPath); } } //获取生产者IONSProducer producer = func(onsProducerFactoryProperty, topic, producerId); //记录生产者初始化信息 DebugUtil.Debug("Topic:" + topic + ",ProducerId(" + producer.Type.ToString() + "):" + producer.ProducerId + "生产者new()"); //新增代理类ONSProducer实例到ONSProducerList中 ONSProducerList.Add(producer); } } } }
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(); }
/// <summary> RocketMQ管理器 </summary> public RocketMQManager(RocketMQItemConfig config) { DynamicGlobSetting.InitDynamicLib(); _factoryInfo = new ONSFactoryProperty(); if (config.AccessKey != null) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, config.AccessKey); } if (config.SecretKey != null) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, config.SecretKey); } if (config.ConsumerID != null) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, config.ConsumerID); } if (config.ProducerID != null) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, config.ProducerID); } if (config.Topic != null) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, config.Topic); } if (config.Server != null) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, config.Server); //_factoryInfo.setFactoryProperty(ONSFactoryProperty.ONSAddr, config.Server); } // 设置线程数 if (config.ConsumeThreadNums < 1) { config.ConsumeThreadNums = 1; } _factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumeThreadNums, config.ConsumeThreadNums.ToString()); // 默认值为ONSChannel.ALIYUN,聚石塔用户必须设置为CLOUD,阿里云用户不需要设置(如果设置,必须设置为ALIYUN) _factoryInfo.setOnsChannel(config.Channel); if (config.IsWriteLog) { _factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, SysPath.LogPath); } _factoryInfo.setFactoryProperty(ONSFactoryProperty.SendMsgTimeoutMillis, "3000"); }
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(); }
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(); }
public RocketMQPushConsumer(ONSFactoryProperty factoryInfo) { _factoryInfo = factoryInfo; }
public ProducerMessage() { factoryInfo = new ONSFactoryProperty(); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ONSFactoryProperty obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
/// <summary> /// 生产消息 /// </summary> /// <param name="factoryInfo">消息队列属性</param> public RocketMQProduct(ONSFactoryProperty factoryInfo) { _factoryInfo = factoryInfo; }
/// <summary> /// 初始化生产者实例,此方法会在GetProducer()中被调用 /// </summary> /// <param name="onsProducerFactoryProperty"></param> /// <returns></returns> protected abstract IONSProducer InitilizeProducer(ONSFactoryProperty onsProducerFactoryProperty);
/// <summary> /// 生成消费者实例 /// </summary> /// <typeparam name="T">消费者服务基类类型</typeparam> /// <param name="assembly">消费者服务类所在程序集</param> /// <param name="type">消费者服务类的类型</param> /// <param name="messageType">消息类型BASE,ORDER,TRAN</param> /// <param name="func">生成消费者实例的委托代码</param> internal static void CreateConsumer <T>(Assembly assembly, Type type, ONSMessageType messageType, Func <ONSFactoryProperty, string, string, Type, IONSConsumer> func) { if (type.BaseType.FullName.IndexOf(typeof(T).Name) >= 0) { //添加到消费者服务类实例列表 //ONSConsumerServiceList.Add(type); object service = assembly.CreateInstance(type.FullName); ONSConsumerServiceList.Add(service); //获取服务接口 IAbstractConsumerService iservice = (IAbstractConsumerService)service; //获取枚举数组对象 Enum[] topicTagList = iservice.TopicTagList; if (topicTagList != null && topicTagList.Length > 0) { foreach (Enum topicTag in topicTagList) { string serviceTopic = topicTag.GetType().Name; string serviceTag = topicTag.ToString(); string className = type.Name; string topic = (_Environment + "_" + serviceTopic).ToUpper(); string consumerId = ("CID_" + topic + "_" + _ApplicationAlias + "_" + className).ToUpper(); DebugUtil.Debug("consumerId:" + consumerId); IONSConsumer consumer = ONSConsumerList.Where(c => c.Type == messageType.ToString() && c.ConsumerId == consumerId).FirstOrDefault(); if (consumer == null) { //实例化ONSFactoryProperty ONSFactoryProperty onsConsumerFactoryProperty = new ONSFactoryProperty(); onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.AccessKey, _AliyunOnsAccessKey); onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.SecretKey, _AliyunOnsSecretKey); onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.ConsumerId, consumerId); //onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.PublishTopics, _ONSTopic); if (_AliyunOnsConsumerLogPath != "") { if (Directory.Exists(_AliyunOnsConsumerLogPath)) { onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.LogPath, _AliyunOnsConsumerLogPath); } } //获取消费者IONSConsumer consumer = func(onsConsumerFactoryProperty, topic, consumerId, type); //记录消费者初始化信息 DebugUtil.Debug("Topic:" + topic + ",ConsumerId(" + consumer.Type + "):" + consumer.ConsumerId + "消费者.new()"); //新增代理类ONSConsumer实例到ONSConsumerList中 ONSConsumerList.Add(consumer); } if (!consumer.TagList.Contains(serviceTag)) { consumer.TagList.Add(serviceTag); } } } } }
public AliyunMessageQueueFactory(AliyunMessageQueueFactoryOptions options) { _factory = new ONSFactory(); _factoryInstance = _factory.getInstance(); _factoryProperty = GetOnsFactoryProperty(options); }
public ConsumerMessage() { factoryInfo = new ONSFactoryProperty(); }