简体   繁体   English

使用C ++将图像写入RabbitMQ队列

[英]Using C++ to write an image to a RabbitMQ queue

I'm using SimpleAmqpClient which is a C++ library to use with a RabbitMQ broker. 我正在使用SimpleAmqpClient ,它是一个与RabbitMQ代理一起使用的C ++库。 I can send and receive string ie "hello world". 我可以发送和接收字符串,即“ 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. 我想将JPEG图像写入不是字符串的队列。

Could anybody comment on how I would do this? 有人可以评论我该怎么做吗?

You have two options. 您有两个选择。

  1. Serialize the image bytes to a Base-64 encoded string 将图像字节序列化为Base-64编码的字符串

  2. Publish the image as a byte array directly. 直接将图像发布为字节数组。

It should be noted that RabbitMQ works best when operating on very small (<25kB) messages. 应该注意的是,RabbitMQ在处理非常小的(<25kB)消息时效果最佳。 If your images are of any considerable size (eg 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. 在这种情况下,最好为不涉及消息代理的大文件设置备用流。

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

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