连接到AWS上设置的ActiveMQ后几秒钟后连接关闭

时间:2018-05-31 20:25:14

标签: c# activemq nms

我正在连接到AWS上托管的Apache Active MQ,以将我的应用程序集成到自定义服务中。我需要始终保持这种运行,而不是像现在这样。下面的代码有效,但只有一条消息,我需要一直保持连接处于活动状态,以便接收所有消息。 这是代码。

    using Apache.NMS;
    using Apache.NMS.Util;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

namespace ApacheMQAsync
{
    class Program
    {

        protected static ITextMessage message = null;


        public static void Main(string[] args)
        {

            Uri connecturi = new Uri("URL:61617");

            Console.WriteLine("About to connect to " + connecturi);

            // NOTE: ensure the nmsprovider-activemq.config file exists in the executable folder.
            IConnectionFactory factory = new Apache.NMS.ActiveMQ.ConnectionFactory(connecturi);

            IConnection connection = factory.CreateConnection("username", "password");

            ISession session = connection.CreateSession();
            IDestination destination = SessionUtil.GetDestination(session, "queue://FOO.BAR");

            Console.WriteLine("Using destination: " + destination);

            // Create a consumer and producer
            IMessageConsumer consumer = session.CreateConsumer(destination);

            consumer.Listener += new MessageListener(OnMessage);
            connection.Start();
            // Wait for the message


            if (message == null)
            {
                Console.WriteLine("No message received!");
            }
            else
            {
                Console.WriteLine("Received message with ID:   " + message.NMSMessageId);
                Console.WriteLine("Received message with text: " + message.Text);
            }
        }

        protected static void OnMessage(IMessage receivedMsg)
        {
            message = receivedMsg as ITextMessage;
            message.Acknowledge();

        }
    }
}

在控制台上显示以下内容 没有收到消息! 几秒后控制台就存在了?

1 个答案:

答案 0 :(得分:1)

那里没有真正的魔力,您需要做一些事情来保持您的应用程序运行,例如暂停控制台输入或循环睡眠或其他等待类型调用,然后检查某些内容以查看您的应用程序是否应该继续。 JMS客户端不保证您的应用程序保持打开和运行,您永远不应该依赖它。