Ejemplo n.º 1
0
        public void Setup()
        {
            _transportMessageSerializer = new TransportMessageSerializer();
            _configurationMock          = new Mock <IPersistenceConfiguration>();
            _configurationMock.Setup(conf => conf.SafetyPhaseDuration).Returns(500.Milliseconds());
            _configurationMock.SetupGet(x => x.ReplayBatchSize).Returns(_replayBatchSize);
            _configurationMock.SetupGet(x => x.ReplayUnackedMessageCountThatReleasesNextBatch).Returns(200);

            _selfPeer    = new Peer(new PeerId("Abc.Testing.Self"), "tcp://abctest:888");
            _targetPeer  = new Peer(new PeerId("Abc.Testing.Target"), "tcp://abcother:123");
            _anotherPeer = new Peer(new PeerId("Abc.Testing.Another"), "tcp://abcanother:123");

            _bus                = new TestBus();
            _transport          = new TestTransport(_selfPeer, "test");
            _messageMatcherMock = new Mock <IInMemoryMessageMatcher>();

            _replayId = Guid.NewGuid();

            _insertedMessages = new List <byte[]>();
            var readerMock = new Mock <IMessageReader>();

            _storageMock = new Mock <IStorage>();
            _storageMock.Setup(x => x.CreateMessageReader(It.IsAny <PeerId>())).Returns(readerMock.Object);

            readerMock.Setup(x => x.GetUnackedMessages()).Returns(_insertedMessages);

            var speedReporter = new Mock <IReporter>();

            _replayer = new MessageReplayer(_configurationMock.Object, _storageMock.Object, _bus, _transport, _messageMatcherMock.Object, _targetPeer, _replayId, speedReporter.Object, new MessageSerializer());

            _messageMatcherMock.Setup(x => x.EnqueueWaitHandle(It.IsAny <EventWaitHandle>())).Callback <EventWaitHandle>(x => x.Set());
        }
Ejemplo n.º 2
0
 public void Send(TransportMessage toSend, IEnumerable <string> destinations)
 {
     TransactionWrapper.RunInTransaction(transaction => {
         toSend.TimeSent       = DateTime.UtcNow;
         toSend.ReturnAddress  = this.ReturnAddress;
         var serializedMessage = string.Empty;
         using (var stream = new MemoryStream()) {
             TransportMessageSerializer.Serialize(toSend, stream);
             foreach (var destination in destinations)
             {
                 var conversationHandle = ServiceBrokerWrapper.SendOne(transaction, ReturnAddress, destination, NServiceBusTransportMessageContract, NServiceBusTransportMessage, stream.ToArray());
                 toSend.Id = conversationHandle.ToString();
                 if (Logger.IsDebugEnabled)
                 {
                     Logger.Debug(string.Format("Sending message {0} with ID {1} to destination {2}.\n" +
                                                "ToString() of the message yields: {3}\n" +
                                                "Message headers:\n{4}",
                                                toSend.Body[0].GetType().AssemblyQualifiedName,
                                                toSend.Id,
                                                destination,
                                                toSend.Body[0],
                                                string.Join(", ", toSend.Headers.Select(h => h.Key + ":" + h.Value).ToArray())
                                                ));
                 }
             }
         }
     });
 }
Ejemplo n.º 3
0
        public void ReceiveMessage(SqlTransaction transaction)
        {
            Message message = null;

            try {
                message = ServiceBrokerWrapper.WaitAndReceive(transaction, this.ListenerQueue, waitTimeout);
            } catch (Exception e) {
                Logger.Error("Error in receiving message from queue.", e);
                throw; // Throw to rollback
            } finally {
                transactionWaitPool.Release(1);
                releasedWaitLock = true;
            }

            // No message? That's okay
            if (message == null)
            {
                return;
            }

            Guid conversationHandle = message.ConversationHandle;

            try {
                // Only handle transport messages
                if (message.MessageTypeName == NServiceBusTransportMessage)
                {
                    TransportMessage transportMessage = null;
                    try {
                        transportMessage = TransportMessageSerializer.Deserialize(message.BodyStream, true);
                    } catch (Exception ex) {
                        Logger.Error("Could not extract message data.", ex);
                        OnSerializationFailed(conversationHandle, message, ex);
                        return; // deserialization failed - no reason to try again, so don't throw
                    }

                    // Set the message Id
                    if (string.IsNullOrEmpty(transportMessage.Id))
                    {
                        transportMessage.Id = conversationHandle.ToString();
                    }

                    // Set the correlation Id
                    if (string.IsNullOrEmpty(transportMessage.IdForCorrelation))
                    {
                        transportMessage.IdForCorrelation = transportMessage.Id;
                    }

                    ProcessMessage(message, transportMessage);
                }
            } finally {
                // End the conversation
                ServiceBrokerWrapper.EndConversation(transaction, conversationHandle);
            }
        }
Ejemplo n.º 4
0
 private void OnSerializationFailed(Guid conversationHandle, Message underlyingTransportObject, Exception exception)
 {
     try {
         TransportMessage transportMessage = null;
         try
         {
             transportMessage = TransportMessageSerializer.Deserialize(underlyingTransportObject.BodyStream, false);
         }
         catch (Exception)
         {
             // Eat this exception
         }
         this.WriteFailedMessage(conversationHandle, underlyingTransportObject, transportMessage, exception, 3);
         if (MessageFault != null)
         {
             MessageFault(this, new TransportMessageFaultEventArgs(transportMessage, exception, "SerializationFailed"));
         }
         SendFailureMessage(transportMessage, exception, "SerializationFailed");
     } catch (Exception e) {
         Logger.FatalFormat("Fault manager failed to process the failed message {0}", e, underlyingTransportObject);
         // TODO critical error will stop the transport from handling new messages
         //Configure.Instance.OnCriticalError();
     }
 }