public WFMessageQueue_RabbitMQ(string ipaddress, int port, string queuename, WFMessageQueueType queuetype) { this.IPAddress = ipaddress; this.Port = port; this.QueueName = queuename; this.RoutingKey = this.QueueName + "consumer"; ConnectionFactory factory = new ConnectionFactory(); factory.HostName = this.IPAddress; factory.Port = this.Port; factory.Protocol = Protocols.FromEnvironment(); IConnection connection = factory.CreateConnection(); _model = connection.CreateModel(); _model.ExchangeDeclare(WFMessageQueue_RabbitMQ <T> .Exchange, ExchangeType.Direct); // _model.QueueBind(this.QueueName, WFMessageQueue_RabbitMQ<T>.Exchange, this.RoutingKey); if ((queuetype & WFMessageQueueType.Consumer) == WFMessageQueueType.Consumer) { _queue = _model.QueueDeclare(this.QueueName, false, false, false, null); _model.QueueBind(this.QueueName, WFMessageQueue_RabbitMQ <T> .Exchange, this.RoutingKey); #if !__USE_BASICGET _consumer = new QueueingBasicConsumer(_model); _model.BasicConsume(this.QueueName, true, _consumer); #endif } // else // { // _model.BasicPublish(WFMessageQueue_RabbitMQ<T>.Exchange, this.QueueName, null, System.Text.Encoding.Unicode.GetBytes("hello")); // _queue = _model.QueueDeclarePassive(this.QueueName); // } }
//[email protected]:queue@pstqueue:queuetype@rabbitmq public static IWFMessageQueue <U> CreateWFMessageQueue(string config, WFMessageQueueType messagequeuetype) { string queuehost = System.Net.Dns.GetHostName(); string queuetype = "msmq"; string queuename = string.Empty; string[] splitdata = config.Split(new char[] { ':' }); foreach (string split in splitdata) { string[] elemdata = split.Split(new char[] { '@' }); if (elemdata.Length == 2) { switch (elemdata[0]) { case "host": queuehost = elemdata[1]; break; case "queue": queuename = elemdata[1]; break; case "queuetype": queuetype = elemdata[1]; break; } } } string ipaddress = string.Empty; int port = 0; if (WFUtilities.SetHostAndIPAddress(queuehost, ref ipaddress)) { Type type = null; switch (queuetype) { case ".netqueue": type = typeof(System.Collections.Queue); break; case "msmq": type = typeof(MessageQueue); break; case "rabbitmq": type = typeof(QueueingBasicConsumer); port = 5672; break; default: break; } } return(CreateWFMessageQueue(ipaddress, port, queuename, messagequeuetype)); }
public WFTarget(WFTargetData wftargetdata, string assemblycache) { if (WFTarget.TargetQueues == null) { WFTarget.TargetQueues = new Dictionary <string, IWFMessageQueue>(); } if (wftargetdata.AssemblyType != string.Empty && wftargetdata.AssemblyDll != string.Empty) { this.AssemblyPath = Directory.GetCurrentDirectory(); // if (wftargetdata.AssemblyType != "ProcessorData" && !string.IsNullOrEmpty(assemblycache)) if (!string.IsNullOrEmpty(assemblycache)) { this.AssemblyPath = string.Format(@"{0}\{1}", assemblycache, Path.GetFileNameWithoutExtension(wftargetdata.AssemblyDll)); } Assembly assembly = string.IsNullOrEmpty(wftargetdata.AssemblyDll) ? Assembly.GetExecutingAssembly() : Assembly.LoadFrom(this.AssemblyPath + @"\" + wftargetdata.AssemblyDll); if (assembly != null) { //The AssemblyResolve event is called when the common language runtime tries to bind to the assembly and fails. AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve); try { this.AssemblyType = assembly.GetType(assembly.GetTypes().Where(x => (x.Name.ToUpper() == wftargetdata.AssemblyType.ToUpper()) || (x.FullName.ToUpper() == wftargetdata.AssemblyType.ToUpper())).Select(x => x.FullName).FirstOrDefault()); this.AssemblyTypeInstance = assembly.CreateInstance(this.AssemblyType.FullName); } catch (Exception ex) { throw new Exception("", ex); } finally { currentDomain.AssemblyResolve -= new ResolveEventHandler(currentDomain_AssemblyResolve); } } else { throw new Exception(""); } } if (wftargetdata.QueueName != string.Empty) { string ip = string.Empty; if (WFUtilities.SetHostAndIPAddress(wftargetdata.QueueHost, ref ip)) { Type type = null; int port = 0; WFMessageQueueType publisherorconsumer = WFMessageQueueType.None; switch (wftargetdata.QueueType) { case ".netqueue": type = typeof(System.Collections.Queue); break; case "msmq": type = typeof(MessageQueue); break; case "rabbitmq": type = typeof(QueueingBasicConsumer); port = 5672; publisherorconsumer = WFMessageQueueType.Publisher; if (this is WFSrc) { publisherorconsumer = WFMessageQueueType.Consumer; } break; default: break; } if (WFTarget.TargetQueues.ContainsKey(wftargetdata.QueueName)) { this.MessageQueue = WFTarget.TargetQueues[wftargetdata.QueueName]; } else { Type genericqueuetype = typeof(IWFMessageQueueFactory <,>).MakeGenericType(new[] { type, this.AssemblyType == null ? typeof(ProcessorData) : this.AssemblyType }); this.MessageQueue = (IWFMessageQueue)genericqueuetype.GetMethod(WFTarget.CreateWFMessageQueue, new object[] { ip, port, wftargetdata.QueueName, publisherorconsumer }.Select(p => p.GetType()).ToArray()).Invoke(null, new object[] { ip, port, wftargetdata.QueueName, publisherorconsumer }); WFTarget.TargetQueues[wftargetdata.QueueName] = this.MessageQueue; } // genericqueuetype = typeof(WFMessageQueue<>).MakeGenericType(new[] { this.AssemblyType }); // this.GenericQueue = Activator.CreateInstance(genericqueuetype, new object[] { @"FormatName:Direct=TCP:" + ip + @"\Private$\" + queuename }); // this.MessageQueue = (System.Messaging.MessageQueue)genericqueuetype.GetProperty("MessageQueue").GetGetMethod().Invoke(this.GenericQueue, new object[0]); } else { throw new Exception(""); } } }
public static IWFMessageQueue <U> CreateWFMessageQueue(string ipaddress, int port, string queuename, WFMessageQueueType queuetype) { // if (typeof(T) == typeof(Queue<U>)) // return new WFMessageQueue_Queue<U>(); // else if (typeof(T) == typeof(MessageQueue)) // return new WFMessageQueue_MessageQueue<U>(ipaddress, queuename); // else if (typeof(T) == typeof(QueueingBasicConsumer)) { return(new WFMessageQueue_RabbitMQ <U>(ipaddress, port, queuename, queuetype)); } return(null); }