代码之家  ›  专栏  ›  技术社区  ›  BackSlash

Rabbit为AMQP 1.0消息保存额外字节

  •  1
  • BackSlash  · 技术社区  · 6 年前

    我使用 rhea (类型脚本):

    const connection: Connection = new Connection (
      {
        host: 'localhost',
        port: 5672,
        id: 'my_id',
        reconnect: true
      }
    );
    
    const senderName = "sender01";
    const senderOptions: SenderOptions = {
      name: senderName,
      target: {
        address: "target.queue"
      },
      onError: (context: EventContext) => {},
      onSessionError: (context: EventContext) => {}
    };
    
    await connection.open();
    const sender: Sender = await connection.createSender(senderOptions);
    sender.send({
      body: JSON.stringify({"one": "two", "three": "four"}),
      content_encoding: 'UTF-8',
      content_type: 'application/json'
    });
    console.log("sent");
    await sender.close();
    
    await connection.close();
    console.log("connection closed");
    
    

    此示例有效,但这是存储在队列中的内容:

    enter image description here

    base64编码的消息是 AFN3oRx7Im9uZSI6InR3byIsInRocmVlIjoiZm91ciJ9 ,解码后变成:

    Sw{"one":"two","three":"four"}
    

    Sw

    我试着用官方的RabbitMQ库(AMQP 0.9.1)设置一个java客户机,看看这些额外的字节是否被发送到了客户机:

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.basicConsume(
      "target.queue",
      true,
      (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      },
      ignored -> {}
    );
    

    这是输出:

     [x] Received ' Sw�{"one":"two","three":"four"}'
    

    奇怪的是,如果我尝试在AMQP 1.0客户端上使用完全相同的消息,那么这些额外的字节不会出现在接收的消息正文中,只有在使用amqp1.0发布和使用AMQP 0.9.1订阅时才会出现额外的字节。


    更新

    我还尝试了使用SwiftMQ:

    int nMsgs = 100;
    
    int qos = QoS.AT_MOST_ONCE;
    AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
    String host = "localhost";
    int port = 5672;
    String queue = "target.queue";
    
    try {
    
      Connection connection = new Connection(ctx, host, port, false);
      connection.setContainerId(UUID.randomUUID().toString());
      connection.setIdleTimeout(-1);
      connection.setMaxFrameSize(1024 * 4);
      connection.setExceptionListener(Exception::printStackTrace);
      connection.connect();
      {
    
        Session session = connection.createSession(10, 10);
        Producer p = session.createProducer(queue, qos);
        for (int i = 0; i < nMsgs; i++) {
          AMQPMessage msg = new AMQPMessage();
          System.out.println("Sending " + i);
          msg.setAmqpValue(new AmqpValue(new AMQPString("{\"one\":\"two\",\"three\":\"four\"}")));
          p.send(msg);
        }
        p.close();
        session.close();
      }
      connection.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    

    [x] Received '□�□□□□□□□w�{"one":"two","three":"four"}'
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Luke Bakken    6 年前

    请看 this response

    如果AMQP 1.0客户机向0-9-1客户机发送消息,并在“数据部分”(即不在AMQP序列部分,不在AMQP值部分)中将其负载编码为二进制,则0-9-1客户机应获得完整的负载,而无需任何额外字节


    注: RabbitMQ团队监控 rabbitmq-users mailing list