Beispiel #1
0
            public bool Matches(IDestination newTopic, string newSelector)
            {
                if (!topic.Equals(newTopic))
                {
                    return(false);
                }

                if (selector == null)
                {
                    return(newSelector == null);
                }
                else
                {
                    return(selector.Equals(newSelector));
                }
            }
        public void Send(IDestination destination, IMessage imessage, bool persistent, byte priority, TimeSpan timeToLive)
        {
            BaseMessage message = (BaseMessage) imessage;
            MessageQueue mq=null;
            MessageQueue responseQueue = null;
            MessageQueueTransaction transaction = null;
            try
            {
                // Locate the MSMQ Queue we will be sending to
                if (messageQueue != null)
                {
                    if( destination.Equals(this.destination) )
                    {
                        mq = messageQueue;
                    }
                    else
                    {
                        throw new NMSException("This producer can only be used to send to: " + destination);
                    }
                }
                else
                {
                    mq = openMessageQueue((Destination) destination);
                }
				
                // Convert the Mesasge into a MSMQ message
                message.NMSPersistent = persistent;
                message.NMSExpiration = timeToLive;
                message.NMSPriority = priority;
                
                // message.NMSTimestamp = new DateTime().Date.;
                Message msg = messageConverter.ToMsmqMessage(message);
                // TODO: message.NMSMessageId =
                // Now Send the message
                if( mq.Transactional )
                {
                    if (session.Transacted)
                    {
                        mq.Send(msg, session.MessageQueueTransaction);
                        
                    } else
                    {
                        // Start our own mini transaction here to send the message.
                        transaction = new MessageQueueTransaction();
                        transaction.Begin();
                        mq.Send(msg, transaction);
                        transaction.Commit();
                    }
                } else
                {
                    if( session.Transacted )
                    {
                        // We may want to raise an exception here since app requested
                        // a transeced NMS session, but is using a non transacted message queue
                        // For now silently ignore it.
                    }
                    mq.Send(msg);
                }
                
            } finally
            {
                // Cleanup
                if(transaction!=null)
                {
                    transaction.Dispose();
                }
                if (responseQueue != null)
                {
                    responseQueue.Dispose();
                }
                if( mq!=null && mq!=messageQueue )
                {
                    mq.Dispose();
                }
            }
        }
Beispiel #3
0
        public void Send(IDestination destination, IMessage imessage, bool persistent, byte priority, TimeSpan timeToLive)
        {
            BaseMessage             message       = (BaseMessage)imessage;
            MessageQueue            mq            = null;
            MessageQueue            responseQueue = null;
            MessageQueueTransaction transaction   = null;

            try
            {
                // Locate the MSMQ Queue we will be sending to
                if (messageQueue != null)
                {
                    if (destination.Equals(this.destination))
                    {
                        mq = messageQueue;
                    }
                    else
                    {
                        throw new NMSException("This producer can only be used to send to: " + destination);
                    }
                }
                else
                {
                    mq = openMessageQueue((Destination)destination);
                }

                // Convert the Mesasge into a MSMQ message
                message.NMSPersistent = persistent;
                message.NMSTimeToLive = timeToLive;
                message.NMSPriority   = priority;

                // message.NMSTimestamp = new DateTime().Date.;
                Message msg = messageConverter.ToMsmqMessage(message);
                // TODO: message.NMSMessageId =
                // Now Send the message
                if (mq.Transactional)
                {
                    if (session.Transacted)
                    {
                        mq.Send(msg, session.MessageQueueTransaction);
                    }
                    else
                    {
                        // Start our own mini transaction here to send the message.
                        transaction = new MessageQueueTransaction();
                        transaction.Begin();
                        mq.Send(msg, transaction);
                        transaction.Commit();
                    }
                }
                else
                {
                    if (session.Transacted)
                    {
                        // We may want to raise an exception here since app requested
                        // a transeced NMS session, but is using a non transacted message queue
                        // For now silently ignore it.
                    }
                    mq.Send(msg);
                }
            } finally
            {
                // Cleanup
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                if (responseQueue != null)
                {
                    responseQueue.Dispose();
                }
                if (mq != null && mq != messageQueue)
                {
                    mq.Dispose();
                }
            }
        }
Beispiel #4
0
        public void TestSendToTemporaryOnClosedSession()
        {
            const int NUM_MSGS  = 100;
            string    errString = null;
            const int TIMEOUT   = NUM_MSGS * 100;

            try
            {
                using (IConnection connection = GetConnection("c1"))
                    using (ISession tFactory = GetSession("tFactory"))
                        using (IMessageProducer producer = GetProducer("sender"))
                            using (IMessageConsumer consumer = GetConsumer("receiver"))
                            {
                                IDestination    destination = GetDestination("temp");
                                ITextMessage    sendMessage = producer.CreateTextMessage();
                                MessageListener ackCallback = CreateListener(NUM_MSGS);
                                MessageListener callback    = (message) =>
                                {
                                    if (errString == null)
                                    {
                                        if (!destination.Equals(message.NMSReplyTo))
                                        {
                                            errString = string.Format("Received message, id = {0}, has incorrect ReplyTo property.", ExtractMsgId(message.NMSMessageId));
                                            waiter.Set();
                                        }

                                        ackCallback(message);
                                    }
                                };
                                consumer.Listener            += callback;
                                connection.ExceptionListener += DefaultExceptionListener;

                                sendMessage.NMSReplyTo = destination;

                                connection.Start();

                                // close session
                                tFactory.Close();

                                for (int i = 0; i < NUM_MSGS; i++)
                                {
                                    sendMessage.Text = string.Format("Link:{0},count:{1}", "temp", i);
                                    producer.Send(sendMessage);

                                    sendMessage.ClearBody();
                                }

                                if (!waiter.WaitOne(TIMEOUT))
                                {
                                    if (errString == null)
                                    {
                                        Assert.Fail("Timed out waiting messages. Received, {0} of {1} messages in {2}ms.", msgCount, NUM_MSGS, TIMEOUT);
                                    }
                                    else
                                    {
                                        Assert.Fail(errString);
                                    }
                                }

                                Assert.AreEqual(NUM_MSGS, msgCount, "Did not receive expected number of messages.");
                            }
            }
            catch (Exception ex)
            {
                this.PrintTestFailureAndAssert(GetTestMethodName(), "Unexpected exception.", ex);
            }
        }
Beispiel #5
0
        public void TestTemporaryTopicReplyTo()
        {
            const int      NUM_MSGS        = 100;
            const string   MSG_BODY        = "num : ";
            IDestination   replyTo         = GetDestination("temp1");
            long           repliedCount    = 0;
            long           lastRepliedId   = -1;
            string         errString       = null;
            CountDownLatch replierFinished = new CountDownLatch(NUM_MSGS);


            using (IConnection connection = GetConnection("c1"))
                using (IMessageConsumer receiver = GetConsumer("receiver"))
                    using (IMessageConsumer listener = GetConsumer("listener"))
                        using (IMessageProducer sender = GetProducer("sender"))
                            using (IMessageProducer replyer = GetProducer("replyer"))
                            {
                                try
                                {
                                    connection.ExceptionListener += DefaultExceptionListener;
                                    ITextMessage rmsg    = null;
                                    ITextMessage sendMsg = sender.CreateTextMessage();
                                    sendMsg.NMSReplyTo = replyTo;

                                    listener.Listener += (message) =>
                                    {
                                        if (errString == null)
                                        {
                                            repliedCount++;
                                            long msgId = ExtractMsgId(message.NMSMessageId);
                                            if (msgId != lastRepliedId + 1)
                                            {
                                                // Test failed release blocked thread for shutdown.
                                                errString = String.Format("Received msg {0} out of order expected {1}", msgId, lastRepliedId + 1);
                                                waiter.Set();
                                            }
                                            else
                                            {
                                                lastRepliedId = msgId;
                                                if (msgId == NUM_MSGS - 1)
                                                {
                                                    message.Acknowledge();
                                                    // test done signal complete.
                                                    waiter.Set();
                                                    return;
                                                }
                                                message.Acknowledge();
                                            }
                                        }
                                    };

                                    receiver.Listener += (message) =>
                                    {
                                        if (errString == null)
                                        {
                                            msgCount++;
                                            rmsg = message as ITextMessage;
                                            if (rmsg == null)
                                            {
                                                // test failure
                                                errString = string.Format(
                                                    "Received message, id = {2}, body of type {0}, expected {1}.",
                                                    message.GetType().Name,
                                                    typeof(ITextMessage).Name,
                                                    ExtractMsgId(message.NMSMessageId)
                                                    );
                                                waiter.Set();
                                                return;
                                            }
                                            IDestination replyDestination = message.NMSReplyTo;
                                            if (!replyDestination.Equals(replyTo))
                                            {
                                                // test failure
                                                errString = string.Format(
                                                    "Received message, id = {0}, with incorrect reply Destination. Expected : {1}, Actual : {2}.",
                                                    ExtractMsgId(message.NMSMessageId),
                                                    replyTo,
                                                    replyDestination
                                                    );
                                                waiter.Set();
                                                return;
                                            }
                                            else
                                            {
                                                ITextMessage reply = replyer.CreateTextMessage();
                                                reply.Text = "Received:" + rmsg.Text;
                                                try
                                                {
                                                    replyer.Send(reply);
                                                    replierFinished.countDown();
                                                }
                                                catch (NMSException nEx)
                                                {
                                                    Logger.Error("Failed to send message from replyer Cause : " + nEx);
                                                    throw nEx;
                                                }
                                            }
                                        }
                                    };

                                    connection.Start();

                                    for (int i = 0; i < NUM_MSGS; i++)
                                    {
                                        sendMsg.Text = MSG_BODY + i;
                                        sender.Send(sendMsg);
                                    }

                                    // allow for two seconds for each message to be sent and replied to.
                                    int timeout = 2000 * NUM_MSGS;
                                    if (!waiter.WaitOne(timeout))
                                    {
                                        Assert.Fail("Timed out waiting on message delivery to complete. Received {1} of {0}, Replied {2} of {0}, Last Replied Msg Id {3}.", NUM_MSGS, msgCount, repliedCount, lastRepliedId);
                                    }
                                    else if (errString != null)
                                    {
                                        Assert.Fail("Asynchronous failure occurred. Cause : {0}", errString);
                                    }
                                    else
                                    {
                                        Assert.IsTrue(replierFinished.await(TimeSpan.FromMilliseconds(timeout)), "Replier thread has not finished sending messages. Remaining {0}", replierFinished.Remaining);
                                        Assert.IsNull(asyncEx, "Received Exception Asynchronously. Cause : {0}", asyncEx);
                                        Assert.AreEqual(NUM_MSGS, msgCount, "Failed to receive all messages.");
                                        Assert.AreEqual(NUM_MSGS, repliedCount, "Failed to reply to all messages");
                                        Assert.AreEqual(NUM_MSGS - 1, lastRepliedId, "Failed to receive the final message");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    this.PrintTestFailureAndAssert(GetTestMethodName(), "Unexpected exception.", ex);
                                }
                            }
        }
Beispiel #6
0
        public void Send(IDestination destination, IMessage imessage, MsgDeliveryMode deliveryMode, MsgPriority priority, TimeSpan timeToLive)
        {
            BaseMessage  message = (BaseMessage)imessage;
            MessageQueue mq      = null;

            try
            {
                // Locate the MSMQ Queue we will be sending to
                if (messageQueue != null)
                {
                    if (destination.Equals(this.destination))
                    {
                        mq = messageQueue;
                    }
                    else
                    {
                        throw new NMSException("This producer can only be used to send to: " + destination);
                    }
                }
                else
                {
                    mq = openMessageQueue((Destination)destination);
                }

                message.NMSDeliveryMode = deliveryMode;
                message.NMSTimeToLive   = timeToLive;
                message.NMSPriority     = priority;
                if (!DisableMessageTimestamp)
                {
                    message.NMSTimestamp = DateTime.UtcNow;
                }

                if (!DisableMessageID)
                {
                    // TODO: message.NMSMessageId =
                }

                // Convert the Mesasge into a MSMQ message
                Message msg = session.MessageConverter.ToMsmqMessage(message);

                if (mq.Transactional)
                {
                    if (session.Transacted)
                    {
                        mq.Send(msg, session.MessageQueueTransaction);
                    }
                    else
                    {
                        // Start our own mini transaction here to send the message.
                        using (MessageQueueTransaction transaction = new MessageQueueTransaction())
                        {
                            transaction.Begin();
                            mq.Send(msg, transaction);
                            transaction.Commit();
                        }
                    }
                }
                else
                {
                    if (session.Transacted)
                    {
                        // We may want to raise an exception here since app requested
                        // a transeced NMS session, but is using a non transacted message queue
                        // For now silently ignore it.
                    }
                    mq.Send(msg);
                }
            }
            finally
            {
                if (mq != null && mq != messageQueue)
                {
                    mq.Dispose();
                }
            }
        }