繁体   English   中英

为什么我不能在 Windows 的 rabbitmq 中创建队列?

[英]Why I cannot create queue in rabbitmq in Windows?

我是 RabbitMQ 的新手。

我在 Windows 10 上安装了 RabbitMQ 服务器。我可以在 Web 浏览器中登录到服务器。 当我运行下面的客户端代码(使用 AMQP-CPP 库)时,不会调用channel.onSuccesschannel.onError 而且,我在 Web 浏览器中看不到我声明的my-queue队列和my-exchange交换。

如果我理解正确,我需要添加一些事件循环(?)。 但是,我找不到任何适用于 Windows 的示例。 你能解释一下可能是什么问题吗?

int main()
{
    // create an instance of your own tcp handler
    MyTcpHandler myHandler;

    // address of the server
    //AMQP::Address address("amqp://guest:guest@localhost:5672/");
    AMQP::Address address("localhost", 15672, AMQP::Login("guest", "guest"), "");

    // create a AMQP connection object
    AMQP::TcpConnection connection(&myHandler, address);

    // and create a channel b
    AMQP::TcpChannel channel(&connection);

    // use the channel object to call the AMQP method you like
    channel.declareExchange("my-exchange", AMQP::fanout)
        .onSuccess([]()
    {
        std::cout << "declared exchange " << std::endl;
    }).onError([](const char *message)
    {
        std::cout << "error: " << message << std::endl;

    });

    channel.declareQueue("my-queue");
    channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

    std::cout << "Press Enter..." << std::endl;
    std::getchar();

    return 0;
}

MyTcpHandler

class MyTcpHandler : public AMQP::TcpHandler
{
public:

    virtual void onConnected(AMQP::TcpConnection *connection) {}
    virtual void onError(AMQP::TcpConnection *connection, const char *message) {}
    virtual void onClosed(AMQP::TcpConnection *connection) {}
    virtual void monitor(AMQP::TcpConnection *connection, AMQP::tcp::Socket fd, int flags) {}
};

您在错误的端口上连接 15672 是管理插件端口,您需要在端口 5672 上连接,这是 AMQP 端口

请更正代码如下

AMQP::Address address("localhost", 5672, AMQP::Login("guest", "guest"), "");

您可以使用以下代码使用 HareDu 2 Broker API 来完成此操作。 文档可以在这里找到: https : //github.com/ahives/HareDu2

var result = _container.Resolve<IBrokerObjectFactory>()
                .Object<Queue>()
                .Create(x =>
                {
                    x.Queue("your_queue");
                    x.Configure(c =>
                    {
                        c.IsDurable();
                        c.AutoDeleteWhenNotInUse();
                        c.HasArguments(arg =>
                        {
                            arg.SetQueueExpiration(1000);
                            arg.SetPerQueuedMessageExpiration(2000);
                        });
                    });
                    x.Targeting(t =>
                    {
                        t.VirtualHost("your_vhost");
                        t.Node("your_node");
                    });
                });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM