RabbitMQクライアントのソースコードシリーズ-メッセージを磨く

序文

今回は引き続き共有する予定です-メッセージを送信します。最初に、 RabbitMQクライアントのソースコード-接続RabbitMQクライアントのソースコード-チャネル以前に共有したもの(以前のものに基づいていますRabbitMQ Client pulishの概要を説明します。发布消息 - Pulish MessageJava Client Connecting to RabbitMQ Demo

RabbitMQpulisherの概要

この図から、RabbitMQがメッセージプロセスを公開していることがわかります。ConnectionFactory --> Connection --> Channel --> Pulish Message

ポリッシュメッセージインタラクティブパケットキャプチャ

古いルーチン-ソースコードを共有する前にパッケージをキャプチャします(すばやく理解するのに役立ちます

基本的な公開と確認

パケットキャプチャからわかるように、pulisher(RabbitMQメッセージ送信者)とBroker(RabbitMQ Broker)がChannel開かれた後、それらは再び開始しConfirm.Select/Select-Okます-公開されたメッセージの受信を確認する必要があることをブローカーに通知するため、ファローアップBasic.Pulish/Ack

インタラクションプロセスを整理した後、今日のテーマを入力し始めますPulish Message

ピュリッシュメッセージのソースコードの分析

メッセージの公開一般エントリ-ChannelN.basicPublish()

/** Public API - {@inheritDoc} */
@Override
public void basicPublish(String exchange, String routingKey,
                         boolean mandatory,
                         BasicProperties props, byte[] body)
  throws IOException
{
  basicPublish(exchange, routingKey, mandatory, false, props, body);
}


/** Public API - {@inheritDoc} */
@Override
public void basicPublish(String exchange, String routingKey,
                         boolean mandatory, boolean immediate,
                         BasicProperties props, byte[] body)
  throws IOException
{
  // Pulisher 配置了 `Confirm.Select` nextPublishSeqNo 设置从 1 开始
  // 将未确认的消息 放入 unconfirmedSet,并 自增加一
  if (nextPublishSeqNo > 0) {
    unconfirmedSet.add(getNextPublishSeqNo());
    nextPublishSeqNo++;
  }
  BasicProperties useProps = props;
  if (props == null) {
    useProps = MessageProperties.MINIMAL_BASIC;
  }
  // 构造 AMQCommand 并传输
  transmit(new AMQCommand(new Basic.Publish.Builder()
                          .exchange(exchange)
                          .routingKey(routingKey)
                          .mandatory(mandatory)
                          .immediate(immediate)
                          .build(),
                          useProps, body));
  // 用于指标统计和监控,默认是 NoOpMetricsCollector,需要配置才会可以使用 提供的 MicrometerMetricsCollector 和 StandardMetricsCollector(引入对应的包和配置 开箱即可食用~)
  metricsCollector.basicPublish(this);
}
复制代码

AMQCommandを構築する

RabbitMQクライアントアプリケーションメッセージの最小単位はFrame(フレーム、接続の章で説明)であり、フレームは主type 类型、channel 通道、payload 消息内容字节、accumulator 写出数据、NON_BODY_SIZEにいることに注意してください。

フレーム構造

public class Frame {
    /** Frame type code */
  	// FRAME_HEARTBEAT :心跳, FRAME_METHOD: 方法, FRAME_HEADER : 头部信息, FRAME_BODY 内容主题
    public final int type;

    /** Frame channel number, 0-65535 */
  	// channel 序列号
    public final int channel;

    /** Frame payload bytes (for inbound frames) */
  	// 消息内容字节
    private final byte[] payload;

    /** Frame payload (for outbound frames) */
    // 写出数据
    private final ByteArrayOutputStream accumulator;

    private static final int NON_BODY_SIZE = 1 /* type */ + 2 /* channel */ + 4 /* payload size */ + 1 /* end character */;
 
  ...
  
}
复制代码

AMQP 0-9-1固有のコマンド読み取り、一連のフレームから累積方法、头部和正文

/**
* Construct a command with a specified method, header and body.
* @param method the wrapped method
* @param contentHeader the wrapped content header
* @param body the message body data
*/
public AMQCommand(com.rabbitmq.client.Method method, AMQContentHeader contentHeader, byte[] body) {
	this.assembler = new CommandAssembler((Method) method, contentHeader, body);
}

// AMQP 0-9-1 特定的Command,构造 方法、头部和正文
public CommandAssembler(Method method, AMQContentHeader contentHeader, byte[] body) {
  this.method = method;
  this.contentHeader = contentHeader;
  this.bodyN = new ArrayList<byte[]>(2);
  this.bodyLength = 0;
  this.remainingBodyBytes = 0;
  appendBodyFragment(body);
  if (method == null) {
    this.state = CAState.EXPECTING_METHOD;
  } else if (contentHeader == null) {
    this.state = method.hasContent() ? CAState.EXPECTING_CONTENT_HEADER : CAState.COMPLETE;
  } else {
    this.remainingBodyBytes = contentHeader.getBodySize() - this.bodyLength;
    updateContentBodyState();
  }
}
复制代码

AMQCommandの送信--Channel.transmit()

public void transmit(AMQCommand c) throws IOException {
  synchronized (_channelMutex) {
    // 确认 channel 是否打开(逻辑比较简单:判断 shutdownCause 为空即是打开)
    ensureIsOpen();
    quiescingTransmit(c);
  }
}

public void quiescingTransmit(AMQCommand c) throws IOException {
  // 防止并发同时使用 同一个channel
  synchronized (_channelMutex) {
    // 判断 该消息是否 携带content,如果有 需要判断该 channel是否是阻塞(如果channel state为 `FLOW` 即为 阻塞 _blockContent = true)
    if (c.getMethod().hasContent()) {
      while (_blockContent) {
        try {
          _channelMutex.wait();
        } catch (InterruptedException ignored) {}

        // 防止 从阻塞中被唤醒时,channel 已经关闭(挺好的一个 多线程操作的案例)
        ensureIsOpen();
      }
    }
    c.transmit(this);
  }
}
复制代码

AMQCommand.transmit

/**
 * Sends this command down the named channel on the channel's
 * connection, possibly in multiple frames.
 * @param channel the channel on which to transmit the command
 * @throws IOException if an error is encountered
 */
public void transmit(AMQChannel channel) throws IOException {
  // 每个 channel 都有序列号 从 0开始,(0是特殊的channel)
  int channelNumber = channel.getChannelNumber();
  AMQConnection connection = channel.getConnection();

  synchronized (assembler) {
    // 方法:FRAME_HEARTBEAT :心跳, FRAME_METHOD: 方法, FRAME_HEADER : 头部信息, FRAME_BODY 内容主题
    Method m = this.assembler.getMethod();
    if (m.hasContent()) {
      byte[] body = this.assembler.getContentBody();

      // FRAME_HEADER : 头部信息
      Frame headerFrame = this.assembler.getContentHeader().toFrame(channelNumber, body.length);

      int frameMax = connection.getFrameMax();
      boolean cappedFrameMax = frameMax > 0;
      int bodyPayloadMax = cappedFrameMax ? frameMax - EMPTY_FRAME_SIZE : body.length;

      if (cappedFrameMax && headerFrame.size() > frameMax) {
        String msg = String.format("Content headers exceeded max frame size: %d > %d", headerFrame.size(), frameMax);
        throw new IllegalArgumentException(msg);
      }
      // 1. 写 channelNumber帧  FRAME_METHOD
      connection.writeFrame(m.toFrame(channelNumber));
      // 2. 写 头部信息帧   AMQP.FRAME_HEADER
      connection.writeFrame(headerFrame);

      // 3. 如果 body过多,会拆成多个帧  AMQP.FRAME_BODY
      for (int offset = 0; offset < body.length; offset += bodyPayloadMax) {
        int remaining = body.length - offset;

        int fragmentLength = (remaining < bodyPayloadMax) ? remaining
          : bodyPayloadMax;
        Frame frame = Frame.fromBodyFragment(channelNumber, body,
                                             offset, fragmentLength);
        connection.writeFrame(frame);
      }
    } else {
      // 1. 写 channelNumber帧  FRAME_METHOD
      connection.writeFrame(m.toFrame(channelNumber));
    }
  }
	// 最后刷新 输出缓冲区
  connection.flush();
}
复制代码

最後に、connection.writeFrame(frame)を分析します

/**
 * Public API - sends a frame directly to the broker.
 */
public void writeFrame(Frame f) throws IOException {
  _frameHandler.writeFrame(f);
  // lastActivityTime
  _heartbeatSender.signalActivity();
}

@Override
public void writeFrame(Frame frame) throws IOException {
  synchronized (_outputStream) {
    frame.writeTo(_outputStream);
  }
}

/**
 * Public API - writes this Frame to the given DataOutputStream
 */
public void writeTo(DataOutputStream os) throws IOException {
  // 1. 写type 类型
  os.writeByte(type);
  // 2. 写channel 序列号
  os.writeShort(channel);
  if (accumulator != null) {
    // 3. 写出数据大小
    os.writeInt(accumulator.size());
    // 4. 输出数据
    accumulator.writeTo(os);
  } else {
    // 3. 写消息内容字节大小
    os.writeInt(payload.length);
    // 4. 写消息内容
    os.write(payload);
  }
  // 5. 帧结束标志位
  os.write(AMQP.FRAME_END);
}
复制代码

最後に

AMQPプロトコルを明確に理解し、それに従ってソースコードを理解するのに役立つRabbitMQ Clientことを願っています。もちろん、読者がゆっくりと味わう必要のあるソースコードの詳細はたくさんあります〜RabbitMQ Broker发布消息ChannelN.basicPublish


私のWeChatパブリックアカウント:Java Architect Advanced Programming
Javaテクノロジーの乾物の共有に焦点を当て、あなたの注意を楽しみにしています!

おすすめ

転載: juejin.im/post/7083484960375439368
おすすめ