首页 新闻 赞助 找找看

利用AMQP-CPP访问RabbitMq,消息不能超过10K否则consume不到

0
悬赏园豆:5 [已关闭问题] 关闭于 2017-01-23 23:21

client代码如下:

#include <iostream>

#include "tools.h"
#include "asiohandler.h"
struct FibS
{
    int a;
    char b[1024*10];
};
int main(int argc, const char* argv[])
{
    const std::string correlation(uuid());

    boost::asio::io_service ioService;
    AsioHandler handler(ioService);
    handler.connect("168.1.1.50", 5672);

    AMQP::Connection connection(&handler, AMQP::Login("epri", "123456"), "/");

    AMQP::Channel channel(&connection);
    AMQP::QueueCallback callback = [&](const std::string &name,
            int msgcount,
            int consumercount)
    {
        FibS s;
        s.a = 8;
        std::string m = "Hello, I am a big object from client.";
        strcpy(s.b, m.c_str());

        char* buf;
        unsigned int buf_size;
        buf = (char*)malloc(sizeof(FibS));
        memcpy(buf, (char*)&s, sizeof(s));
        buf_size = sizeof(s);

        AMQP::Envelope env((const char*)buf,buf_size);
        env.setCorrelationID(correlation);
        env.setReplyTo(name);
        channel.publish("","rpc_queue",env);
        std::cout<<" [x] Requesting fib(8)"<<std::endl;

    };
    channel.declareQueue(AMQP::exclusive).onSuccess(callback);

    boost::asio::deadline_timer t(ioService, boost::posix_time::millisec(10000));

    auto receiveCallback = [&](const AMQP::Message &message,
            uint64_t deliveryTag,
            bool redelivered)
    {
        std::cout << " [x] Got a reply." << std::endl;
        if(message.correlationID() != correlation)
            return;

        FibS s;
        memcpy(&s,message.body(),message.bodySize());
        std::cout<<" [.] Got "<<s.a<<":"<<s.b<<std::endl;
        t.async_wait([&](const boost::system::error_code&){ioService.stop();});
    };

    channel.consume("", AMQP::noack).onReceived(receiveCallback);

    ioService.run();
    return 0;
}

server代码如下:

#include <iostream>
#include <algorithm>
#include <thread>
#include <chrono>

#include "asiohandler.h"
using namespace std;
int fib(int n)
{
    switch (n)
    {
    case 0:
        return 0;
    case 1:
        return 1;
    default:
        return fib(n - 1) + fib(n - 2);
    }
}
struct FibS
{
    int a;
    char b[1024*10];
};
int main(void)
{
    boost::asio::io_service ioService;
    AsioHandler handler(ioService);
    handler.connect("168.1.1.50", 5672);
    AMQP::Connection connection(&handler, AMQP::Login("epri", "123456"), "/");
    AMQP::Channel channel(&connection);
    channel.setQos(1);

    channel.declareQueue("rpc_queue",AMQP::autodelete);
    channel.consume("").onReceived([&channel](const AMQP::Message &message,
            uint64_t deliveryTag,
            bool redelivered)
    {

        std::cout << " [x] Got a message." << std::endl;



        if (message.bodySize() == sizeof(FibS))
        {
            FibS *f = (FibS*)message.body();
            std::cout<<" [.] Got "<<f->a<<":"<<f->b<<std::endl;

            int sum = fib(f->a);

            FibS s;
            s.a = sum;
            std::string name = "Hello, I am a big object from server.";
            strcpy(s.b, name.c_str());

            char* buf;
            unsigned int buf_size;
            buf = (char*)malloc(sizeof(FibS));
            memcpy(buf, (char*)&s, sizeof(s));
            buf_size = sizeof(s);


            AMQP::Envelope env((const char*)buf,buf_size);
            env.setCorrelationID(message.correlationID());

            channel.publish("", message.replyTo(), env);
            channel.ack(deliveryTag);
        }
        else
        {
            std::cout << " [x] Got a error message." << std::endl;
        }
    });

    std::cout << " [x] Awaiting RPC requests" << std::endl;

    ioService.run();
    return 0;
}

当FibS的大小较大的时候,不如1024*10,消息就发送不成功,客户端和服务端都获取不到消息,有熟悉AMQP-CPP的同伴请指教,回头如果找到了答案,我也会贴在下面。

xpwilson的主页 xpwilson | 初学一级 | 园豆:124
提问于:2016-05-25 21:42
< >
分享
所有回答(1)
0

楼主可以看看你的amqp的封装吗

Alias2015 | 园豆:202 (菜鸟二级) | 2017-01-16 23:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册