使用用户名和密码连接到 Amazon ActiveMQ

Connecting to Amazon ActiveMQ with username & password

我正在尝试通过 SSL 连接到 ActiveMQ Amazon 代理。我的应用程序是用 C# 编写的。 以前,我通过 TCP 连接到本地主机 ActiveMQ 代理,它有效:

using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;


void Connect()
{       
    String brokerUri = "activemq:tcp://" + host + ":" + port + "? transport.useLogging=true&wireFormat.maxInactivityDuration=0&userName=myUsername&password=myPassword";
    NMSConnectionFactory factory = new NMSConnectionFactory(brokerUri);
    IConnection connection = factory.CreateConnection();
    connection.Start();
}

为了通过 SSL 连接到 ActiveMQ Amazon 代理,我按以下方式修改了代码:

void Connect()
{
    String brokerUri = "ssl://" + host + ":" + port + "? transport.useLogging=true&wireFormat.maxInactivityDuration=0&userName=myUsername&password=myPassword";
    SslTransportFactory transportFactory = new SslTransportFactory();
    Uri uri = new Uri(brokerUri);
    ITransport transport = transportFactory.CreateTransport(uri);
    transport.Command = Command;
    transport.Exception = Exception;
    transport.Start();

    ConnectionFactory factory = new ConnectionFactory(brokerUri);
    IConnection connection = factory.CreateConnection();

    Connection con = (Connection)connection;
    con.ITransport = transport;

    con.Start();                  => Here the exception is thrown: User name [null] or password is invalid. 
}

private void Exception(ITransport sender, Exception command)
{            
}

private void Command(ITransport sender, Command command)
{            
}

但是,在启动连接时,User name [null] or password is invalid. 抛出异常。

请指教

您目前正在通过 URI 传递用户凭据。但是,我在 URI reference documentation 中没有看到任何对 usernamepassword 的引用。此外,异常指示用户名是 null 的事实表明您的 URI 中的用户凭据只是被忽略了。

相反,您应该按照记录 hereCreateConnection 方法中传递用户凭据,例如:

IConnection connection = factory.CreateConnection("myUsername", "myPassword");