RabbitMQ简单模式代码实现

RabbitMQ简单模式代码实现_第1张图片

 

1.构建maven工程,创建生产者、消费者

RabbitMQ简单模式代码实现_第2张图片

 2.引入依赖


    
    
        com.rabbitmq
        amqp-client
        5.6.0
    




    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.8.0
            
                1.8
                1.8
            
        
    

3.编写生产者代码

/**
 * 发送消息
 */
public class Producer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {

        // 1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2.设置参数
        factory.setHost("192.168.10.11");// ip 默认值 localhost
        factory.setPort(5672); // 端口 默认值 5672
        factory.setVirtualHost("/lei404"); // 虚拟机 默认值/
        factory.setUsername("lei"); // 用户名 默认值 guest
        factory.setPassword("lei404"); // 密码 默认值 guest
        // 3.创建连接connection
        Connection connection = factory.newConnection();
        // 4.创建channel
        Channel channel = connection.createChannel();
        // 5.创建队列Queue
//        public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare(
//        String queue, 队列名称
//        boolean durable, 是否持久化,当mq重启后它还在
//        boolean exclusive, 是否独占 只能有一个消费者监听队列;当connection关闭时是否删除队列 一般设为false
//        boolean autoDelete, 是否自动删除,当没有consumer时自动删除
//        Map arguments) throws IOException {
//        }
        // 如果没有一个名字叫做hello_world的队列会进行创建
        channel.queueDeclare("hello_world", true, false, false, null);
        // 6.发送消息
//        public void basicPublish(
//        String exchange, 交换机名称,简单模式下交换机会使用默认的
//        String routingKey, 路由名称
//        BasicProperties props, 配置信息
//        byte[] body 发送的消息数据) throws IOException {
        String body ="hello rabbitmq!";
        channel.basicPublish("","hello_world", null, body.getBytes());
        // 释放资源
        channel.close();
        connection.close();
    }
}

RabbitMQ简单模式代码实现_第3张图片

 4.消费者代码编写

public class Consumer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2.设置参数
        factory.setHost("192.168.10.11");// ip 默认值 localhost
        factory.setPort(5672); // 端口 默认值 5672
        factory.setVirtualHost("/lei404"); // 虚拟机 默认值/
        factory.setUsername("lei"); // 用户名 默认值 guest
        factory.setPassword("lei404"); // 密码 默认值 guest
        // 3.创建连接connection
        Connection connection = factory.newConnection();
        // 4.创建channel
        Channel channel = connection.createChannel();
        // 5.创建队列Queue
//        public com.rabbitmq.client.AMQP.Queue.DeclareOk queueDeclare(
//        String queue, 队列名称
//        boolean durable, 是否持久化,当mq重启后它还在
//        boolean exclusive, 是否独占 只能有一个消费者监听队列;当connection关闭时是否删除队列 一般设为false
//        boolean autoDelete, 是否自动删除,当没有consumer时自动删除
//        Map arguments) throws IOException {
//        }
        // 如果没有一个名字叫做hello_world的队列会进行创建
        channel.queueDeclare("hello_world", true, false, false, null);
        // 6.接收消息
//        basicConsume(
//        String queue, 队列名称
//        boolean autoAck, 是否自动确认
//        Consumer callback 回调函数)
        Consumer consumer = new DefaultConsumer(channel){
            /**
             * 回调方法,当收到消息后会自动执行该方法
             * @param consumerTag 消息的标识
             * @param envelope 获取一些信息,交换机的信息,路由key的信息
             * @param properties 配置信息
             * @param body 真实的数据
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumerTag:" + consumerTag);
                System.out.println("Exchange:" + envelope.getExchange());
                System.out.println("RoutingKey:" + envelope.getRoutingKey());
                System.out.println("properties:" + properties);
                System.out.println("body:" + new String(body));
            }
        };
        channel.basicConsume("hello_world", true, consumer);
        // 释放资源?不要关闭资源,实时监听,不能关闭资源
//        channel.close();
//        connection.close();
    }
}

RabbitMQ简单模式代码实现_第4张图片

 

你可能感兴趣的:(RabbitMQ,rabbitmq)