七、RabbitMQ-客户端源码之AMQPImpl+Method
AMQPImpl类包括AMQP接口(public class AMQImpl implements AMQP)主要囊括了AMQP协议中的通信帧的类别。
这里以Connection.Start帧做一个例子。
public static class Connection {
public static final int INDEX = 10;
public static class Start
extends Method
implements com.rabbitmq.client.AMQP.Connection.Start
{
public static final int INDEX = 10;
private final int versionMajor;
private final int versionMinor;
private final Map
serverProperties;
private final LongString mechanisms;
private final LongString locales;
....//下面省略很多代码。。。
可以看到Start类是Connection类的内部静态子类,表示此Start类为Connection.Start,而且Start类是继承Method方法的,包括接下来所有的AMQP协议帧都是继承这个Method方法,Method可以看成用来区分AMQP协议帧的类型。
Method类是一个抽象类(Base class for AMQP method objects, specialized by autogenerated code in AMQP.java),我们来看下Method类的代码:
public abstract class Method implements com.rabbitmq.client.Method {
/** {@inheritDoc} */
public abstract int protocolClassId(); /* properly an unsigned short */
/** {@inheritDoc} */
public abstract int protocolMethodId(); /* properly an unsigned short */
/** {@inheritDoc} */
public abstract String protocolMethodName();
/**
* Tell if content is present.
* @return true if the wire-protocol for this method should involve a content header and body,
* or false if it should just involve a single method frame.
*/
public abstract boolean hasContent();
/**
* Visitor support (double-dispatch mechanism).
* @param visitor the visitor object
* @return the result of the "visit" operation
* @throws IOException if an error is encountered
*/
public abstract Object visit(MethodVisitor visitor) throws IOException;
/**
* Private API - Autogenerated writer for this method.
* @param writer interface to an object to write the method arguments
* @throws IOException if an error is encountered
*/
public abstract void writeArgumentsTo(MethodArgumentWriter writer) throws IOException;
/**
* Public API - debugging utility
* @param buffer the buffer to append debug data to
*/
public void appendArgumentDebugStringTo(StringBuilder buffer) {
buffer.append("(?)");
}
@Override public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("#method<").append(protocolMethodName()).append(">");
this.appendArgumentDebugStringTo(sb);
return sb.toString();
}
public Frame toFrame(int channelNumber) throws IOException {
Frame frame = new Frame(AMQP.FRAME_METHOD, channelNumber);
DataOutputStream bodyOut = frame.getOutputStream();
bodyOut.writeShort(protocolClassId());
bodyOut.writeShort(protocolMethodId());
MethodArgumentWriter argWriter = new MethodArgumentWriter(new ValueWriter(bodyOut));
writeArgumentsTo(argWriter);
argWriter.flush();
return frame;
}