当前位置:编程学习 > 网站相关 >>

ActiveMQ消息收发简单例子

例子中使用的消息类型是:MapMessage。代码比较简单,帖出来给大家看看:
接收消息:
 private void Receive()
        {
            var factory = new ConnectionFactory(Program.BrokerUri);
            var timeout = new TimeSpan(0, 0, 10);
            using (var connection = factory.CreateConnection())
            {
                using (var session = connection.CreateSession())
                {
                    var destination = SessionUtil.GetDestination(session, Program.NormalQueueDestination);
                    using (var consumer = session.CreateConsumer(destination))
                    {
                        connection.Start();
                        var stopwatch = new Stopwatch();
                        var index = 1;
                        for (var i = 0; i < Program.MessageCount; i++)
                        {
                            if (index == 1)
                            {
                                stopwatch.Start();
                            }
 
                            try
                            {
                                var message = (IMapMessage)consumer.Receive(timeout);
                                index++;
                                var messageObj = Common.GetMessageObjByIMapMessage(message);
                                if (messageObj != null)
                                {
                                    //Debug.WriteLine(messageObj.MediaTaskId);
                                }
 
                                if (index == Program.StatisticsMessageCountSpan)
                                {
                                    stopwatch.Stop();
                                    var spendSeconds = stopwatch.Elapsed.TotalSeconds;
                                    var speed = Program.StatisticsMessageCountSpan / spendSeconds;
                                    Debug.WriteLine("Receive " + Program.StatisticsMessageCountSpan + " Messages Spend:" + spendSeconds + " Seconds. (" + speed.ToString("0.00") + "/s)");
 
                                    stopwatch.Reset();
                                    index = 1;
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex);
      &nbs

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,