[七]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<String,Object> 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;
    }
}

代码不长。挑几个解释下。

protocolClassId()和protocolMethodId():每一个Method(Connection.Start/.StartOk, Connection.Tune/.TuneOk等等)都包含classId和methodId,可以参考下图:

protocolMethodName()返回本Method的名称,比如Connection.Start的就是:

public String protocolMethodName() { return "connection.start";}

boolean hasContent()用来区分这个Method之后是否有Content-Body,比如Connection.Start的为:

public boolean hasContent() { return false; }

又比如Basic.Publish的为:

public boolean hasContent() { return true; }


好了,这里可以回来接着讲AMQPImpl了。

下面是一张表,用来涵盖AQMP协议各个种类的Method以及其一些属性,看完这张表就看完了AMQPImpl的全部。

Method-Name classId methodId hasContent
Connection.Start 10 10 false
Connection.StartOk 10 11 false
Connection.Secure 10 20 false
Connection.SecureOk 10 21 false
Connection.Tune 10 30 false
Connection.TuneOk 10 31 false
Connection.Open 10 40 false
Connection.OpenOk 10 41 false
Connection.Close 10 50 false
Connection.CloseOk 10 51 false
Connection.Blocked 10 60 false
Connection.Unblocked 10 61 false
Channel.Open 20 10 false
Channel.OpenOk 20 11 false
Channel.Flow 20 20 false
Channel.FlowOk 20 21 false
Channel.Close 20 40 false
Channel.CloseOk 20 41 false
Access.Request 30 10 false
Access.RequestOk 30 11 false
Exchange.Declare 40 10 false
Exchange.DeclareOk 40 11 false
Exchange.Delete 40 20 false
Exchange.DeleteOk 40 21 false
Exchange.Bind 40 30 false
Exchange.BindOk 40 31 false
Exchange.Unbind 40 40 false
Exchange.UnbindOk 40 51 false
Queue.Declare 50 10 false
Queue.DeclareOk 50 11 false
Queue.Bind 50 20 false
Queue.BindOk 50 21 false
Queue.Purge 50 30 false
Queue.PurgeOk 50 31 false
Queue.Delete 50 40 false
Queue.DeleteOk 50 41 false
Queue.Unbind 50 50 false
Queue.UnbindOk 50 51 false
Basic.Qos 60 10 false
Basic.QosOk 60 11 false
Basic.Consume 60 20 false
Basic.ConsumeOk 60 21 false
Basic.Cancel 60 30 false
Basic.CancelOk 60 31 false
Basic.Publish 60 40 true
Basic.Return 60 50 true
Basic.Deliver 60 60 true
Basic.Get 60 70 false
Basic.GetOk 60 71 true
Basic.GetEmpty 60 72 false
Basic.Ack 60 80 false
Basic.Reject 60 90 false
Basic.RecoverAsync 60 100 false
Basic.Recover 60 110 false
Basic.RecoverOk 60 111 false
Basic.Nack 60 120 false
Tx.Select 90 10 false
Tx.SelectOk 90 11 false
Tx.Commit 90 20 false
Tx.CommitOk 90 21 false
Tx.Rollback 90 30 false
Tx.RollbackOk 90 31 false
Confirm.Select 85 10 false
Confirm.SelectOk 85 11 false

附:本系列全集

  1. [Conclusion]RabbitMQ-客户端源码之总结
  2. [一]RabbitMQ-客户端源码之ConnectionFactory
  3. [二]RabbitMQ-客户端源码之AMQConnection
  4. [三]RabbitMQ-客户端源码之ChannelManager
  5. [四]RabbitMQ-客户端源码之Frame
  6. [五]RabbitMQ-客户端源码之AMQChannel
  7. [六]RabbitMQ-客户端源码之AMQCommand
  8. [七]RabbitMQ-客户端源码之AMQPImpl+Method
  9. [八]RabbitMQ-客户端源码之ChannelN
  10. [九]RabbitMQ-客户端源码之Consumer
时间: 2024-10-29 02:19:52

[七]RabbitMQ-客户端源码之AMQPImpl+Method的相关文章

memcached客户端源码分析

转载:memcached客户端源码分析 memcached的Java客户端有好几种,http://code.google.com/p/memcached/wiki/Clients 罗列了以下几种 Html代码   spymemcached          * http://www.couchbase.org/code/couchbase/java             o An improved Java API maintained by Matt Ingenthron and other

急求百度手环手机客户端源码

问题描述 急求百度手环手机客户端源码 50C dulife手环的安卓手机客户端的源码或者手机端测试的源码百度手环Android手机客户端源码 解决方案 这种源码怎么可能给你,都是商业机密.去研究研究原理.技术还是正途.

机器人操作系统ROS教程(七) smartcar源码上传

看到前面写的博客还是帮助了很多ROS的学习者,我感到非常荣幸.其实我也是一名ROS的新手,ROS的相关 资料少,上手难度大,我现在也在摸索着学习,还希望大家都将自己的学习成果在网上或者ROS群里分享. 我看到有些人在运行我前面写的smartcar程序,为了方便大家的学习,我这两天整理了一下代码,已 经上传到csdn上,下载请见: http://download.csdn.net/detail/hcx25909/5487985 代码的内 容主要是前边用到的机器人smartcar的urdf文件,已经

求java开发的FTP服务器与客户端源码,必有重谢!

问题描述 要求界面和谐,有权限控制,各位大大手头上有的请帮个忙吧!

[一]RabbitMQ-客户端源码之ConnectionFactory

首先看一段amqp-client发送端的示例代码(展示出主要部分): ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ip); factory.setPort(5672); factory.setUsername("root"); factory.setPassword("root"); Connection connection = factory.newConnection

[Conclusion]RabbitMQ-客户端源码之总结

RabbitMQ遵从的是AMQP协议,其broker端代码采用erlang编写,对于没有接触过erlang的同学(包括博主我)来说,想要了解其中的奥秘实在是不容易,大多只能从网上"搜刮"点散碎的知识点来充实一下.但是这样是不能究其然,更不能究其所以然.博主这里翻阅了amqp-client的java客户端的源码,通过其来学习下AMQP协议,进而更深刻的了解RabbitMQ. 注:如无特殊说明,本系列的文章采用的amqp-client版本均为3.5.3. 本系列的文章主要是来阐述客户端与b

[二]RabbitMQ-客户端源码之AMQConnection

上一篇文章([一]RabbitMQ-客户端源码之ConnectionFactory)中阐述了conn.start()方法完成之后客户端就已经和broker建立了正常的连接,而这个Connection的关键就在于这个start()方法之内,下面我们来慢慢分析. 首先来看看start()方法的源码,这个方法有点长,这里拆开来一一分析,首先是注释: /** * Start up the connection, including the MainLoop thread. * Sends the pro

[九]RabbitMQ-客户端源码之Consumer

在[八]RabbitMQ-客户端源码之ChannelN中讲述basicConsume的方法时设计到Consumer这个回调函数,Consumer其实是一个接口,真正实现它的是QueueingConsumer和DefaultConsumer,且DefaultConsumer是QueueingConsumer的父类,里面都是空方法.在用户使用时可以简单的采用QueueingConsumer或者采用DefaultConsumer来重写某些方法. 这里先来看下消费者客户端的关键代码: QueueingC

[五]RabbitMQ-客户端源码之AMQChannel

AMQChannel是一个抽象类,是ChannelN的父类.其中包含唯一的抽象方法: /** * Protected API - called by nextCommand to check possibly handle an incoming Command before it is returned to the caller of nextCommand. If this method * returns true, the command is considered handled a