示例#1
0
        public Task<PollerResult<Event>> NextAsync(QueueName name)
        {
            var connection = _connectionProvider.GetConnection();
            var result = new PollerResult<Event>(false , Event.Empty);
            using (var channel = connection.CreateModel())
            {
                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume(name.SubscriptionName, true, consumer);
                BasicDeliverEventArgs eventArgs = null;
                if (consumer.Queue.Dequeue((int)_longPollingTimeout.TotalMilliseconds, 
                    out eventArgs))
                {
                    var @event = new Event()
                    {
                        Body = Encoding.UTF8.GetString(eventArgs.Body),
                        QueueName = name.SubscriptionName,
                        UnderlyingMessage = eventArgs,
                        ContentType = "text/plain", // should it be JSON?
                        EventType = name.SubscriptionName
                    };

                    result = new PollerResult<Event>(true, @event);
                }
                return Task.FromResult(result);
            }

        }
示例#2
0
        public RabbitMQQueue(QueueName queueName, IQueueListener listener, IConnection connection,
            Encoding encoding = null, QueueOptions options = default(QueueOptions))
        {
            if (queueName == null) throw new ArgumentNullException("queueName");
            if (listener == null) throw new ArgumentNullException("listener");
            if (connection == null) throw new ArgumentNullException("connection");

            _queueName = queueName;
            _queueExchange = _queueName.GetExchangeName();
            _retryQueueName = queueName.GetRetryQueueName();
            _retryExchange = _queueName.GetRetryExchangeName();
            _deadLetterExchange = _queueName.GetDeadLetterExchangeName();

            _listener = listener;
            _connection = connection;
            _encoding = encoding ?? Encoding.UTF8;
            _ttl = options.TTL;
            _maxAttempts = Math.Max(options.MaxAttempts, 1);
            _retryDelay = options.RetryDelay < TimeSpan.Zero ? TimeSpan.Zero : options.RetryDelay;
            _cancellationTokenSource = new CancellationTokenSource();

            var autoAcknowledge = options.AutoAcknowledge;
            var concurrencyLimit = Math.Max(options.ConcurrencyLimit, 1);
            _consumers = new DurableConsumer[concurrencyLimit];
            for (var i = 0; i < _consumers.Length; i++)
            {
                var consumerTag = _queueName + "_" + i;
                _consumers[i] = new DurableConsumer(_connection, queueName, HandleDelivery, consumerTag,
                    autoAcknowledge);
            }
        }
        /// <summary>
        /// Stages indexes. Should run only using one process.
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="documentType"></param>
        /// <param name="rebuild"></param>
        /// <exception cref="System.ArgumentNullException">scope</exception>
        public void Prepare(string scope, string documentType = "", bool rebuild = false)
        {
            if (String.IsNullOrEmpty(scope))
                throw new ArgumentNullException("scope");

            foreach (var builder in _indexBuilders)
            {
                // skip not requested indexers or index using all if index is not specified
                if (!String.IsNullOrEmpty(documentType) && !documentType.Equals(builder.DocumentType))
                    continue;

                // Execute builder, which will create partitions and put them in the queue
                var queueName = new QueueName("index-{0}-{1}-in", scope, builder.DocumentType);

                var config = GetBuildConfig(_repository, queueName.Scope, queueName.DocumentType);

                var lastBuild = DateTime.UtcNow;
                var newBuildDate = lastBuild;
                if (config.Status == BuildStatus.NeverStarted.GetHashCode() || rebuild) // build was never started, so set min date
                {
                    rebuild = true;
                    lastBuild = DateTime.MinValue;
                    config.LastBuildDate = DateTime.UtcNow.AddYears(-30); // have to set the date to something repository won't complain
                }
                else
                {
                    lastBuild = config.LastBuildDate.AddSeconds(-30); // make sure we get all the changes 
                }

                // Delete all the records
                if (rebuild)
                {
                    _searchProvider.RemoveAll(queueName.Scope, queueName.DocumentType);
                }

                var partitions = builder.CreatePartitions(queueName.Scope, lastBuild);

                var newPartitionsExist = false; // tells if there are any partitions that has been processed
                foreach (var partition in partitions)
                {
                    newPartitionsExist = true;
                    //_observer.Notify(new ConsumeBegin(msg, consumer, envelope.QueueName));
                   _messageSender.Send(queueName.ToString(), partition);
                }

                var newBuildStatus = BuildStatus.Started;
                if (newPartitionsExist)
                {
                    _messageSender.Send(queueName.ToString(), new SearchIndexStatusMessage(queueName.Scope, queueName.DocumentType, BuildStatus.Completed));
                }
                else
                {
                    newBuildStatus = BuildStatus.Completed;
                }

                config.LastBuildDate = newBuildDate;
                config.Status = newBuildStatus.GetHashCode();
                _repository.UnitOfWork.Commit();
            }
        }
        public async Task PushBatchAsync(IEnumerable<Event> messages)
        {

            const int BatchSize = 50;
            
            var msgs = messages.ToArray();
            if (!msgs.Any())
                return;

            var message = msgs.First();
            var queueName = new QueueName(message.QueueName);
            int i = 0;

            if (queueName.IsSimpleQueue)
            {
                var client = _clientProvider.GetQueueClient(queueName);
                while (i < msgs.Length)
                {
                    await client.SendBatchAsync(msgs.Skip(i).Take(BatchSize).Select(x => x.ToMessage()));
                    i += BatchSize;
                }
            }
            else
            {
                var client = _clientProvider.GetTopicClient(queueName);
                while (i < msgs.Length)
                {
                    await client.SendBatchAsync(msgs.Skip(i).Take(BatchSize).Select(x => x.ToMessage()));
                    i += BatchSize;
                }
            }
        }
示例#5
0
        public async Task Given_ClaimsPrincipal_When_Reading_Principal_Should_Be_Read()
        {
            var tempDir = GetTempDirectory();
            var queueName = new QueueName(Guid.NewGuid().ToString());
            var queuePath = Path.Combine(tempDir.FullName, queueName);
            var queueDir = new DirectoryInfo(queuePath);
            if (!queueDir.Exists)
            {
                queueDir.Create();
            }

            var message = new Message(new MessageHeaders
            {
                {HeaderName.ContentType, "text/plain"},
                {HeaderName.MessageId, Guid.NewGuid().ToString()}
            }, "Hello, world!");

            var senderPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[]
            {
                new Claim("username", "testuser"),
                new Claim("role", "testrole")
            }));

            var file = (await MessageFile.Create(queueDir, message, senderPrincipal)).File;
            var messageFile = new MessageFile(file);
            var readSenderPrincipal = await messageFile.ReadSenderPrincipal();
            var readMessage = await messageFile.ReadMessage();

            Assert.That(readSenderPrincipal, Is.EqualTo(senderPrincipal).Using(new ClaimsPrincipalEqualityComparer()));
            Assert.That(readMessage, Is.EqualTo(message).Using(new MessageEqualityComparer()));
        }
示例#6
0
 public RouteRegistration(string messageFilter, QueueName destination)
 {
     Condition.Requires(messageFilter, "messageFilter").IsNotNullOrEmpty();
       Condition.Requires(destination, "destination").IsNotNull();
       MessageFilter = messageFilter;
       Destination = destination;
 }
示例#7
0
 public SubscribeCommand(Type subscribedMessageType, QueueName subscriberQueue)
 {
     Condition.Requires(subscribedMessageType, "subscribedMessageType").IsNotNull();
       Condition.Requires(subscriberQueue, "subscriberQueue").IsNotNull();
       SubscribedMessagesTypeName = subscribedMessageType.AssemblyQualifiedName;
       SubscriberQueueName = subscriberQueue.Name;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Object"/> class.
 /// </summary>
 public MsmqQueueObserver(string serviceBusName, QueueName inputQueue, QueueName errorQueue, MsmqQueueTransport queueTransport)
 {
     _serviceBusName = serviceBusName;
     _inputQueue = inputQueue;
     _errorQueue = errorQueue;
     _queueTransport = queueTransport;
 }
示例#9
0
        public SQLMessageQueue(IDbConnectionProvider connectionProvider, ISQLDialect dialect, QueueName queueName,
            IQueueListener listener, QueueOptions options = default(QueueOptions))
        {
            if (connectionProvider == null) throw new ArgumentNullException("connectionProvider");
            if (dialect == null) throw new ArgumentNullException("dialect");
            if (queueName == null) throw new ArgumentNullException("queueName");
            if (listener == null) throw new ArgumentNullException("listener");

            _connectionProvider = connectionProvider;
            _dialect = dialect;
            _queueName = queueName;

            _listener = listener;
            _autoAcknowledge = options.AutoAcknowledge;
            _maxAttempts = options.MaxAttempts <= 0 ? 10 : options.MaxAttempts;
            _retryDelay = options.RetryDelay < TimeSpan.Zero ? TimeSpan.Zero : options.RetryDelay;

            var concurrencyLimit = options.ConcurrencyLimit <= 0
                ? QueueOptions.DefaultConcurrencyLimit
                : options.ConcurrencyLimit;
            _concurrentMessageProcessingSlot = new SemaphoreSlim(concurrencyLimit);

            _cancellationTokenSource = new CancellationTokenSource();
            _queuedMessages = new BufferBlock<SQLQueuedMessage>(new DataflowBlockOptions
            {
                CancellationToken = _cancellationTokenSource.Token
            });
        }
示例#10
0
        private static IDbConnectionProvider InitDb(DirectoryInfo directory, QueueName queueName)
        {
            var dbPath = Path.Combine(directory.FullName, queueName + ".db");
            var connectionStringSettings = new ConnectionStringSettings
            {
                Name = dbPath,
                ConnectionString = "Data Source=" + dbPath + "; Version=3",
                ProviderName = "System.Data.SQLite"
            };

            var connectionProvider = new SingletonConnectionProvider(connectionStringSettings);
            var connection = connectionProvider.GetConnection();
            try
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = new SQLiteDialect().CreateMessageQueueingServiceObjectsCommand;
                    command.ExecuteNonQuery();
                }
            }
            finally
            {
                connectionProvider.ReleaseConnection(connection);
            }
            return connectionProvider;
        }
 public async Task DeleteQueueAsyncAwait()
 {
     #region DeleteQueueAsync (await)
     IQueueingService queueingService = new CloudQueuesProvider(identity, region, clientId, internalUrl, identityProvider);
     QueueName queueName = new QueueName("ExampleQueue");
     await queueingService.DeleteQueueAsync(queueName, CancellationToken.None);
     #endregion
 }
 public void CreateQueue()
 {
     #region CreateQueueAsync (TPL)
     IQueueingService queueingService = new CloudQueuesProvider(identity, region, clientId, internalUrl, identityProvider);
     QueueName queueName = new QueueName("ExampleQueue");
     Task<bool> task = queueingService.CreateQueueAsync(queueName, CancellationToken.None);
     #endregion
 }
示例#13
0
 public static async Task PublishMessage(Message message, IPrincipal principal, IConnection connection,
     QueueName queueName, string exchange = "", Encoding encoding = null, int attempts = 0)
 {
     using (var channel = connection.CreateModel())
     {
         await PublishMessage(message, principal, channel, queueName, exchange, encoding, attempts);
     }
 }
 public Task CreateQueue(QueueName queueName, IQueueListener listener, QueueOptions options = default(QueueOptions), CancellationToken cancellationToken = default(CancellationToken))
 {
     if (!_queues.TryAdd(queueName, new InMemoryQueue(listener, options)))
     {
         throw new QueueAlreadyExistsException(queueName);
     }
     return Task.FromResult(true);
 }
示例#15
0
 /// <summary>
 /// Initializes a new <see cref="HandlingRule"/> with the supplied message
 /// <paramref name="specification"/>, <see cref="MessageHandler"/>, and
 /// <paramref name="queueName"/>.
 /// </summary>
 /// <param name="specification">The message specification that selects messages 
 /// to which the handling rule applies</param>
 /// <param name="messageHandler">The handler to which messages matching the
 /// specification will be routed</param>
 /// <param name="queueName">(Optional) The name of the queue in which matching
 /// messages will be placed while they await handling</param>
 /// <remarks>
 /// If the <paramref name="queueName"/> is ommitted, a default queue name will
 /// be generated based on the MD5 hash of the full type name of the supplied
 /// <paramref name="messageHandler"/>
 /// </remarks>
 /// <seealso cref="GenerateQueueName"/>
 public HandlingRule(IMessageSpecification specification, IMessageHandler messageHandler,
     QueueName queueName = null)
 {
     if (specification == null) throw new ArgumentNullException("specification");
     if (messageHandler == null) throw new ArgumentNullException("messageHandler");
     _specification = specification;
     _messageHandler = messageHandler;
     _queueName = queueName ?? GenerateQueueName(messageHandler);
 }
        public async Task EnqueueMessage(QueueName queueName, Message message, IPrincipal senderPrincipal, CancellationToken cancellationToken = default(CancellationToken))
        {
            FilesystemMessageQueue queue;
            if (!_queues.TryGetValue(queueName, out queue)) throw new QueueNotFoundException(queueName);

            Log.DebugFormat("Enqueueing message ID {0} in filesystem queue \"{1}\"...", message.Headers.MessageId, queueName);
            await queue.Enqueue(message, senderPrincipal, cancellationToken);
            Log.DebugFormat("Message ID {0} enqueued successfully in filesystem queue \"{1}\"", message.Headers.MessageId, queueName);
        }
 public Task EnqueueMessage(QueueName queueName, Message message, IPrincipal senderPrincipal, CancellationToken cancellationToken = default(CancellationToken))
 {
     InMemoryQueue queue;
     if (!_queues.TryGetValue(queueName, out queue))
     {
         throw new QueueNotFoundException(queueName);
     }
     return queue.Enqueue(message, senderPrincipal);
 }
示例#18
0
        public void AddRoute(string messageFilter, QueueName destination)
        {
            Condition.Requires(messageFilter, "messageFilter").IsNotNull();
              Condition.Requires(destination, "destination").IsNotNull();

              if (Routes.Any(r => r.MessageFilter == messageFilter && r.Destination == destination))
            throw new InvalidOperationException(string.Format("The subscription for {0} at {1} is already added.", messageFilter, destination));
              Routes.Add(new RouteRegistration(messageFilter, destination));
        }
 public string Name(QueueName name)
 {
     var val = _ht[name];
     if (val == null)
     {
         val = name.ToString("G");
         _ht[name] = val;
     }
     return val.ToString();
 }
        public async Task Given_Queued_Message_When_Acknowledged_Then_Message_Should_Be_Deleted()
        {
            var listenerCalledEvent = new ManualResetEvent(false);
            var tempDir = GetTempDirectory();
            var queueName = new QueueName(Guid.NewGuid().ToString());
            var queuePath = Path.Combine(tempDir.FullName, queueName);
            var queueDir = new DirectoryInfo(queuePath);
            if (!queueDir.Exists)
            {
                queueDir.Create();
            }

            var fsQueueingService = new FilesystemMessageQueueingService(tempDir);
            fsQueueingService.Init();

            var mockListener = new Mock<IQueueListener>();
            mockListener.Setup(
                x =>
                    x.MessageReceived(It.IsAny<Message>(), It.IsAny<IQueuedMessageContext>(),
                        It.IsAny<CancellationToken>()))
                .Callback<Message, IQueuedMessageContext, CancellationToken>((msg, ctx, ct) =>
                {
                    ctx.Acknowledge();
                    listenerCalledEvent.Set();
                })
                .Returns(Task.FromResult(true));

            await fsQueueingService.CreateQueue(queueName, mockListener.Object, new QueueOptions {MaxAttempts = 1});

            var message = new Message(new MessageHeaders
            {
                {HeaderName.ContentType, "text/plain"},
                {HeaderName.MessageId, Guid.NewGuid().ToString()}
            }, "Hello, world!");

            await fsQueueingService.EnqueueMessage(queueName, message, Thread.CurrentPrincipal);
            await listenerCalledEvent.WaitOneAsync(TimeSpan.FromSeconds(1));

            // The listener is called before the file is deleted, so there is a possible
            // race condition here.  Wait for a second to allow the delete to take place
            // before enumerating the files to see that they were actually deleted.
            await Task.Delay(TimeSpan.FromSeconds(1));

            var messageEqualityComparer = new MessageEqualityComparer();
            mockListener.Verify(
                x =>
                    x.MessageReceived(It.Is<Message>(m => messageEqualityComparer.Equals(m, message)),
                        It.IsAny<IQueuedMessageContext>(), It.IsAny<CancellationToken>()), Times.Once());

            var queuedMessages = queueDir.EnumerateFiles()
                .Select(f => new MessageFile(f))
                .ToList();

            Assert.That(queuedMessages, Is.Empty);
        }
        public void Start()
        {
            var connectionStringName = _configuration.Get(c => c.RabbitMqConnectionStringName);
            var connectionString = _configuration.ConnectionString(connectionStringName);
            var exchangeName = new ExchangeName(_configuration.Get(c => c.RabbitMqExchangeName));
            var queueName = new QueueName(_configuration.Get(c => c.RabbitMqQueueName));
            var routingKeys = _messageParserFactory.GetRoutingKeys();

            _receiver.Connect(exchangeName, connectionString);
            _receiver.Subscribe(queueName, b => Task.Factory.StartNew(() => Handle(b)), routingKeys.ToArray());
        }
        public async Task CreateQueue(QueueName queueName, IQueueListener listener, QueueOptions options = default(QueueOptions), CancellationToken cancellationToken = default(CancellationToken))
        {
            var queue = new SQLMessageQueue(_connectionProvider, _dialect, queueName, listener, options);
            if (!_queues.TryAdd(queueName, queue))
            {
                throw new QueueAlreadyExistsException(queueName);
            }

            Log.DebugFormat("Initializing SQL queue named \"{0}\"...", queueName);
            await queue.Init();
            Log.DebugFormat("SQL queue \"{0}\" created successfully", queueName);
        }
        public async Task CreateQueue(QueueName queueName, IQueueListener listener, QueueOptions options = default(QueueOptions), CancellationToken cancellationToken = default(CancellationToken))
        {
            CheckDisposed();
            var queue = new SQLiteMessageQueue(_baseDirectory, queueName, listener, options);
            if (!_queues.TryAdd(queueName, queue))
            {
                throw new QueueAlreadyExistsException(queueName);
            }

            await queue.Init();
            Log.DebugFormat("SQLite queue \"{0}\" created successfully", queueName);
        }
示例#24
0
 public void Send(QueueName destination, Message msg)
 {
     Logger.DebugFormat("Send {0} to {1} via MSMQ", msg.Body, destination);
       using (Msmq.MessageQueue q = new Msmq.MessageQueue(destination.Name))
       {
     Msmq.Message m = new Msmq.Message();
     m.Label = msg.Body != null ? msg.Body.GetType().ToString() : "MSG";
     m.Recoverable = true;
     m.Formatter = MessageFormater;
     m.Body = msg.Body;
     q.Send(m);
       }
 }
 public static Event ToEvent(this BrokeredMessage message,
    QueueName queueName)
     {
     return
         new Event()
         {
             Body = message.GetBody<string>(),
             ContentType = message.ContentType,
             EventType = queueName.TopicName,
             QueueName = queueName.ToString(),
             UnderlyingMessage = message
         };
 }
示例#26
0
 public SQLiteMessageQueue(DirectoryInfo baseDirectory, QueueName queueName, IQueueListener listener,
     QueueOptions options = default(QueueOptions))
     : base(InitDb(baseDirectory, queueName), new SQLiteDialect(), queueName, listener, options)
 {
     _cancellationTokenSource = new CancellationTokenSource();
     _operationQueue = new ActionBlock<ISQLiteOperation>(
         op => op.Execute(),
         new ExecutionDataflowBlockOptions
         {
             CancellationToken = _cancellationTokenSource.Token,
             MaxDegreeOfParallelism = 1
         });
 }
示例#27
0
        public void AddSubscriber(Type messageType, QueueName destination)
        {
            Condition.Requires(messageType, "messageType").IsNotNull();
              Condition.Requires(destination, "destination").IsNotNull();

              SubscriptionRegistration registration;
              if (!Subscriptions.TryGet(messageType, out registration))
            registration = new SubscriptionRegistration();

              if (!registration.SubscriberQueueNames.Any(q => q == destination))
            registration.SubscriberQueueNames.Add(destination.Name);

              Subscriptions.Put(messageType, registration);
        }
示例#28
0
 public async Task PushAsync(Event message)
 {
     var queueName = new QueueName(message.QueueName);
     if (queueName.IsSimpleQueue)
     {
         var client = _clientProvider.GetQueueClient(queueName);
         await client.SendAsync(message.ToMessage());
     }
     else
     {
         var client = _clientProvider.GetTopicClient(queueName);
         await client.SendAsync(message.ToMessage());
     }
 }
        public void CheckAvailabilityOfQueue(QueueName queue)
        {
            var userName = WindowsIdentity.GetCurrent().Name;

            if (!MessageQueue.Exists(queue.GetQueueLocalName()))
            {
                MessageQueue.Create(queue.GetQueueLocalName(), true); // transactional
                MsmqPermissionManager.SetPermissionsForQueue(queue.GetQueueLocalName(), userName, _configuration.Name);
            }
            else
            {
                MsmqPermissionManager.SetPermissionsForQueue(queue.GetQueueLocalName(), userName, _configuration.Name);
            }
        }
        public async Task Given_Queued_Message_When_Acknowledged_Then_Message_Should_Be_Deleted()
        {
            var listenerCalledEvent = new ManualResetEvent(false);
            var queueName = new QueueName(Guid.NewGuid().ToString());
            var rmqQueueingService = new RabbitMQMessageQueueingService(RabbitMQUri);
            try
            {
                var mockListener = new Mock<IQueueListener>();
                mockListener.Setup(
                    x =>
                        x.MessageReceived(It.IsAny<Message>(), It.IsAny<IQueuedMessageContext>(),
                            It.IsAny<CancellationToken>()))
                    .Callback<Message, IQueuedMessageContext, CancellationToken>((msg, ctx, ct) =>
                    {
                        ctx.Acknowledge();
                        listenerCalledEvent.Set();
                    })
                    .Returns(Task.FromResult(true));

                await rmqQueueingService.CreateQueue(queueName, mockListener.Object, new QueueOptions {MaxAttempts = 1});

                var message = new Message(new MessageHeaders
                {
                    {HeaderName.ContentType, "text/plain"},
                    {HeaderName.MessageId, Guid.NewGuid().ToString()}
                }, "Hello, world!");

                await rmqQueueingService.EnqueueMessage(queueName, message, Thread.CurrentPrincipal);
                await listenerCalledEvent.WaitOneAsync(TimeSpan.FromSeconds(1));

                // The listener is called before the message is published to the retry queue, 
                // so there is a possible race condition here.  Wait for a second to allow the 
                // publish to take place before checking the queueu and retry queue depth.
                await Task.Delay(TimeSpan.FromSeconds(1));

                var messageEqualityComparer = new MessageEqualityComparer();
                mockListener.Verify(
                    x =>
                        x.MessageReceived(It.Is<Message>(m => messageEqualityComparer.Equals(m, message)),
                            It.IsAny<IQueuedMessageContext>(), It.IsAny<CancellationToken>()), Times.Once());

                Assert.That(GetQueueDepth(queueName), Is.EqualTo(0));
                Assert.That(GetQueueDepth(queueName.GetRetryQueueName()), Is.EqualTo(0));
            }
            finally
            {
                rmqQueueingService.Dispose();
                DeleteQueue(queueName);
            }
        }
 public virtual bool CanAccept()
 {
     return(!QueueName.IsEmpty() &&
            !SelectedMachine.IsEmpty());
 }
示例#32
0
 protected override Task <bool> MessageDead(QueueName queueName, Message message)
 {
     // TODO: Bind new queue to dead letter exchange and check queue
     return(Task.FromResult(true));
 }
示例#33
0
        protected override void Execute(CodeActivityContext context)
        {
            // Open the config file and get the connection string
            Configuration            config = WebConfigurationManager.OpenWebConfiguration("/RequestWeb");
            ConnectionStringsSection css    =
                (ConnectionStringsSection)config.GetSection("connectionStrings");
            string connectionString = css.ConnectionStrings["Request"].ConnectionString;

            // Lookup the Queue
            RequestDataContext dc = new RequestDataContext(connectionString);

            Queue queue = dc.Queues.SingleOrDefault(x => x.QueueName == QueueName.Get(context));

            if (queue == null)
            {
                throw new InvalidProgramException("The specified queue (" + QueueName.Get(context) + ") was not found");
            }

            if (queue.AllowSelection)
            {
                IEnumerable <Request> q = dc.Requests
                                          .Where(x => x.Queue.QueueName == QueueName.Get(context) &&
                                                 x.QC == QC.Get(context) &&
                                                 (x.AssignedOperator == null ||
                                                  x.AssignedOperator == OperatorID.Get(context)))
                                          .OrderBy(x => x.AssignedDate);

                if (q.Count() > 0)
                {
                    Request[] reqList = new Request[q.Count()];
                    int       i       = 0;
                    foreach (Request r in q)
                    {
                        reqList[i++] = r;
                    }

                    RequestList.Set(context, reqList);
                }
            }
            else
            {
                IEnumerable <Request> q = dc.Requests
                                          .Where(x => x.Queue.QueueName == QueueName.Get(context) &&
                                                 x.QC == QC.Get(context) &&
                                                 (x.AssignedOperator == null ||
                                                  x.AssignedOperator == OperatorID.Get(context)))
                                          .OrderBy(x => x.AssignedDate)
                                          .Take(1);

                if (q.Count() > 0)
                {
                    Request r = q.First <Request>();
                    r.AssignedOperator = OperatorID.Get(context);
                    dc.SubmitChanges();

                    Request[] reqList = new Request[1];
                    reqList[0] = r;

                    RequestList.Set(context, reqList);
                }
            }
        }
示例#34
0
 /// <summary>
 /// Initializes a new <see cref="HandlingRule"/> with the supplied message
 /// <paramref name="specification"/>, <see cref="MessageHandler"/>, and
 /// <paramref name="queueName"/>.
 /// </summary>
 /// <param name="specification">The message specification that selects messages
 /// to which the handling rule applies</param>
 /// <param name="messageHandler">The handler to which messages matching the
 /// specification will be routed</param>
 /// <param name="queueName">(Optional) The name of the queue in which matching
 /// messages will be placed while they await handling</param>
 /// <param name="queueOptions">(Optional) Options for how queued messages for the handler
 /// should be processed</param>
 /// <remarks>
 /// If the <paramref name="queueName"/> is ommitted, a default queue name will
 /// be generated based on the MD5 hash of the full type name of the supplied
 /// <paramref name="messageHandler"/>
 /// </remarks>
 /// <seealso cref="GenerateQueueName"/>
 public HandlingRule(IMessageSpecification specification, IMessageHandler messageHandler, QueueName queueName = null, QueueOptions queueOptions = null)
 {
     Specification  = specification ?? throw new ArgumentNullException(nameof(specification));
     MessageHandler = messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
     QueueName      = queueName ?? GenerateQueueName(messageHandler);
     QueueOptions   = queueOptions;
 }
示例#35
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddJustSaying(config =>
            {
                config.Client(x =>
                {
                    if (_configuration.HasAWSServiceUrl())
                    {
                        // The AWS client SDK allows specifying a custom HTTP endpoint.
                        // For testing purposes it is useful to specify a value that
                        // points to a docker image such as `p4tin/goaws` or `localstack/localstack`
                        x.WithServiceUri(_configuration.GetAWSServiceUri())
                        .WithAnonymousCredentials();
                    }
                    else
                    {
                        // The real AWS environment will require some means of authentication
                        //x.WithBasicCredentials("###", "###");
                        //x.WithSessionCredentials("###", "###", "###");
                    }
                });
                config.Messaging(x =>
                {
                    // Configures which AWS Region to operate in
                    x.WithRegion(_configuration.GetAWSRegion());
                });
                config.Subscriptions(x =>
                {
                    // Creates the following if they do not already exist
                    //  - a SQS queue of name `orderingapi-orderreadyevent`
                    //  - a SQS queue of name `orderingapi-orderreadyevent_error`
                    //  - a SNS topic of name `orderreadyevent`
                    //  - a SNS topic subscription on topic 'orderreadyevent'
                    x.ForTopic <OrderReadyEvent>(QueueName.Create <OrderReadyEvent>(_configuration));

                    // Add another subscription just to show that we can (subscribing to our own publish!)
                    x.ForTopic <OrderPlacedEvent>(QueueName.Create <OrderPlacedEvent>(_configuration));
                });
                config.Publications(x =>
                {
                    // Creates the following if they do not already exist
                    //  - a SNS topic of name `orderplacedevent`
                    x.WithTopic <OrderPlacedEvent>();
                });
            });

            // Added a message handler for message type for 'OrderReadyEvent' on topic 'orderreadyevent'
            services.AddJustSayingHandler <OrderReadyEvent, OrderReadyEventHandler>();

            // Added another message handler for message type for 'OrderPlacedEvent' on topic 'orderplacedevent'
            services.AddJustSayingHandler <OrderPlacedEvent, OrderPlacedEventHandler>();

            // Add a background service that is listening for messages related to the above subscriptions
            services.AddHostedService <Subscriber>();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Restaurant Ordering API", Version = "v1"
                });
            });
        }
示例#36
0
        /// <inheritdoc/>
        public Task <QueuedMessageList> ListMessagesAsync(QueueName queueName, QueuedMessageListId marker, int?limit, bool echo, bool includeClaimed, CancellationToken cancellationToken)
        {
            if (queueName == null)
            {
                throw new ArgumentNullException("queueName");
            }
            if (limit <= 0)
            {
                throw new ArgumentOutOfRangeException("limit");
            }

            UriTemplate.UriTemplate template = new UriTemplate.UriTemplate("/queues/{queue_name}/messages?marker={marker}&limit={limit}&echo={echo}&include_claimed={include_claimed}");

            var parameters =
                new Dictionary <string, string>()
            {
                { "queue_name", queueName.Value },
                { "echo", echo.ToString() },
                { "include_claimed", includeClaimed.ToString() }
            };

            if (marker != null)
            {
                parameters["marker"] = marker.Value;
            }
            if (limit != null)
            {
                parameters["limit"] = limit.ToString();
            }

            Func <Task <Tuple <IdentityToken, Uri> >, HttpWebRequest> prepareRequest =
                PrepareRequestAsyncFunc(HttpMethod.GET, template, parameters);

            Func <Task <HttpWebRequest>, Task <ListCloudQueueMessagesResponse> > requestResource =
                GetResponseAsyncFunc <ListCloudQueueMessagesResponse>(cancellationToken);

            Func <Task <ListCloudQueueMessagesResponse>, QueuedMessageList> resultSelector =
                task =>
            {
                ReadOnlyCollection <QueuedMessage> messages = null;
                if (task.Result != null)
                {
                    messages = task.Result.Messages;
                }

                QueuedMessageListId nextMarker = null;
                if (task.Result != null && task.Result.Links != null)
                {
                    Link nextLink = task.Result.Links.FirstOrDefault(i => string.Equals(i.Rel, "next", StringComparison.OrdinalIgnoreCase));
                    if (nextLink != null)
                    {
                        Uri baseUri = new Uri("https://example.com");
                        Uri absoluteUri;
                        if (nextLink.Href.StartsWith("/v1"))
                        {
                            absoluteUri = new Uri(baseUri, nextLink.Href.Substring("/v1".Length));
                        }
                        else
                        {
                            absoluteUri = new Uri(baseUri, nextLink.Href);
                        }

                        UriTemplate.UriTemplateMatch match = template.Match(baseUri, absoluteUri);
                        if (!string.IsNullOrEmpty(match.BoundVariables["marker"]))
                        {
                            nextMarker = new QueuedMessageListId(match.BoundVariables["marker"]);
                        }
                    }
                }

                if (messages == null || messages.Count == 0)
                {
                    // use the same marker again
                    messages   = messages ?? new ReadOnlyCollection <QueuedMessage>(new QueuedMessage[0]);
                    nextMarker = marker;
                }

                Func <CancellationToken, Task <ReadOnlyCollectionPage <QueuedMessage> > > getNextPageAsync = null;
                if (nextMarker != null || messages.Count == 0)
                {
                    getNextPageAsync =
                        nextCancellationToken => ListMessagesAsync(queueName, nextMarker, limit, echo, includeClaimed, nextCancellationToken)
                        .Select(t => (ReadOnlyCollectionPage <QueuedMessage>)t.Result);
                }

                return(new QueuedMessageList(messages, getNextPageAsync, nextMarker));
            };

            return(AuthenticateServiceAsync(cancellationToken)
                   .Select(prepareRequest)
                   .Then(requestResource)
                   .Select(resultSelector));
        }
示例#37
0
 /// <summary>
 /// Initializes a new SQL diagnostic event
 /// </summary>
 /// <param name="source">The object that emitted the event</param>
 /// <param name="type">The type of event</param>
 /// <param name="detail">Specific details regarding this instance of the event</param>
 /// <param name="exception">The exception related to the event, if applicable</param>
 /// <param name="message">The message to which the event pertains, if applicable</param>
 /// <param name="endpoint">The name of the endpoint, if applicable</param>
 /// <param name="queue">The queue to which the event pertains, if applicable</param>
 /// <param name="topic">The topic to which the message pertains, if applicable</param>
 /// <param name="connectionName">The name of the connection, if applicable</param>
 /// <param name="commandText">The query string, if applicable</param>
 public SQLEvent(object source, DiagnosticEventType type, string detail = null, Exception exception = null, Message message = null, EndpointName endpoint = null, QueueName queue = null, TopicName topic = null, string connectionName = null, string commandText = null)
     : base(source, type, detail, exception, message, endpoint, queue, topic)
 {
     ConnectionName = connectionName;
     CommandText    = commandText;
 }
示例#38
0
 /// <summary>
 /// There is no way to check if a queue exists
 /// so we return false so queue is created.
 /// Creation of queue is idempotent in RabbitMQ.
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public Task <bool> QueueExistsAsync(QueueName name)
 {
     return(Task.FromResult(false));
 }
示例#39
0
        public void GivenNameWhenEmptyShouldThrowsArgumentNullException(string name)
        {
            Func <QueueName> action = () => QueueName.Create(name);

            action.Should().Throw <ArgumentNullException>();
        }
示例#40
0
        public static Task Invoke(this IProcessorActor actor, QueueName queueName, Message msg, IEventQueueOperator queueOperator)
        {
            var ev = msg.ToEvent(queueName);

            return(FactoryActor.ProcessEvent(actor, ev, queueOperator));
        }
 public void Send(QueueName destination, Message m)
 {
     LastDestination = destination;
     LastMessage     = m;
 }
示例#42
0
 public QueueSubscriber()
 {
     Queue  = new QueueName(Settings.Default.Queue);
     Client = new QueueClient();
 }
示例#43
0
 /// <summary>
 /// Creates a new <see cref="InMemoryMessageQueue"/>
 /// </summary>
 /// <param name="queueName">The name of the queue</param>
 /// <param name="listener">The object that will be notified when messages are
 ///     added to the queue</param>
 /// <param name="options">(Optional) Settings that influence how the queue behaves</param>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="queueName"/>, or
 /// <paramref name="listener"/> are <c>null</c></exception>
 public InMemoryMessageQueue(QueueName queueName, IQueueListener listener, QueueOptions options = null)
     : base(queueName, listener, options)
 {
 }
示例#44
0
 protected static DiagnosticEvent GenerateDiagnosticEvent(object source, DiagnosticEventType type,
                                                          Message message = null, string detail = null, QueueName queue = null, TopicName topic = null,
                                                          Exception ex    = null)
 {
     return(new DiagnosticEventBuilder(source, type)
     {
         Message = message,
         Detail = detail,
         Exception = ex,
         Queue = queue,
         Topic = topic
     }.Build());
 }
示例#45
0
 /// <summary>
 /// Initializes a new <see cref="MessageQueueEventArgs"/>
 /// </summary>
 /// <param name="queue">The name of the queue from which the message was read</param>
 /// <param name="queuedMessage">The queued message for which the handling attempt was made</param>
 public MessageQueueEventArgs(QueueName queue, QueuedMessage queuedMessage)
 {
     Queue         = queue;
     QueuedMessage = queuedMessage;
 }
示例#46
0
 /// <summary>
 /// Initializes a new <see cref="MessageQueueEventArgs"/>
 /// </summary>
 /// <param name="queue">The name of the queue from which the message was read</param>
 /// <param name="queuedMessage">The queued message for which the handling attempt was made</param>
 /// <param name="exception">The exception that was caught</param>
 public MessageQueueEventArgs(QueueName queue, QueuedMessage queuedMessage, Exception exception)
 {
     Queue         = queue;
     QueuedMessage = queuedMessage;
     Exception     = exception;
 }
示例#47
0
        /// <inheritdoc/>
        public Task <Claim> ClaimMessageAsync(QueueName queueName, int?limit, TimeSpan timeToLive, TimeSpan gracePeriod, CancellationToken cancellationToken)
        {
            if (queueName == null)
            {
                throw new ArgumentNullException("queueName");
            }
            if (limit < 0)
            {
                throw new ArgumentOutOfRangeException("limit");
            }
            if (timeToLive <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("timeToLive");
            }
            if (gracePeriod < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("gracePeriod");
            }

            UriTemplate.UriTemplate template = new UriTemplate.UriTemplate("/queues/{queue_name}/claims?limit={limit}");

            var parameters =
                new Dictionary <string, string>()
            {
                { "queue_name", queueName.Value },
            };

            if (limit != null)
            {
                parameters["limit"] = limit.ToString();
            }

            var request = new ClaimMessagesRequest(timeToLive, gracePeriod);

            Func <Task <Tuple <IdentityToken, Uri> >, Task <HttpWebRequest> > prepareRequest =
                PrepareRequestAsyncFunc(HttpMethod.POST, template, parameters, request);

            Func <Task <Tuple <HttpWebResponse, string> >, Task <Tuple <Uri, IEnumerable <QueuedMessage> > > > parseResult =
                task =>
            {
                string location    = task.Result.Item1.Headers[HttpResponseHeader.Location];
                Uri    locationUri = location != null ? new Uri(_baseUri, location) : null;

                if (task.Result.Item1.StatusCode == HttpStatusCode.NoContent)
                {
                    // the queue did not contain any messages to claim
                    Tuple <Uri, IEnumerable <QueuedMessage> > result = Tuple.Create(locationUri, Enumerable.Empty <QueuedMessage>());
                    return(InternalTaskExtensions.CompletedTask(result));
                }

                IEnumerable <QueuedMessage> messages = JsonConvert.DeserializeObject <IEnumerable <QueuedMessage> >(task.Result.Item2);

                return(InternalTaskExtensions.CompletedTask(Tuple.Create(locationUri, messages)));
            };
            Func <Task <HttpWebRequest>, Task <Tuple <Uri, IEnumerable <QueuedMessage> > > > requestResource =
                GetResponseAsyncFunc <Tuple <Uri, IEnumerable <QueuedMessage> > >(cancellationToken, parseResult);

            Func <Task <Tuple <Uri, IEnumerable <QueuedMessage> > >, Claim> resultSelector =
                task => new Claim(this, queueName, task.Result.Item1, timeToLive, TimeSpan.Zero, true, task.Result.Item2);

            return(AuthenticateServiceAsync(cancellationToken)
                   .Then(prepareRequest)
                   .Then(requestResource)
                   .Select(resultSelector));
        }
示例#48
0
 public MsmqQueue(QueueName queueName)
 {
     _queueName = queueName;
 }
示例#49
0
 /// <summary>
 /// Initializes a new <see cref="HttpEvent"/>
 /// </summary>
 /// <param name="source">The object that emitted the event</param>
 /// <param name="type">The type of event</param>
 /// <param name="detail">Specific details regarding this instance of the event</param>
 /// <param name="exception">The exception related to the event, if applicable</param>
 /// <param name="message">The message to which the event pertains, if applicable</param>
 /// <param name="endpoint">The name of the endpoint, if applicable</param>
 /// <param name="queue">The queue to which the event pertains, if applicable</param>
 /// <param name="topic">The topic to which the message pertains, if applicable</param>
 /// <param name="remote">The IP or hostname of the remote client</param>
 /// <param name="method">The HTTP method specified in the request</param>
 /// <param name="uri">The request URI</param>
 /// <param name="status">The HTTP status code in the response</param>
 public HttpEvent(object source, DiagnosticEventType type, string detail = null, Exception exception = null, Message message = null, EndpointName endpoint = null, QueueName queue = null, TopicName topic = null, string remote = null, string method = null, Uri uri = null, int?status = null)
     : base(source, type, detail, exception, message, endpoint, queue, topic)
 {
     Remote = remote;
     Method = method;
     Uri    = uri;
     Status = status;
 }
示例#50
0
 protected abstract Task GivenExistingQueuedMessage(QueueName queueName, Message message, IPrincipal principal);
示例#51
0
        protected override void Execute(CodeActivityContext context)
        {
            // Get the connection string
            DBConnection ext = context.GetExtension <DBConnection>();

            if (ext == null)
            {
                throw new InvalidProgramException("No connection string available");
            }

            // Lookup the Queue
            UserTasksDataContext dc = new UserTasksDataContext(ext.ConnectionString);
            Queue q = dc.Queues.SingleOrDefault(x => x.QueueName == QueueName.Get(context));

            if (q == null)
            {
                throw new InvalidProgramException("The specified queue (" + QueueName.Get(context) + ") was not found");
            }

            SubQueue s = dc.SubQueues
                         .SingleOrDefault(x => x.QueueID == q.QueueID &&
                                          x.SubQueueName == SubQueueName.Get(context));

            if (s == null)
            {
                throw new InvalidProgramException("The specified sub-queue (" + SubQueueName.Get(context) + ") was not found");
            }

            // Create and initialize a QueueInstance object
            QueueInstance qi = new QueueInstance();

            qi.QueueInstanceKey  = Guid.NewGuid();
            qi.CurrentSubQueueID = s.SubQueueID;
            qi.CreateDate        = DateTime.UtcNow;
            qi.InstanceID        = context.WorkflowInstanceId;

            // Setup the initial values
            qi.AssignedDate       = null;
            qi.AssignedOperatorID = null;
            qi.QC       = false;
            qi.Priority = null;

            // Insert the Request record
            PersistQueueInstance persist = context.GetExtension <PersistQueueInstance>();

            persist.AddQueueInstance(qi);

            // Return the QueueInstance object
            QueueInstanceKey.Set(context, qi.QueueInstanceKey);

            CustomTrackingRecord userRecord = new CustomTrackingRecord("Start")
            {
                Data =
                {
                    { "QueueInstanceKey", qi.QueueInstanceKey  },
                    { "SubQueueID",       qi.CurrentSubQueueID }
                }
            };

            // Emit the custom tracking record
            context.Track(userRecord);
        }
示例#52
0
 protected abstract Task <bool> MessageDead(QueueName queueName, Message message);
示例#53
0
        protected override void Execute(CodeActivityContext context)
        {
            // Get the connection string
            DBConnection ext = context.GetExtension <DBConnection>();

            if (ext == null)
            {
                throw new InvalidProgramException("No connection string available");
            }

            UserTasksDataContext dc = new UserTasksDataContext(ext.ConnectionString);

            Queue    q          = null;
            SubQueue sq         = null;
            int      queueID    = 0;
            int      subQueueID = 0;

            if (QueueName.Get(context) != "" && SubQueueName.Get(context) != "None")
            {
                // Lookup the queue and subqueue
                q = dc.Queues.SingleOrDefault(x => x.QueueName == QueueName.Get(context));
                if (q == null)
                {
                    throw new InvalidProgramException("The specified queue (" + QueueName.Get(context) + ") was not found");
                }

                sq = dc.SubQueues.SingleOrDefault(x => x.QueueID == q.QueueID &&
                                                  x.SubQueueName == SubQueueName.Get(context));
                if (sq == null)
                {
                    throw new InvalidProgramException("The specified subqueue (" +
                                                      QueueName.Get(context) + " - " +
                                                      SubQueueName.Get(context) + ") was not found");
                }

                queueID    = q.QueueID;
                subQueueID = sq.SubQueueID;
            }

            // Lookup the QueueInstance
            dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dc.QueueInstances);
            QueueInstance qi = dc.QueueInstances.SingleOrDefault(x => x.QueueInstanceKey == QueueInstanceKey.Get(context));

            if (qi == null)
            {
                throw new InvalidProgramException("The specified request (" + QueueInstanceKey.Get(context) + ") was not found");
            }

            // Assign the QueueInstance to this subqueue
            if (sq != null)
            {
                qi.CurrentSubQueueID = sq.SubQueueID;
            }
            else
            {
                qi.CurrentSubQueueID = null;
            }

            qi.AssignedDate       = null;
            qi.AssignedOperatorID = null;
            qi.QC       = false;
            qi.Priority = null;

            // Update the QueueInstance record
            PersistQueueInstance persist = context.GetExtension <PersistQueueInstance>();

            persist.AddQueueInstance(qi);

            // Add a custom track record
            CustomTrackingRecord userRecord = new CustomTrackingRecord("Route")
            {
                Data =
                {
                    { "QueueInstanceKey", qi.QueueInstanceKey  },
                    { "SubQueueID",       qi.CurrentSubQueueID }
                }
            };

            // Emit the custom tracking record
            context.Track(userRecord);
        }
示例#54
0
 private void Window_Activated(object sender, EventArgs e)
 {
     QueueName.Focus();
 }
示例#55
0
        protected override Task <bool> MessageQueued(QueueName queueName, Message message)
        {
            var messagesInQueue = GetQueueDepth(queueName) > 0;

            return(Task.FromResult(messagesInQueue));
        }
示例#56
0
 protected virtual async Task AssertMessageNotDead(QueueName queue, Message message)
 {
     Assert.False(await MessageDead(queue, message));
 }
示例#57
0
 protected virtual async Task AssertMessageStillQueuedForRetry(QueueName queue, Message message)
 {
     Assert.True(await MessageQueued(queue, message));
 }
示例#58
0
 public async Task <PollerResult <Event> > NextAsync(QueueName name)
 {
     throw new NotSupportedException();
 }
示例#59
0
 public SQLMessageQueueInspector(SQLMessageQueueingService messageQueueingService, QueueName queueName,
                                 ISecurityTokenService securityTokenService, IMessageEncryptionService messageEncryptionService)
     : base(queueName, new NoopQueueListener(), null, null, messageQueueingService.ConnectionProvider,
            messageQueueingService.CommandBuilders, securityTokenService, messageEncryptionService)
 {
 }
示例#60
0
 protected virtual async Task AssertMessageIsDead(QueueName queue, Message message)
 {
     Assert.True(await MessageDead(queue, message));
 }