在连接到AWS上设置的ActiveMQ时,连接会在几秒钟后关闭 [英] Connection closes after few seconds when connecting to ActiveMQ set up on AWS

查看:301
本文介绍了在连接到AWS上设置的ActiveMQ时,连接会在几秒钟后关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am connecting to Apache Active MQ which is hosted on AWS to integrate my app to a custom service. I need to keep this running always, not one time like it's right now. The code below works, but only for one message, I need to maintain the connection active all the time listening in order to receive all the messages. Here is the code.

    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();

        }
    }
}

控制台,它将显示以下
信息。
,几秒钟后控制台就存在了?

On the console it displays following No message received! and after few seconds the console exist?

推荐答案

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

There's no real magic there, you need to do something to keep your application running such as pausing on console input or looping on a sleep or other wait type call and then checking something to see if your application should continue. The JMS client isn't guaranteed to keep your application open and running and you should never rely on it to.

这篇关于在连接到AWS上设置的ActiveMQ时,连接会在几秒钟后关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆