Android RabbitMQ之Android初探

Android studio

前提條件

    private String userName = "dlw" ;
    private String passWord = "dlw" ;
    private String virtualHost = "/" ;
    private String hostName = "192.168.xx.xxx" ;
    private int portNum = 5672 ;
    private String queueName = "que" ;
    private String exchangeName = "demo" ;
    private String rountingKey = "key" ;

①Rabbit配置

    /**
     * Rabbit配置
     */
    private void setupConnectionFactory(){

        factory.setUsername(userName);
        factory.setPassword(passWord);
        factory.setHost(hostName);
        factory.setPort(portNum);

    }

②發消息

①工廠創建一個新的連接 ---- factory.newConnection() ;
②用連接創建一個通道 ---- connection.createChannel() ;
③聲明一個交換機 ---- channel.exchangeDeclare(String , String , Boolean) ;
④聲明一個獨佔服務器,並得到隊列名稱 ---- channel.queueDeclare().getQueue() ;
⑤將交換機和隊列綁定 ---- channel.queueBind(queueName , exchangeName , rountingKey) ;
⑥發佈消息 ---- channel.basicPublish(exchangeName , rountingKey ,null , msg);

    /**
     * 發消息
     */
    private void basicPublish(){

        try {
            //連接
            Connection connection = factory.newConnection() ;
            //通道
            Channel channel = connection.createChannel() ;
            //聲明瞭一個交換和一個服務器命名的隊列,然後將它們綁定在一起。
            channel.exchangeDeclare(exchangeName , "fanout" , true) ;//聲明exchange
            String queueName = channel.queueDeclare().getQueue() ;//聲明獨佔的服務器,並得到隊列名
            channel.queueBind(queueName , exchangeName , rountingKey) ;//將exchange和queue綁定
            //消息發佈
            byte[] msg = "hello word!".getBytes() ;
            channel.basicPublish(exchangeName , rountingKey  ,null , msg);
            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
API

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

③接受消息

    /**
     * 收消息
     */
    private void basicConsume(){

        try {
            //連接
            Connection connection = factory.newConnection() ;
            //通道
            final Channel channel = connection.createChannel() ;
            //實現Consumer的最簡單方法是將便捷類DefaultConsumer子類化。可以在basicConsume 調用上傳遞此子類的對象以設置訂閱:
            channel.basicConsume(queueName , false , "administrator" , new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);

                    String rountingKey = envelope.getRoutingKey() ;
                    String contentType = properties.getContentType() ;
                    String msg = new String(body) ;
                    long deliveryTag = envelope.getDeliveryTag() ;

                    Log.e("TAG" , rountingKey+":rountingKey") ;
                    Log.e("TAG" , contentType+":contentType") ;
                    Log.e("TAG" , msg+":msg") ;
                    Log.e("TAG" , deliveryTag+":deliveryTag") ;

                    channel.basicAck(deliveryTag , false);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
API

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

測試

在這裏插入圖片描述

完整代碼

public class RabbitTwo_Activity extends AppCompatActivity {

    private String userName = "dlw" ;
    private String passWord = "dlw" ;
    private String virtualHost = "/" ;
    private String hostName = "192.168.xx.xxx" ;
    private int portNum = 5672 ;
    private String queueName = "que" ;
    private String exchangeName = "demo" ;
    private String rountingKey = "key" ;

    ConnectionFactory factory = new ConnectionFactory() ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rabbittwo);
        
        setupConnectionFactory();
        new Thread(new Runnable() {
            @Override
            public void run() {
                basicPublish();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                basicConsume();
            }
        }).start();
    }

    /**
     * Rabbit配置
     */
    private void setupConnectionFactory(){

        factory.setUsername(userName);
        factory.setPassword(passWord);
        factory.setHost(hostName);
        factory.setPort(portNum);

    }

    /**
     * 發消息
     */
    private void basicPublish(){

        try {
            //連接
            Connection connection = factory.newConnection() ;
            //通道
            Channel channel = connection.createChannel() ;
            //聲明瞭一個交換和一個服務器命名的隊列,然後將它們綁定在一起。
            channel.exchangeDeclare(exchangeName , "fanout" , true) ;
            String queueName = channel.queueDeclare().getQueue() ;
            channel.queueBind(queueName , exchangeName , rountingKey) ;
            //消息發佈
            byte[] msg = "hello word!".getBytes() ;
            channel.basicPublish(exchangeName , rountingKey  ,null , msg);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }

    /**
     * 收消息
     */
    private void basicConsume(){

        try {
            //連接
            Connection connection = factory.newConnection() ;
            //通道
            final Channel channel = connection.createChannel() ;
            //實現Consumer的最簡單方法是將便捷類DefaultConsumer子類化。可以在basicConsume 調用上傳遞此子類的對象以設置訂閱:
            channel.basicConsume(queueName , false , "administrator" , new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);

                    String rountingKey = envelope.getRoutingKey() ;
                    String contentType = properties.getContentType() ;
                    String msg = new String(body) ;
                    long deliveryTag = envelope.getDeliveryTag() ;

                    Log.e("TAG" , rountingKey+":rountingKey") ;
                    Log.e("TAG" , contentType+":contentType") ;
                    Log.e("TAG" , msg+":msg") ;
                    Log.e("TAG" , deliveryTag+":deliveryTag") ;

                    channel.basicAck(deliveryTag , false);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }



}

鳴謝

官方指南
官方api

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章