/// <summary> /// /// </summary> /// <param name="receiverindex"></param> /// <param name="r"></param> public static void StartReceiver(int receiverindex, EventHubReceiver r) { var starttime = DateTime.Now; bool isMessageAvailable = true; var requestdata = new WEP.GSP.Data.RequestData(); EventHubProcessor processor = new EventHubProcessor(); do { try { var messages = r.Receive(10); if (messages == null || messages.Count() == 0) { isMessageAvailable = false; } else { Task.Factory.StartNew(() => processor.EventMessageHandler(messages)); requestdata.UpdatePartitionOffset(Convert.ToString(receiverindex), messages.Last().Offset); requests = requests + messages.Count(); } } catch (Exception exception) { Trace.TraceError("exception on receive {0}", exception.Message); } } while (isMessageAvailable); requestdata.UpdateReleasePartitionLock(Convert.ToString(receiverindex)); }
protected override void OnStop() { eventLogger.WriteEntry("Stop working", EventLogEntryType.Information, 3); MyThread thr3 = new MyThread(); thr3.UnRegisterEH(); var requestdata = new WEP.GSP.Data.RequestData(); requestdata.UpdateReleasePartitionLockOfInstance(System.Environment.MachineName); }
/// <summary> /// /// </summary> public void CreateReceiver() { if (!isRunning) { isRunning = true; var connectionString = Constants.GstnServiceBusEventHub; string eventhubPath = Constants.GstnReqEventHub; var nsm = NamespaceManager.CreateFromConnectionString(connectionString); //var description = nsm.CreateEventHubIfNotExists(eventhubPath); var builder = new ServiceBusConnectionStringBuilder(connectionString) { TransportType = TransportType.Amqp }; var factory = MessagingFactory.CreateFromConnectionString(builder.ToString()); var client = factory.CreateEventHubClient(eventhubPath); var group = client.GetDefaultConsumerGroup(); var requestdata = new WEP.GSP.Data.RequestData(); try { var receiverList = new List <EventHubReceiver>(); var availablePartition = requestdata.GetInstancePartitions(System.Environment.MachineName + ConfigurationManager.AppSettings["MachineName"].ToString()); if (availablePartition != null && availablePartition.Any()) { foreach (var partitionno in availablePartition) { receiverList.Add(group.CreateReceiver(partitionno, requestdata.RetrievePartitionOffset(partitionno))); } ; var taskFactory = new TaskFactory(); var task = ( from r in receiverList select taskFactory.StartNew(() => { Task.Delay(TimeSpan.FromSeconds(1)); StartReceiver(Convert.ToInt32(r.PartitionId), r); })).ToList(); Task.WaitAll(task.ToArray()); } isRunning = false; } catch (Exception e) { isRunning = false; Trace.TraceInformation(e.Message); } } System.Threading.Thread.Sleep(60000); CreateReceiver(); }