Exemple #1
0
        public bool ExecuteTransaction(PatientDTO patient, string destination, string message)
        {
            bool failedDBCommand        = false;
            bool failedMessengerCommand = false;

            try
            {
                //_dBConnectionInfo = _repository.PrepareAdd(patient);
                try
                {
                    _messengerConnectionInfo = _client.SendTransactionalMessage(destination, message);
                }
                catch
                {
                    failedMessengerCommand = true;
                }
            }
            catch
            {
                try
                {
                    _messengerConnectionInfo = _client.SendTransactionalMessage(destination, message);
                }
                catch
                {
                    failedMessengerCommand = true;
                }
                failedDBCommand = true;
            }
            if (failedDBCommand == true || failedMessengerCommand == true)
            {
                _client.RollbackTransactionalMessage(_messengerConnectionInfo.Connection, _messengerConnectionInfo.Session);
                _repository.RollbackAdd(_dBConnectionInfo.Connection, _dBConnectionInfo.Transaction);
                return(false);
            }
            else
            {
                _client.CommitTransactionalMessage(_messengerConnectionInfo.Connection, _messengerConnectionInfo.Session);
                _repository.CommitAdd(_dBConnectionInfo.Connection, _dBConnectionInfo.Transaction);
                return(true);
            }
        }
        public Task <MessengerConnectionInfo> SendTransactionalMessageAsync(string destination, string message)
        {
            NMSConnectionFactory factory    = new NMSConnectionFactory(_brokerUri);
            IConnection          connection = factory.CreateConnection();

            connection.Start();

            ISession session = connection.CreateSession(AcknowledgementMode.Transactional);
            MessengerConnectionInfo messengerConnectionInfo = new MessengerConnectionInfo(connection, session);

            using (IDestination dest = session.GetQueue(destination))
                using (IMessageProducer producer = session.CreateProducer(dest))
                {
                    producer.DeliveryMode = MsgDeliveryMode.NonPersistent;

                    producer.Send(session.CreateTextMessage(message));
                }
            connection.Stop();
            return(Task.FromResult(messengerConnectionInfo));
        }