Beispiel #1
0
        private static async Task CleanUpEntity(string connectionString, string entityPAth)
        {
            var client = new SessionClient(connectionString, entityPAth, ReceiveMode.PeekLock);

            client.OperationTimeout = TimeSpan.FromSeconds(5);

            IMessageSession session = null;

            try
            {
                session = await client.AcceptMessageSessionAsync();

                var messages = await session.ReceiveAsync(1000, TimeSpan.FromSeconds(1));

                await session.CompleteAsync(messages.Select(m => m.SystemProperties.LockToken));
            }
            catch (ServiceBusException)
            {
            }
            finally
            {
                if (session != null)
                {
                    await session.CloseAsync();
                }
            }
        }
Beispiel #2
0
        static async Task ReceiveSessionMessagesAsync()
        {
            Console.WriteLine("===================================================================");
            Console.WriteLine("Accepting sessions in the reverse order of sends for demo purposes");
            Console.WriteLine("===================================================================");

            var configuration = new AzureStorageAttachmentConfiguration(StorageConnectionString, containerName: "ovp");

            sessionClient.RegisterAzureStorageAttachmentPlugin(configuration);

            // AcceptMessageSessionAsync(i.ToString()) as below with session id as parameter will try to get a session with that sessionId.
            // AcceptMessageSessionAsync() without any messages will try to get any available session with messages associated with that session.
            IMessageSession session = await sessionClient.AcceptMessageSessionAsync();

            if (session != null)
            {
                Message message = await session.ReceiveAsync();

                var          stream = new MemoryStream(message.Body);
                StreamReader reader = new StreamReader(stream);
                string       text   = reader.ReadToEnd();

                Console.WriteLine($"message body : {text}");

                await session.CompleteAsync(message.SystemProperties.LockToken);
            }
        }
        private static void ReceiveMessages(ISessionClient sessionClient)
        {
            IMessageSession messageSession = sessionClient.AcceptMessageSessionAsync("Test1").GetAwaiter().GetResult();
            Program         c = new Program();

            try
            {
                Message messageReceived = null;

                do
                {
                    log.Info("Receiver listening");
                    messageReceived = messageSession.ReceiveAsync().GetAwaiter().GetResult();

                    if (messageReceived != null)
                    {
                        Console.WriteLine($"Received message: SequenceNumber:{messageReceived.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(messageReceived.Body)}");

                        //this command permit to delete messages from the Cloud service, if the connection lost the application retry to download datas
                        messageSession.CompleteAsync(messageReceived.SystemProperties.LockToken).GetAwaiter().GetResult();
                    }

                    //call of method to move files from a directory to anothe
                    c.ConfigurationSourceDestinationPath("sourcePath", "destinationPath", $"{Encoding.UTF8.GetString(messageReceived.Body)}");
                } while (!messageSession.IsClosedOrClosing);

                Console.WriteLine("Press any key to exit");
                Console.ReadKey();
            }
            finally
            {
                messageSession.CloseAsync().GetAwaiter().GetResult();
                Console.ReadKey();
            }
        }
        public async Task <ActionResult <IEnumerable <string> > > Get()
        {
            var log = new List <string>();

            var sessionId = Guid.NewGuid().ToString();

            log.Add($"Creating session {sessionId}");

            IMessageSession session = await _sessionClient.AcceptMessageSessionAsync(sessionId);

            Message response = null;
            var     t        = session.ReceiveAsync(new TimeSpan(0, 5, 0))
                               .ContinueWith(m => response = m.Result)
                               .ContinueWith(m => log.Add($"received response from service for {sessionId}"));

            var message = new Message(Encoding.UTF8.GetBytes("test message"))
            {
                SessionId = sessionId
            };
            await _messageSender.SendAsync(message);

            log.Add($"Sent work request for {sessionId}");

            await t;
            await session.CompleteAsync(response.SystemProperties.LockToken);

            log.Add($"Completed session: {sessionId}");

            return(log);
        }
        private async Task ReceiveResponse(string sessionId)
        {
            try
            {
                SessionClient   sessionClient  = new SessionClient(_connectionString, _queueServerName);
                IMessageSession messageSession = await sessionClient.AcceptMessageSessionAsync(sessionId);

                Message messagePayload = await messageSession.ReceiveAsync();

                if (messagePayload != null)
                {
                    string message = Encoding.UTF8.GetString(messagePayload.Body);
                    ReplyListBox.Items.Add(ConstructMessageListBox(sessionId, message, false));
                }
                else
                {
                    ReplyListBox.Items.Add(ConstructMessageListBox(sessionId, "Failed getting response", false));
                }
                await messageSession.CloseAsync();

                await sessionClient.CloseAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #6
0
        public async Task <List <Message> > ReceiveMessagesAsync()
        {
            var             messages = new List <Message>();
            IMessageSession session  = await _sessionClient.AcceptMessageSessionAsync(_serviceBusContext.SessionId);

            if (session == null)
            {
                return(messages);
            }

            for (int i = 0; i < _serviceBusContext.MaxConcurrentMessagesToBeRetrieved; i++)
            {
                Message message = await session.ReceiveAsync(_serviceBusContext.OperationTimeout);

                if (message == null)
                {
                    break;
                }

                messages.Add(message);
                await session.CompleteAsync(message.SystemProperties.LockToken);
            }

            await session.CloseAsync();

            return(messages);
        }
        async Task OperationsOnMessageSessionAfterCloseShouldThrowObjectDisposedExceptionTest()
        {
            var             sender          = new MessageSender(TestUtility.NamespaceConnectionString, TestConstants.SessionNonPartitionedQueueName);
            var             sessionClient   = new SessionClient(TestUtility.NamespaceConnectionString, TestConstants.SessionNonPartitionedQueueName);
            IMessageSession sessionReceiver = null;

            try
            {
                var messageId = "test-message1";
                var sessionId = Guid.NewGuid().ToString();
                await sender.SendAsync(new Message { MessageId = messageId, SessionId = sessionId });

                TestUtility.Log($"Sent Message: {messageId} to Session: {sessionId}");

                sessionReceiver = await sessionClient.AcceptMessageSessionAsync(sessionId);

                Assert.NotNull(sessionReceiver);
                TestUtility.Log($"Received Session: SessionId: {sessionReceiver.SessionId}: LockedUntilUtc: {sessionReceiver.LockedUntilUtc}");

                await sessionReceiver.CloseAsync();

                await Assert.ThrowsAsync <ObjectDisposedException>(async() => await sessionReceiver.ReceiveAsync());

                await Assert.ThrowsAsync <ObjectDisposedException>(async() => await sessionReceiver.GetStateAsync());

                await Assert.ThrowsAsync <ObjectDisposedException>(async() => await sessionReceiver.SetStateAsync(null));

                sessionReceiver = await sessionClient.AcceptMessageSessionAsync(sessionId);

                Assert.NotNull(sessionReceiver);
                TestUtility.Log($"Reaccept Session: SessionId: {sessionReceiver.SessionId}: LockedUntilUtc: {sessionReceiver.LockedUntilUtc}");

                var message = await sessionReceiver.ReceiveAsync();

                Assert.True(message.MessageId == messageId);
                TestUtility.Log($"Received Message: MessageId: {message.MessageId}");
                await sessionReceiver.CompleteAsync(message.SystemProperties.LockToken);

                await sessionReceiver.CloseAsync();
            }
            finally
            {
                await sender.CloseAsync();

                await sessionClient.CloseAsync();
            }
        }
Beispiel #8
0
        public async Task TransactionalSessionDispositionTest(string queueName)
        {
            var             sender        = new MessageSender(ConnectionString, queueName);
            var             sessionClient = new SessionClient(ConnectionString, queueName);
            IMessageSession receiver      = null;

            try
            {
                string body    = Guid.NewGuid().ToString("N");
                var    message = new Message(body.GetBytes())
                {
                    SessionId = body
                };
                await sender.SendAsync(message).ConfigureAwait(false);

                receiver = await sessionClient.AcceptMessageSessionAsync(body);

                var receivedMessage = await receiver.ReceiveAsync(ReceiveTimeout);

                Assert.NotNull(receivedMessage);
                Assert.Equal(body, receivedMessage.Body.GetString());

                using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken);

                    ts.Dispose();
                }

                // Adding delay since transaction Commit/Rollback is an asynchronous operation.
                // Operating on the same message should not be done.
                await Task.Delay(TimeSpan.FromSeconds(2));

                using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken);

                    ts.Complete();
                }

                // Adding delay since transaction Commit/Rollback is an asynchronous operation.
                // Operating on the same message should not be done.
                await Task.Delay(TimeSpan.FromSeconds(2));

                await Assert.ThrowsAsync <SessionLockLostException>(async() => await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken));
            }
            finally
            {
                await sender.CloseAsync();

                await sessionClient.CloseAsync();

                if (receiver != null)
                {
                    await receiver.CloseAsync();
                }
            }
        }
        private async Task OnMessage(IMessageSession session, Message messageToHandle, CancellationToken lockToken)
        {
            try {
                session.PrefetchCount = 500;
                IList <Message> batchList;
                List <Message>  fullList = new List <Message> {
                    messageToHandle
                };
                batchList = await session.ReceiveAsync(500);

                if (!(batchList is null) && batchList.Count > 0)
                {
                    fullList.AddRange(batchList);
                }

                foreach (Message msg in fullList)
                {
                    if (!MessagesListedBySession.ContainsKey(session.SessionId))
                    {
                        _sessionsInitializedCount++;
                        MessagesListedBySession.TryAdd(session.SessionId, new List <string>());
                    }

                    string dataJSON = Encoding.UTF8.GetString(msg.Body);

                    MessagesListedBySession[session.SessionId].Add(dataJSON);

                    ProcessMessage(session, msg, dataJSON);

                    if (msg.Label.Equals("last", StringComparison.InvariantCultureIgnoreCase))
                    {
                        try {
                            ProcessMessagesWhenLastReceived(session, MessagesListedBySession[session.SessionId], msg);
                        } catch (Exception ex) {
                            if (!(logger is null))
                            {
                                logger.LogError(ex.Message);
                                logger.LogDebug(ex.StackTrace);
                            }
                        } finally {
                            MessagesListedBySession.Remove(session.SessionId);
                        }
                    }
                }
                //await sLock.WaitAsync();
                //await session.CompleteAsync(fullList.Select(m => m.SystemProperties.LockToken)).ContinueWith((t) => sLock.Release());
                await session.CompleteAsync(fullList.Select(m => m.SystemProperties.LockToken));
            } catch (Exception ex) {
                if (!(logger is null))
                {
                    logger.LogError(ex.Message);
                    logger.LogDebug(ex.StackTrace);
                }
                //throw new ApplicationException(ex.Message + ex.StackTrace);
            }
        }
Beispiel #10
0
        async Task <long> DoPurgeSessionEntity(long messagesToPurgeCount)
        {
            long totalMessagesPurged = 0;

            ISessionClient sessionClient = new SessionClient(
                serviceBusHelper.ConnectionString,
                GetEntityPath(deadLetterQueue: false),
                null,
                receiveMode: ReceiveMode.ReceiveAndDelete,
                retryPolicy: RetryPolicy.Default,
                prefetchCount: 10,
                transportType: serviceBusHelper.TransportType);

            var consecutiveSessionTimeOuts = 0;

            try
            {
                const int enoughZeroReceives = 3;
                while (consecutiveSessionTimeOuts < enoughZeroReceives && totalMessagesPurged < messagesToPurgeCount)
                {
                    IMessageSession session = await sessionClient.AcceptMessageSessionAsync();

                    var consecutiveZeroBatchReceives = 0;
                    while (consecutiveZeroBatchReceives < enoughZeroReceives &&
                           totalMessagesPurged < messagesToPurgeCount)
                    {
                        var messages = await session.ReceiveAsync(1000, TimeSpan.FromMilliseconds(1000))
                                       .ConfigureAwait(false);

                        if (messages != null && messages.Any())
                        {
                            Interlocked.Add(ref totalMessagesPurged, messages.Count());
                            consecutiveZeroBatchReceives = 0;
                        }
                        else
                        {
                            ++consecutiveZeroBatchReceives;
                        }
                    }

                    await session.CloseAsync().ConfigureAwait(false);
                }
            }
            catch (TimeoutException)
            {
                ++consecutiveSessionTimeOuts;
            }
            finally
            {
                await sessionClient.CloseAsync().ConfigureAwait(false);
            }

            return(totalMessagesPurged);
        }
Beispiel #11
0
        public List <Message> ReceiveMessages(Dictionary <string, string> topicAndSubscriptionReceived, string tokenID)
        {
            if (topicAndSubscriptionReceived.Count < 1)
            {
                return(null);
            }

            List <Message>  listMessage         = new List <Message>();
            Action <object> delegateGetMessages = (topicSubscriptionMessage) =>
            {
                var             messageFound  = (KeyValuePair <string, string>)topicSubscriptionMessage;
                ISessionClient  sessionClient = new SessionClient(appOptions.BusConnectionString, EntityNameHelper.FormatSubscriptionPath(messageFound.Key, messageFound.Value), ReceiveMode.PeekLock);
                IMessageSession session       = sessionClient.AcceptMessageSessionAsync(tokenID).GetAwaiter().GetResult();
                while (true)
                {
                    Message message = session.ReceiveAsync(new TimeSpan(0, 0, 20)).GetAwaiter().GetResult();
                    if (message == null)
                    {
                        session.CloseAsync().GetAwaiter().GetResult();
                        sessionClient.CloseAsync().GetAwaiter().GetResult();
                        break;
                    }
                    else
                    {
                        session.CompleteAsync(message.SystemProperties.LockToken).GetAwaiter().GetResult();
                        listMessage.Add(message);
                    }
                }
            };

            Task[] taskArray = new Task[topicAndSubscriptionReceived.Count];
            List <KeyValuePair <string, string> > lstTopicAndSubscriptionReceived = new List <KeyValuePair <string, string> >();

            foreach (KeyValuePair <string, string> topicAndSubscription in topicAndSubscriptionReceived)
            {
                lstTopicAndSubscriptionReceived.Add(topicAndSubscription);
            }

            for (int i = 0; i < taskArray.Length; i++)
            {
                taskArray[i] = Task.Factory.StartNew(delegateGetMessages, lstTopicAndSubscriptionReceived[i]);
            }
            Task.WaitAll(taskArray);

            return(listMessage);
        }
Beispiel #12
0
        public async Task <string> ExecuteRequestReplyOverSyncAsync(object content, bool waitForReply = true)
        {
            // Set/lock session
            string          replySessionId = Guid.NewGuid().ToString();
            IMessageSession session        = await _replySessionClient.AcceptMessageSessionAsync(replySessionId); // lock session

            try
            {
                // Arrange payload
                string raw     = JsonConvert.SerializeObject(content);
                var    message = new Message
                {
                    Body             = Encoding.UTF8.GetBytes(raw),
                    ReplyToSessionId = replySessionId // tell recipient to reply using this session ID
                };

                // Send request
                using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
                    await _requestClient.SendAsync(message);

                // Exit early if you don't want to wait for reply
                if (waitForReply == false)
                {
                    return($"Successfully sent request with sessionId: {replySessionId}");
                }

                // Receive reply
                Message reply = await session.ReceiveAsync(TimeSpan.FromSeconds(10)); // 10s timeout

                if (reply == null)
                {
                    return($"Failed to get reply within timeout for session {replySessionId}");
                }

                string response = Encoding.UTF8.GetString(reply.Body);
                await session.CompleteAsync(reply.SystemProperties.LockToken);

                return(response);
            }
            finally
            {
                await session.CloseAsync(); // release exlusive lock
            }
        }
Beispiel #13
0
        public async Task <string> GetResponseServiceBus()
        {
            var sessionClient = new SessionClient(_connectionStrings.ServiceBus, _responseQueueName);

            IMessageSession session = await sessionClient.AcceptMessageSessionAsync(HttpContext.Session.Id);

            if (session != null)
            {
                Message message = await session.ReceiveAsync();

                if (message != null)
                {
                    await session.CompleteAsync(message.SystemProperties.LockToken);

                    return(Encoding.UTF8.GetString(message.Body));
                }
                await session.CloseAsync();
            }
            return(string.Empty);
        }
Beispiel #14
0
        public static async Task Run([ServiceBusTrigger("load-test", Connection = "LoadQueueConnectionString", IsSessionsEnabled = true)] Message message, IMessageSession messageSession, ILogger log)
        {
            log.LogInformation($"MessageReceived: {message.MessageId}");

            //Force assembly to be in memory for this example - can be fixed with DI I think
            var _ = typeof(IntermediateModelRepository);

            var messages = new List <Message> {
                message
            };
            var entityName = (string)message.UserProperties["EntityName"];
            var taskId     = (Guid)message.UserProperties["TaskId"];
            var isLast     = (bool)message.UserProperties["Last"];
            await messageSession.CompleteAsync(message.SystemProperties.LockToken);

            //Get the model type
            var model = AppDomain.CurrentDomain
                        .GetAssemblies()
                        .FirstOrDefault(assembly => assembly.GetName().Name == "Kalibrate.DTM.DataMapper.Shared")
                        ?.GetTypes()
                        .FirstOrDefault(type => type.Name == entityName);

            //Keep receiving new messages in the session until the last message is received.
            while (!isLast)
            {
                message = await messageSession.ReceiveAsync();

                messages.Add(message);
                log.LogInformation($"MessageReceived: {message.MessageId} ");
                isLast = (bool)message.UserProperties["Last"];
                await messageSession.CompleteAsync(message.SystemProperties.LockToken);
            }

            log.LogInformation($"Messages - Task:{taskId}, Messages:{messages.Count}");

            //All messages in the session have been received, now process them
            ProcessMessages(messages, model, taskId, messageSession, log);

            //All messages have been processed and closed, so we can now close the session
            await messageSession.CloseAsync();
        }
Beispiel #15
0
        static async Task ReceiveSessionMessagesAsync(int numberOfSessions, int messagesPerSession)
        {
            Console.WriteLine("===================================================================");
            Console.WriteLine("Accepting sessions in the reverse order of sends for demo purposes");
            Console.WriteLine("===================================================================");

            for (int i = 0; i < numberOfSessions; i++)
            {
                int messagesReceivedPerSession = 0;

                // AcceptMessageSessionAsync(i.ToString()) as below with session id as parameter will try to get a session with that sessionId.
                // AcceptMessageSessionAsync() without any messages will try to get any available session with messages associated with that session.
                IMessageSession session = await sessionClient.AcceptMessageSessionAsync("2c812750-f89e-4e1e-b8bc-a104bd8dde16");

                if (session != null)
                {
                    // Messages within a session will always arrive in order.
                    Console.WriteLine("=====================================");
                    Console.WriteLine($"Received Session: {session.SessionId}");

                    while (messagesReceivedPerSession++ < messagesPerSession)
                    {
                        Message message = await session.ReceiveAsync();

                        //Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
                        Console.WriteLine($"Message {message.UserProperties["MessageNo"].ToString()} of {message.UserProperties["TotalMessages"].ToString()} received");

                        // Complete the message so that it is not received again.
                        // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
                        await session.CompleteAsync(message.SystemProperties.LockToken);
                    }

                    Console.WriteLine($"Received all messages for Session: {session.SessionId}");
                    Console.WriteLine("=====================================");

                    // Close the Session after receiving all messages from the session
                    await session.CloseAsync();
                }
            }
        }
        private async Task StartListeningForClientMessages(CancellationToken cancelToken)
        {
            _activeClientSession = await ClientSessionListener.Value.AcceptMessageSessionAsync(State[ApolloConstants.RegisteredAsKey].ToString(), TimeSpan.FromMinutes(30));

            while (_activeClientSession != null && _clientSessionListenCancellationToken != null && !_clientSessionListenCancellationToken.Token.IsCancellationRequested)
            {
                try
                {
                    var messages = await _activeClientSession.ReceiveAsync(ClientMessagesPerReceive);

                    if (messages == null)
                    {
                        continue;
                    }
                    foreach (var message in messages)
                    {
                        await InvokeMessageHandlers(_activeClientSession, ApolloQueue.ClientSessions, new ServiceBusMessage(message), cancelToken);
                    }
                }
                catch (SessionLockLostException)
                {
                    Logger.Info("Renewing session lock");
                    if (_activeClientSession == null)
                    {
                        throw;
                    }
                    await ReaquireClientSessionLock();
                }
                catch (ServiceBusTimeoutException ex)
                {
                    Logger.Warn($"Timed out while trying to get session lock ({ex.Message}). Will retry");
                    Logger.Debug(ex);
                    // Timed out reconnecting, just try again
                }
                catch (Exception ex)
                {
                    Logger.Error("Encountered an exception while trying to receive client session messages", ex);
                }
            }
        }
Beispiel #17
0
        public async Task <Dictionary <string, string> > CheckMessagesReceived(string subscriptionName, string tokenID)
        {
            List <Message> message = new List <Message>();
            Dictionary <string, string> topicAndSubscriptionFound = new Dictionary <string, string>();
            var topicsRegistered = await new ManageAzurePortal().CheckTopics(subscriptionName);

            if (topicsRegistered.Count > 0)
            {
                Action <object> delegateCheck = (topic) =>
                {
                    ISessionClient  sessionClient = new SessionClient(appOptions.BusConnectionString, EntityNameHelper.FormatSubscriptionPath(topic.ToString(), subscriptionName), ReceiveMode.PeekLock);
                    IMessageSession session       = sessionClient.AcceptMessageSessionAsync(tokenID).GetAwaiter().GetResult();
                    if (session != null)
                    {
                        message = (List <Message>)session.ReceiveAsync(1, new TimeSpan(0, 0, 20)).GetAwaiter().GetResult();
                        if (message == null)
                        {
                            session.CloseAsync().GetAwaiter().GetResult();
                            sessionClient.CloseAsync().GetAwaiter().GetResult();
                        }
                        else
                        {
                            topicAndSubscriptionFound.Add(topic.ToString(), subscriptionName);
                            session.CloseAsync().GetAwaiter().GetResult();
                            sessionClient.CloseAsync().GetAwaiter().GetResult();
                        }
                    }
                };

                Task[] taskArray = new Task[topicsRegistered.Count];
                for (int i = 0; i < taskArray.Length; i++)
                {
                    taskArray[i] = Task.Factory.StartNew(delegateCheck, topicsRegistered[i]);
                }
                Task.WaitAll(taskArray);
            }
            return(topicAndSubscriptionFound);
        }
        static async Task ReceiveMessage(string sessionId)
        {
            try
            {
                SessionClient   sessionClient  = new SessionClient(_connectionString, _queueServerName);
                IMessageSession messageSession = await sessionClient.AcceptMessageSessionAsync(sessionId);

                Message message = await messageSession.ReceiveAsync();

                if (message != null)
                {
                    string messageText = Encoding.UTF8.GetString(message.Body);
                    Console.WriteLine($"[{DateTime.Now}] Reply from server: {messageText}");
                }
                await messageSession.CloseAsync();

                await sessionClient.CloseAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[x] Error: {ex.Message}");
            }
        }
        private static async Task ClenaUpEntity(string connectionString, string entityPAth)
        {
            var client = new SessionClient(connectionString, entityPAth, ReceiveMode.PeekLock);

            client.OperationTimeout = TimeSpan.FromSeconds(5);

            IMessageSession session = null;

            try
            {
                session = await client.AcceptMessageSessionAsync();

                while (true)
                {
                    var message = await session.ReceiveAsync(TimeSpan.FromSeconds(5));

                    if (message != null)
                    {
                        await session.CompleteAsync(message.SystemProperties.LockToken);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (ServiceBusException)
            {
            }
            finally
            {
                if (session != null)
                {
                    await session.CloseAsync();
                }
            }
        }
Beispiel #20
0
        public static async Task ReceiveMessagesAsync(IMessageSession session,
                                                      Queue <QueueMessage> queue,
                                                      int count)
        {
            var messages = await session.ReceiveAsync(count) ?? new List <Message>();

            var queueMessages = messages.Where(m => m != null)
                                .Select(m => new QueueMessage(m));

            foreach (var message in queueMessages)
            {
                var exampleClass =
                    JsonConvert.DeserializeObject <ExampleClass>(Encoding.UTF8.GetString(message.Body));
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine(
                    $"{DateTime.UtcNow:o} Message received:" +
                    $"\n\tSessionId = {message.SessionId}, " +
                    $"\n\tMessageId = {message.MessageId}, " +
                    $"\n\tSequenceNumber = {message.Message.SystemProperties.SequenceNumber}," +
                    $"\n\tContent: [ item = {exampleClass.ObjName} ]"
                    );
                queue.Enqueue(message);
            }
        }
        private async Task ProcessMessagesFromQueueSession()
        {
            try
            {
                _processID  = System.Diagnostics.Process.GetCurrentProcess().Id;
                _threadID   = Thread.CurrentThread.ManagedThreadId;
                _instanceID = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
                //_tclient.TrackTrace($"InstanceID:{ _instanceid}");

                #region receive the sessions from wo-xml and process the messages
                _sessionClient.OperationTimeout = new TimeSpan(0, 0, 5);
                //Console.WriteLine($"Start Receiving Session");
                _tclient.TrackTrace($"InstanceID:{ _instanceID}, Start Receiving Session");
                session = await _sessionClient.AcceptMessageSessionAsync();

                if (session != null)
                {
                    // Messages within a session will always arrive in order.
                    _tclient.TrackTrace($"InstanceID:{ _instanceID}, Received Session: [{session.SessionId}]");

                    while (true)
                    {
                        //Read All the messages from sessions
                        Message message = await session.ReceiveAsync();

                        if (message != null)
                        {
                            MemoryStream woBody = new MemoryStream(message.Body);
                            XmlDocument  xmlDoc = new XmlDocument();
                            xmlDoc.Load(woBody);
                            var wordOrderID             = WOCommonUtility.GetNodeValue(xmlDoc, "Source_WO_ID");
                            var workOrderPriority       = WOCommonUtility.GetNodeValue(xmlDoc, "WOPriority");
                            var workOrderSequence       = WOCommonUtility.GetNodeValue(xmlDoc, "QueueSequence");
                            var workOrderPostedDateTime = WOCommonUtility.GetNodeValue(xmlDoc, "PostedDateTime");


                            string query = $"Insert into tblQueueLog (ProcessedBy,WorkOrder,Sequence,Priority,PostedTime,ProcessedTime,ProcessID,ThreadID, InstanceId) " +
                                           $"values ('PocWebJob9090', '{wordOrderID}', '{workOrderSequence}', '{workOrderPriority}','{workOrderPostedDateTime}' , '{DateTime.UtcNow}','{_processID}','{_threadID }','{_instanceID}')";
                            await LogMesssagesInDb(query, _commonSettings.Value.SQLDBConnectionString);

                            // Complete the message so that it is not received again.
                            // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
                            await session.CompleteAsync(message.SystemProperties.LockToken);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    //Console.WriteLine($"No any session available in queue to process from WebJob.");
                }
            }
            catch (ServiceBusTimeoutException timeOutEx)
            {
                string query = $"Insert into tblQueueLog (ProcessedBy,ProcessedTime,ProcessID,ThreadID, InstanceId, ExceptionDetails ) " +
                               $"values ('PocWebJob9090', '{DateTime.UtcNow}','{_processID}','{_threadID}','{_instanceID}','Exception Type: ServiceBusTimeoutException, Queue session timed out')";
                await LogMesssagesInDb(query, _commonSettings.Value.SQLDBConnectionString);

                _tclient.TrackException(timeOutEx, new Dictionary <string, string>()
                {
                    { "ApplicationName", "PocWebJob9090" }, { "ModuleName", "Functions" }, { "Method", "ProcessMessagesFromQueue" },
                    { "ServiceBusTimeoutException", "Service bus session timeout, No sessions in FIFO order." },
                    { "InstanceID", _instanceID }, { "Message", timeOutEx.Message }
                });
            }
            catch (SessionCannotBeLockedException lockedEx)
            {
                string query = $"Insert into tblQueueLog (ProcessedBy,ProcessedTime,ProcessID,ThreadID, InstanceId,ExceptionDetails ) " +
                               $"values ('PocWebJob9090', '{DateTime.UtcNow}','{_processID}','{_threadID }','{_instanceID}','Exception Type: SessionCannotBeLockedException, The requested session cannot be accepted. It may be locked by another receiver.')";
                await LogMesssagesInDb(query, _commonSettings.Value.SQLDBConnectionString);

                _tclient.TrackTrace($"The requested session cannot be accepted. It may be locked by another receiver.");

                _tclient.TrackException(lockedEx, new Dictionary <string, string>()
                {
                    { "ApplicationName", "PocWebJob9090" }, { "ModuleName", "Functions" }, { "Method", "ProcessMessagesFromQueue" },
                    { "SessionCannotBeLockedException", "The requested session cannot be accepted. It may be locked by another receiver." }, { "Message", lockedEx.Message },
                    { "InstanceID", _instanceID }
                });
            }
            catch (Exception ex)
            {
                string query = $"Insert into tblQueueLog (ProcessedBy,ProcessedTime,ProcessID,ThreadID,InstanceId,ExceptionDetails ) " +
                               $"values ('PocWebJob9090', '{DateTime.UtcNow}','{_processID}','{_threadID }','{_instanceID}','Exception Type: Exception, exception Occurred while going to fetch message from queue.')";
                await LogMesssagesInDb(query, _commonSettings.Value.SQLDBConnectionString);

                _tclient.TrackException(ex, new Dictionary <string, string>()
                {
                    { "ApplicationName", "PocWebJob9090" }, { "ModuleName", "Functions" }, { "Method", "ProcessMessagesFromQueue" },
                    { "InstanceID", _instanceID }, { "Message", ex.Message }
                });
            }
            #endregion
        }
Beispiel #22
0
        async Task MessagePumpTaskAsync(IMessageSession session)
        {
            if (session == null)
            {
                return;
            }

            CancellationTokenSource renewLockCancellationTokenSource = new CancellationTokenSource();

            if (this.ShouldRenewSessionLock())
            {
                TaskExtensionHelper.Schedule(() => this.RenewSessionLockTaskAsync(session, renewLockCancellationTokenSource.Token));
            }

            Timer userCallbackTimer = new Timer(
                OnUserCallBackTimeout,
                renewLockCancellationTokenSource,
                Timeout.Infinite,
                Timeout.Infinite);

            try
            {
                while (!this.pumpCancellationToken.IsCancellationRequested && !session.IsClosedOrClosing)
                {
                    Message message;
                    try
                    {
                        message = await session.ReceiveAsync(this.sessionHandlerOptions.MessageWaitTimeout).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        MessagingEventSource.Log.MessageReceivePumpTaskException(this.clientId, session.SessionId, exception);
                        if (exception is ServiceBusTimeoutException)
                        {
                            // Timeout Exceptions are pretty common. Not alerting the User on this.
                            continue;
                        }

                        await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Receive).ConfigureAwait(false);

                        break;
                    }

                    if (message == null)
                    {
                        MessagingEventSource.Log.SessionReceivePumpSessionEmpty(this.clientId, session.SessionId);
                        break;
                    }

                    // Set the timer
                    userCallbackTimer.Change(this.sessionHandlerOptions.MaxAutoRenewDuration, TimeSpan.FromMilliseconds(-1));
                    bool callbackExceptionOccured = false;
                    try
                    {
                        await this.userOnSessionCallback(session, message, this.pumpCancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        MessagingEventSource.Log.MessageReceivePumpTaskException(this.clientId, session.SessionId, exception);
                        await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.UserCallback).ConfigureAwait(false);

                        callbackExceptionOccured = true;
                        if (!(exception is MessageLockLostException || exception is SessionLockLostException))
                        {
                            await this.AbandonMessageIfNeededAsync(session, message).ConfigureAwait(false);
                        }
                    }
                    finally
                    {
                        userCallbackTimer.Change(Timeout.Infinite, Timeout.Infinite);
                    }

                    if (!callbackExceptionOccured)
                    {
                        await this.CompleteMessageIfNeededAsync(session, message).ConfigureAwait(false);
                    }
                    else if (session.IsClosedOrClosing)
                    {
                        // If User closed the session as part of the callback, break out of the loop
                        break;
                    }
                }
            }
            finally
            {
                userCallbackTimer.Dispose();
                await this.CloseSessionIfNeededAsync(session).ConfigureAwait(false);

                CancelAndDisposeCancellationTokenSource(renewLockCancellationTokenSource);
                this.maxConcurrentSessionsSemaphoreSlim.Release();
            }
        }
Beispiel #23
0
        async Task MessagePumpTaskAsync(IMessageSession session)
        {
            if (session == null)
            {
                return;
            }

            var renewLockCancellationTokenSource = new CancellationTokenSource();

            if (this.ShouldRenewSessionLock())
            {
                TaskExtensionHelper.Schedule(() => this.RenewSessionLockTaskAsync(session, renewLockCancellationTokenSource.Token));
            }

            var autoRenewLockCancellationTimer = new Timer(
                CancelAutoRenewLock,
                renewLockCancellationTokenSource,
                Timeout.Infinite,
                Timeout.Infinite);

            try
            {
                while (!this.pumpCancellationToken.IsCancellationRequested && !session.IsClosedOrClosing)
                {
                    Message message;
                    try
                    {
                        message = await session.ReceiveAsync(this.sessionHandlerOptions.MessageWaitTimeout).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        MessagingEventSource.Log.MessageReceivePumpTaskException(this.clientId, session.SessionId, exception);
                        if (exception is ServiceBusTimeoutException)
                        {
                            // Timeout Exceptions are pretty common. Not alerting the User on this.
                            continue;
                        }

                        if (!(exception is ObjectDisposedException && this.pumpCancellationToken.IsCancellationRequested))
                        {
                            await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Receive).ConfigureAwait(false);
                        }
                        break;
                    }

                    if (message == null)
                    {
                        MessagingEventSource.Log.SessionReceivePumpSessionEmpty(this.clientId, session.SessionId);
                        break;
                    }

                    bool     isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled();
                    Activity activity    = isDiagnosticSourceEnabled ? this.diagnosticSource.ProcessSessionStart(session, message) : null;
                    Task     processTask = null;

                    try
                    {
                        // Set the timer
                        autoRenewLockCancellationTimer.Change(this.sessionHandlerOptions.MaxAutoRenewDuration,
                                                              TimeSpan.FromMilliseconds(-1));
                        var callbackExceptionOccurred = false;
                        try
                        {
                            processTask = this.userOnSessionCallback(session, message, this.pumpCancellationToken);
                            await processTask.ConfigureAwait(false);
                        }
                        catch (Exception exception)
                        {
                            if (isDiagnosticSourceEnabled)
                            {
                                this.diagnosticSource.ReportException(exception);
                            }

                            MessagingEventSource.Log.MessageReceivePumpTaskException(this.clientId, session.SessionId, exception);
                            await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.UserCallback).ConfigureAwait(false);

                            callbackExceptionOccurred = true;
                            if (!(exception is MessageLockLostException || exception is SessionLockLostException))
                            {
                                await this.AbandonMessageIfNeededAsync(session, message).ConfigureAwait(false);
                            }
                        }
                        finally
                        {
                            autoRenewLockCancellationTimer.Change(Timeout.Infinite, Timeout.Infinite);
                        }

                        if (!callbackExceptionOccurred)
                        {
                            await this.CompleteMessageIfNeededAsync(session, message).ConfigureAwait(false);
                        }
                        else if (session.IsClosedOrClosing)
                        {
                            // If User closed the session as part of the callback, break out of the loop
                            break;
                        }
                    }
                    finally
                    {
                        this.diagnosticSource.ProcessSessionStop(activity, session, message, processTask?.Status);
                    }
                }
            }
            finally
            {
                renewLockCancellationTokenSource.Cancel();
                renewLockCancellationTokenSource.Dispose();
                autoRenewLockCancellationTimer.Dispose();

                await this.CloseSessionIfNeededAsync(session).ConfigureAwait(false);

                this.maxConcurrentSessionsSemaphoreSlim.Release();
            }
        }
Beispiel #24
0
        private async Task RunDispatcherForSession(IMessageSession session, CancellationToken cancelToken)
        {
            while (cancelToken.IsCancellationRequested == false)
            {
                var msg = await session.ReceiveAsync(TimeSpan.FromSeconds(1));

                if (msg != null)
                {
                    bool isPersistedAfterCalculus = false;
                    try
                    {
                        ActorBase actor = null;

                        Type tp = Type.GetType((string)msg.UserProperties[ActorReference.cActorType]);
                        if (tp == null)
                        {
                            throw new ArgumentException($"Cannot find type '{session.SessionId}'");
                        }

                        var id = new ActorId((string)msg.UserProperties[ActorReference.cActorId]);
                        if (!actorMap.ContainsKey(session.SessionId))
                        {
                            if (this.persistenceProvider != null)
                            {
                                actor = await this.persistenceProvider.LoadActor(id);

                                if (actor != null)
                                {
                                    logger?.LogInformation($"{this.Name} - Loaded from pesisted store: {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                                }
                            }

                            if (actor == null)
                            {
                                actor = Activator.CreateInstance(tp, id) as ActorBase;
                                logger?.LogInformation($"{this.Name} - New instance created: {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                            }

                            actor.PersistenceProvider = this.PersistenceProvider;

                            actor.Logger = logger;

                            actorMap[session.SessionId] = actor;

                            actor.Activated();
                        }

                        actor = actorMap[session.SessionId];

                        logger?.LogInformation($"{this.Name} - Received message: {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");

                        var invokingMsg = ActorReference.DeserializeMsg <object>(msg.Body);

                        var replyMsg = await InvokeOperationOnActorAsync(actor, invokingMsg, (bool)msg.UserProperties[ActorReference.cExpectResponse],
                                                                         msg.MessageId, msg.ReplyTo);

                        logger?.LogInformation($"{this.Name} - Invoked : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");

                        await persistAndCleanupIfRequired(session);

                        logger?.LogInformation($"{this.Name} - Persisted : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");

                        isPersistedAfterCalculus = true;

                        // If actor operation was invoked with Ask<>(), then reply is expected.
                        if (replyMsg != null)
                        {
                            await this.sendReplyQueueClients[msg.ReplyTo].SendAsync(replyMsg);

                            logger?.LogTrace($"{this.Name} - Replied : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                        }

                        await session.CompleteAsync(msg.SystemProperties.LockToken);

                        logger?.LogInformation($"{this.Name} - Completed : {tp.Name}/{id}, actorMap: {actorMap.Keys.Count}");
                    }
                    catch (Exception ex)
                    {
                        logger?.LogWarning(ex, "Messsage processing error");

                        if (isPersistedAfterCalculus == false)
                        {
                            await persistAndCleanupIfRequired(session);
                        }

                        if (!(ex is SessionLockLostException))
                        {
                            await session.CompleteAsync(msg.SystemProperties.LockToken);
                        }

                        await SendExceptionResponseIfRequired(msg, ex);
                    }
                }
                else
                {
                    logger?.LogTrace($"{this.Name} - No more messages received for sesson {session.SessionId}");
                    await persistAndCleanupIfRequired(session);

                    //return;
                }

                if (IsMemoryCritical())
                {
                    logger?.LogWarning($"Memory reached critical value: {this.CriticalMemInGb}, {Environment.WorkingSet / 1024 / 1024 / 1024}");
                }

                break;
            }
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ExecutionContext context,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var    connectionString = "Endpoint=sb://ccaidauesdevir2sb001.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=OLQnsp0G3L2TtD9WfNUTWZ0TdJ9lnpuFggJV2R6Vc/A=";
            string queueName        = req.Query["queueName"];
            var    policy           = new RetryExponential(
                minimumBackoff: TimeSpan.FromSeconds(10),
                maximumBackoff: TimeSpan.FromSeconds(30),
                maximumRetryCount: 3);

            ISessionClient client = new SessionClient(connectionString, queueName, ReceiveMode.PeekLock, policy);

            log.LogInformation("Waiting for new message");
            string sessionIdParam = req.Query["sessionId"];

            #region variables
            IMessageSession messageSession = await client.AcceptMessageSessionAsync(sessionIdParam);

            var       keepPolling           = true;
            var       isFirstMessage        = true;
            var       expectedNoMessages    = 0;
            Message[] messages              = null;
            var       messagesReceived      = 0;
            var       sessionId             = string.Empty;
            var       correlationId         = string.Empty;
            Stream    fullMessageBodyStream = null;
            #endregion

            while (keepPolling)
            {
                var message = await messageSession.ReceiveAsync();

                if (message == null)
                {
                    continue;
                }

                if (isFirstMessage)
                {
                    log.LogInformation("Receiving first message");
                    expectedNoMessages = (int)message.UserProperties["TotalMessages"];
                    messages           = new Message[expectedNoMessages];
                    isFirstMessage     = false;
                    sessionId          = message.SessionId;
                    correlationId      = message.CorrelationId;
                }

                var messageNo    = (int)message.UserProperties["MessageNo"];
                var messageIndex = messageNo - 1;
                log.LogInformation(string.Format("Receiving message {0}", messageNo));
                messages[messageIndex] = message;
                messagesReceived++;

                if (messagesReceived == expectedNoMessages)
                {
                    keepPolling = false;
                }
            }

            //Rebuild Object
            fullMessageBodyStream = ReconstructMessageBody(messages);
            var result = ReadStreamData(fullMessageBodyStream);

            log.LogInformation($"SessionId = {sessionId}.");
            log.LogInformation($"result = {result}.");

            var completeTasks = new List <Task>();
            var lockToken     = new List <string>();

            foreach (var message in messages)
            {
                lockToken.Add(message.SystemProperties.LockToken);
                //await messageSession.CompleteAsync(message.SystemProperties.LockToken);
            }

            completeTasks.Add(messageSession.CompleteAsync(lockToken));

            Task.WaitAll(completeTasks.ToArray());

            await messageSession.CloseAsync();

            return(new OkObjectResult("Ok"));
        }