RabbitMQ工作原理

RabbitMQ工作原理

在这里插入图片描述

Hello World(直连)模式

在这里插入图片描述

RabbitMQ连接工具类

public class RabbitMqUtils {
    
    

    private static ConnectionFactory connectionFactory;

    static {
    
    
        connectionFactory = new ConnectionFactory();
        //设置连接rabbitmq主机
        connectionFactory.setHost("10.15.0.9");
        //设置端口号
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名和密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");
    }


    //定义提供连接对象的方法
    public static Connection getConnection() {
    
    
        try {
    
    
            //重量级资源  -----静态资源只在类加载时启动,且只执行一次
//            ConnectionFactory connectionFactory = new ConnectionFactory();
//            //设置连接rabbitmq主机
//            connectionFactory.setHost("10.15.0.9");
//            //设置端口号
//            connectionFactory.setPort(5672);
//            //设置连接哪个虚拟主机
//            connectionFactory.setVirtualHost("/ems");
//            //设置访问虚拟主机的用户名和密码
//            connectionFactory.setUsername("ems");
//            connectionFactory.setPassword("123");
            return connectionFactory.newConnection();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return null;
    }

    //关闭通道和关闭连接工具方法
    public static void closeConnectionAndChannel(Channel channel, Connection connection) {
    
    
        try {
    
    
            if (channel != null) {
    
    
                channel.close();

            }
            if (connection != null) {
    
    
                connection.close();
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }

    }
}

Consumer

public class Consumer {
    
    
    public static void main(String[] args) throws IOException, TimeoutException {
    
    
      /*  //创建连接mq的连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接rabbitmq主机
        connectionFactory.setHost("10.15.0.9");
        //设置端口号
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名和密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");


        //获取连接对象
        Connection connection = connectionFactory.newConnection();

        //获取连接通道对象
        Channel channel = connection.createChannel();*/

        //通过工具类获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();

        //通道绑定对应的消息队列
        //参数1:队列名称,不存在情况下自动创建
        //参数2:用来定义队列特性是否要持久化 true----持久化队列   false----不持久化队列
        //参数3:exclusive 是否独占队列  true---独占   false---不独占
        //参数4:autoDelete 是否在消费完成后自动删除队列  true---自动删除  false---不自动删除
        channel.queueDeclare("hello", true, false, false, null);

        //消费消息
        //参数1:消费哪个队列的消息  队列名称
        //参数2:开始消息的自动确认机制
        //参数3:消费时的回调接口
        channel.basicConsume("hello", true, new DefaultConsumer(channel) {
    
    
            //最后一个参数
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) {
    
    
                System.out.println("new String(body) = " + new String(body));
            }
        });

       /* channel.close();
        connection.close();*/
        //使用工具类关闭连接
        RabbitMqUtils.closeConnectionAndChannel(channel, connection);

    }
}

Provider

public class Provider {
    
    
    //生产消息
    @Test
    public void testSendMessage() throws IOException, TimeoutException {
    
    
        /*//创建连接mq的连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接rabbitmq主机
        connectionFactory.setHost("10.15.0.9");
        //设置端口号
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名和密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");

        //获取连接对象
        Connection connection = connectionFactory.newConnection();

        //获取连接通道对象
        Channel channel = connection.createChannel();*/

        //通过工具类获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();

        //通道绑定对应的消息队列
        //参数1:队列名称,不存在情况下自动创建
        //参数2:用来定义队列特性是否要持久化 true----持久化队列   false----不持久化
        //参数3:exclusive 是否独占队列  true---独占   false---不独占
        //参数4:autoDelete 是否在消费完成后自动删除队列  true---自动删除  false---不自动删除
        //参数5:额外附加参数
        channel.queueDeclare("hello", true, false, false, null);

        //发布消息
        //参数1:交换机名称
        //参数2:队列名称
        //参数3:传递消息额外设置
        //参数4:消息的具体内容
        //MessageProperties.PERSISTENT_TEXT_PLAIN----持久化消息
        channel.basicPublish("", "hello", MessageProperties.PERSISTENT_TEXT_PLAIN, "hello rabbitmq".getBytes());

        /*channel.close();
        connection.close();*/
        //使用工具类关闭连接
        RabbitMqUtils.closeConnectionAndChannel(channel, connection);

    }


}

猜你喜欢

转载自blog.csdn.net/qq_38530648/article/details/114036553