示例#1
0
        /// <inheritdoc />
        public MessageBusServiceHandshakeReplyMessage SendMessageBusHandshake(IEnumerable <Shared.Messages.SubscribeHandler> subscribeHandlers, IEnumerable <Shared.Messages.RequestHandler> requestHandlers, CancellationToken cancellationToken)
        {
            var messageBusServiceHandshakeMessage = new MessageBusServiceHandshakeMessage
            {
                ServerName        = _processInformation.MachineName,
                ServiceName       = _messageBusConfig.ServiceName,
                SendDateTime      = DateTimeOffset.Now,
                SubscribeHandlers = subscribeHandlers,
                RequestHandlers   = requestHandlers,
                ReplyQueue        = _queueNameUtility.ReplyQueue <MessageBusServiceHandshakeMessage>()
            };

            using (var replyQueue = _queueFactory.CreateLocale(messageBusServiceHandshakeMessage.ReplyQueue, true, LocaleQueueMode.TemporaryMaster, false, AccessMode.Receive))
            {
                _logger.Debug("Message Broker Client sending Message Bus Handshake {@Message}", messageBusServiceHandshakeMessage);

                SendToMessageBroker(messageBusServiceHandshakeMessage);

                var messageBusServiceHandshakeReplyMessage = replyQueue.Receive <MessageBusServiceHandshakeReplyMessage>(30000, cancellationToken);

                if (messageBusServiceHandshakeReplyMessage == null)
                {
                    throw new MessageBusHandshakeTimeoutException(messageBusServiceHandshakeMessage);
                }

                messageBusServiceHandshakeReplyMessage.CompletedDateTime = DateTimeOffset.Now;

                _logger.Debug("Message Broker Client has received reply for Message bus Handshake {@ReplyMessage}", messageBusServiceHandshakeReplyMessage);

                return(messageBusServiceHandshakeReplyMessage);
            }
        }
示例#2
0
        public void SendResponseMessageToLocaleRequesterShouldSendToReplyQueue()
        {
            var replyQueue = Substitute.For <ILocaleQueue>();

            _queueFactory.CreateLocale("RequestMyReplyQueue", Arg.Any <bool>(), Arg.Any <LocaleQueueMode>(), Arg.Any <bool>(), Arg.Any <AccessMode>()).Returns(replyQueue);

            using (var messageBroker = CreateMessageBroker())
            {
                messageBroker.SendResponseMessage("RequestMyReplyQueue", new RequestMessage {
                    RequesterServerName = "TestServer"
                }, "MyResponse");
            }

            replyQueue.Received(1).Send(Arg.Any <ResponseMessage>());
        }
        private void Start(string queueName, bool privateQueue, LocaleQueueMode localeQueueMode, bool transactional, Action <object, CancellationToken> messageHandler, Action heartbeatHandler, int heartRateMilliseconds, bool multiThreadedHandler, bool syncMode, CancellationToken cancellationToken)
        {
            if (_cancellationTokenSource != null)
            {
                throw new ArgumentException("Handler not stopped");
            }

            if (heartRateMilliseconds <= 0 && heartbeatHandler != null)
            {
                throw new ArgumentException("Invalid Heart Rate", nameof(heartRateMilliseconds));
            }

            _queueName = queueName;
            _cancellationTokenSource = new CancellationTokenSource();
            _syncMode                      = syncMode;
            _queue                         = _queueFactory.CreateLocale(_queueName, privateQueue, localeQueueMode, transactional, AccessMode.Receive);
            _messageHandler                = messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
            _heartbeatHandler              = heartbeatHandler;
            _heartRateMilliseconds         = heartRateMilliseconds;
            _multiThreadedHandler          = multiThreadedHandler;
            _cancellationTokenRegistration = cancellationToken.Register(Stop);

            if (_syncMode)
            {
                Process();
            }
            else
            {
                _processTask = _taskFactory.Create();
                _processTask.Start(Process, _cancellationTokenSource.Token);
            }

            _logger.Information("Queue Handler started {QueueName}", _queueName);
        }
示例#4
0
 /// <inheritdoc />
 public MessageBroker(ILogger logger, MessageBusConfig messageBusConfig, IQueueFactory queueFactory, IProcessInformation processInformation, IQueueNameUtility queueNameUtility)
 {
     _logger             = logger;
     _messageBusConfig   = messageBusConfig;
     _queueFactory       = queueFactory;
     _processInformation = processInformation;
     _queueNameUtility   = queueNameUtility;
     _messageBrokerQueue = _queueFactory.CreateLocale(MessageBrokerConfig.LocaleQueueName, true, LocaleQueueMode.Durable, true, AccessMode.Send);
 }
        public QueueHandlerSyncTests()
        {
            _queue = Substitute.For <ILocaleQueue>();

            _queueFactory = Substitute.For <IQueueFactory>();
            _queueFactory.CreateLocale(Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <LocaleQueueMode>(), Arg.Any <bool>(), Arg.Any <AccessMode>()).Returns(_queue);
            _taskFactory = Substitute.For <ITaskFactory>();
            _taskFactory.Create().Returns(new SyncTestTask());
            _cancellationToken = new CancellationToken();
        }
        public QueueHandlerAsyncTests()
        {
            _queue = Substitute.For <ILocaleQueue>();
            _queue.Receive(Arg.Any <int>(), Arg.Any <CancellationToken>()).Returns(e => CreateMessage("Message1"), e => CreateMessage("Message2"), e => CreateMessage("Message3"), e => null);

            _queueFactory = Substitute.For <IQueueFactory>();
            _queueFactory.CreateLocale(Arg.Any <string>(), Arg.Any <bool>(), Arg.Any <LocaleQueueMode>(), Arg.Any <bool>(), Arg.Any <AccessMode>()).Returns(_queue);
            _taskFactory = new TaskFactory();

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken       = _cancellationTokenSource.Token;
        }
示例#7
0
        public MessageBrokerTests()
        {
            _messageBusConfig = new MessageBusConfig
            {
                ServiceName = "MessageBrokerTests"
            };

            _queueFactory       = Substitute.For <IQueueFactory>();
            _queueNameUtility   = new QueueNameUtility("UniTests");
            _processInformation = Substitute.For <IProcessInformation>();
            _processInformation.MachineName.Returns("TestServer");

            _cancellationToken = new CancellationToken();

            _messageBrokerQueue = Substitute.For <ILocaleQueue>();
            _messageBrokerQueue.Exists().Returns(true);
            _queueFactory.CreateLocale(MessageBrokerConfig.LocaleQueueName, Arg.Any <bool>(), Arg.Any <LocaleQueueMode>(), Arg.Any <bool>(), Arg.Any <AccessMode>()).Returns(_messageBrokerQueue);
        }
 private ILocaleQueue MockQueue(string name)
 {
     return(_queueFactory.CreateLocale(name, true, LocaleQueueMode.TemporaryMaster, true, AccessMode.SendAndReceive));
 }