コード例 #1
0
 public void Close()
 {
     nativeMessageProducer.Close();
 }
コード例 #2
0
        public void Send <T>(T message, TimeSpan timeToLive) where T : class
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("The object has been disposed");
            }

            Session session = _connection.CreateSession(true, SessionMode.SessionTransacted);

            if (session == null)
            {
                throw new EndpointException(Uri, "Unable to open session to endpoint");
            }

            Destination destination = session.CreateQueue(_queueName);

            MessageProducer producer = session.CreateProducer(destination);

            if (producer != null)
            {
                Type messageType = typeof(T);

                TextMessage textMessage = session.CreateTextMessage();

                using (MemoryStream mem = new MemoryStream())
                {
                    _serializer.Serialize(mem, message);

                    textMessage.Text = Encoding.UTF8.GetString(mem.ToArray());
                }

                if (timeToLive < TimeSpan.MaxValue)
                {
                    producer.TimeToLive = (long)timeToLive.TotalMilliseconds;
                }

                producer.DeliveryMode = _deliveryMode;

                producer.Send(textMessage);

                session.Commit();

                if (SpecialLoggers.Messages.IsInfoEnabled)
                {
                    SpecialLoggers.Messages.InfoFormat("SEND:{0}:{1}:{2}", Uri, messageType.Name, textMessage.MessageID);
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Sent {0} from {1} [{2}]", messageType.FullName, Uri, textMessage.MessageID);
                }

                producer.Close();
            }
            else
            {
                throw new EndpointException(Uri, "Unable to create message producer");
            }

            session.Close();
        }