易学笔记-RabbitMQ教程1:一个生产者和一个消费者

  • RabbitMQ教程1:一个生产者和一个消费者
  1. 添加依赖:

    <dependency>
        <groupId>com.rabbitmqgroupId
    >
        <artifactId>amqp-clientartifactId>
        <version>4.1.0version>dependency

  2. 新建生产者类:
    //引入相关Class文件
    import java.util.concurrent.TimeUnit;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    public class Send {
    
        //定义队列名字
        private final static String QUEUE_NAME = "hello";
    
        public static void main(String[] argv) throws Exception {
        	System.out.println("test1");
            //创建连接和通道
            ConnectionFactory factory = new ConnectionFactory();
    //        factory.setHost("192.168.248.79");
            factory.setHost("192.168.65.129");
            factory.setPort(5672);
            System.out.println("test2");
            Connection connection = factory.newConnection();
            System.out.println("test21");
            Channel channel = connection.createChannel();
            System.out.println("test3");
    
            //为通道指明队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String basemessage = "Hello World!";
            
            System.out.println("test4");
            
            for (int i = 0; i < 10; i++) {
            	String message = basemessage + i;
            	 //发布消息
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + message + "'");
                TimeUnit.SECONDS.sleep(5);
            }
            
            System.out.println("test5");
           
    
            //关闭连接
            channel.close();
            connection.close();
        }
    }
    
    1. 定义队列名称:

      private final static String QUEUE_NAME = "hello";

    2. 创建连接和通道 ,这里的localhost是 :192.168.65.129
    3. 为通道指明队列:

       //为通道指明队列
              channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);
              String message = "Hello World!";

    4. 发布消息:

       //发布消息

                channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));

                System.out.println(" [x] Sent '" + message + "'");

             

    5. 关闭通道:
    引入相关类:

    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;

  3. 新建接收类:
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    
    public class Recv {
    
        private final static String QUEUE_NAME = "hello";
    
        public static void main(String[] argv) throws Exception {
        	System.out.println("begin");
            //建立连接和通道
            ConnectionFactory factory = new ConnectionFactory();
    //        factory.setHost("192.168.248.79");
            factory.setHost("192.168.65.129");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            //声明要消费的队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            //回调消费消息
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                        throws IOException {
                	//获取消息队列内容
                    String message = new String(body, "UTF-8");
                    System.out.println(" [x] Received '" + message + "'");
                }
            };
            /**
             * QUEUE_NAME:这个队列名称要与发布者相同
             * ture:表示自动确认,也就是消息一旦发送就自动确认了,不需要消费者确认
             * false:表示要消费者明确返回确认
             * consumer:消费者对象
             */
            
            channel.basicConsume(QUEUE_NAME, true, consumer);
        }
    }
    
    1. 创建连接和通道 ,这里的localhost是 :192.168.65.129
    2. 为通道指明队列:

       //为通道指明队列
              channel.queueDeclare(QUEUE_NAME, falsefalsefalsenull);
              String message = "Hello World!";

    3. 匿名类声明消费者对象

      //回调消费消息

              Consumer consumer = new DefaultConsumer(channel) {

                  @Override

                  public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)

                          throws IOException {

                      //获取消息队列内容

                      String message = new String(body, "UTF-8");

                      System.out.println(" [x] Received '" + message + "'");

                  }

              };

    4. 从通道上获取消息

       //QUEUE_NAME:这个队列名称要与发布者相同

              channel.basicConsume(QUEUE_NAME, true, consumer);

    引入相关类:
  4. 测试
    1. 如果队列连接不上报错,如:

      Exception in thread "main" java.util.concurrent.TimeoutException

           at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)

      则检查是否开通了rabbitmq远程访问易学笔记-RabbitMQ教程1:一个生产者和一个消费者_第1张图片
    2. 添加IP地址映射: rabbitmq java.util.concurrent.TimeoutException - 好库文摘

      [root@xunilocate rabbitmq]# cat /etc/hosts

      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

      192.168.65.130   huike

      192.168.65.129   xunilocate

你可能感兴趣的:(RabbitMq消息队列)