Using C++ to write an image to a RabbitMQ queue

杀马特。学长 韩版系。学妹 提交于 2019-12-24 08:55:45

问题


I'm using SimpleAmqpClient which is a C++ library to use with a RabbitMQ broker. I can send and receive string i.e. "hello world". Here is the program that does that.

#include <stdlib.h>
#include <stdio.h>
#include <SimpleAmqpClient/SimpleAmqpClient.h>
#include <iostream>
#include "SimplePublisher.h"

using namespace AmqpClient;
using namespace std;
int main()
{
    char *szBroker = getenv("AMQP_BROKER");
    Channel::ptr_t channel;
    if (szBroker != NULL)
        channel = Channel::Create(szBroker);
    else
        channel = Channel::Create("192.168.66.1", 5672);

    string a="hello world";

   // SimplePublisher pub(channel);
    boost::shared_ptr<SimplePublisher> pub=SimplePublisher::Create(channel, "wt");
        pub->Publish(a);
}

It calls the first one of these functions which takes a string.

void SimplePublisher::Publish(const std::string &message)
{
    BasicMessage::ptr_t outgoing_message = BasicMessage::Create();
    outgoing_message->Body(message);

    Publish(outgoing_message);
}

void SimplePublisher::Publish(const BasicMessage::ptr_t message)
{
    m_channel->BasicPublish(m_publisherExchange, "", message);
}

I want to write a JPEG image to the queue which is not a string.

Could anybody comment on how I would do this?


回答1:


You have two options.

  1. Serialize the image bytes to a Base-64 encoded string

  2. Publish the image as a byte array directly.

It should be noted that RabbitMQ works best when operating on very small (<25kB) messages. If your images are of any considerable size (e.g. any larger than this), then you may have performance issues with the broker if your volume of messages is large. In that case, it would be best to set up an alternate stream for large files NOT involving the message broker.



来源:https://stackoverflow.com/questions/50274046/using-c-to-write-an-image-to-a-rabbitmq-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!