You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C++ SimpleAmqpClient向RabbitMQ发送字节数组

Got it, let's walk through how to send a byte array instead of a plain string with SimpleAmqpClient. Here's a complete, drop-in replacement for your Hello World example that handles binary data properly:

#include <SimpleAmqpClient/SimpleAmqpClient.h>
#include <vector>
#include <cstdint>

int main(int argc, char** argv) {
    // Setup your channel (same as your original code, adjust connection details as needed)
    auto channel = SimpleAmqpClient::Channel::Create("amqp://guest:guest@localhost:5672/");
    
    // Example byte array (this could be any binary data: images, serialized objects, etc.)
    std::vector<uint8_t> binary_data = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xAA, 0xFF};
    
    // Wrap our byte array in the amqp_bytes_t_ struct required by the library
    amqp_bytes_t_ amqp_body;
    amqp_body.bytes = binary_data.data(); // Pointer to our binary data
    amqp_body.len = binary_data.size();   // Length of the byte array
    
    // Optional: Initialize message properties (skip if you don't need custom props)
    amqp_basic_properties_t_ message_props;
    amqp_basic_properties_init(&message_props);
    // If you want to set a content type for binary data, uncomment this:
    // message_props.content_type = amqp_cstring_bytes("application/octet-stream");
    
    // Create the BasicMessage using the byte array-specific Create() overload
    auto outgoing_msg = SimpleAmqpClient::BasicMessage::Create(amqp_body, &message_props);
    
    // Create your publisher and send the message (same as your original flow)
    auto publisher = SimpleAmqpClient::SimplePublisher::Create(channel, "wt");
    publisher->Publish(outgoing_msg);
    
    // Clean up the properties struct to avoid memory leaks in the underlying C library
    amqp_basic_properties_destroy(channel->GetConnection()->GetInternalConnection(), &message_props);
    
    return 0;
}

Important Notes:

  • Byte Array Storage: I used std::vector<uint8_t> here because it manages memory safely, but you could also use a raw byte array (just make sure it stays in scope while the message is being sent).
  • amqp_bytes_t_ Setup: This struct is just a wrapper for your raw bytes—we point it to the start of our binary data and set the length. No need to copy the bytes here (the library handles that internally if needed).
  • Properties Handling: If you don't need custom message properties (like content type, headers, etc.), you can pass nullptr instead of &message_props to BasicMessage::Create(). If you do use properties, always remember to initialize them with amqp_basic_properties_init() and clean them up with amqp_basic_properties_destroy() to prevent leaks.
  • Underlying Library Integration: This uses the exact Create(amqp_bytes_t_ &body, amqp_basic_properties_t_ *properties) overload you referenced, which hooks directly into the underlying RabbitMQ C client's structs as expected.

内容的提问来源于stack exchange,提问作者arcoxia tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:17:47