Android RabbitMQ之Android初探

时间:2024-03-19 16:19:56

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);

    }

②发消息

    /**
     * 发消息
     */
    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();
        }

    }

测试

Android RabbitMQ之Android初探

完整代码

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