RabbitMQ 入门(java)

115 阅读1分钟

1 安装

  • 安装Erlang
  • 安装RabbitMQ server
  • RabbitMQ中的概念:生产者,消费者,队列, 交换器,路由键, 绑定
  • AMQP协议:Modeule 层, Session 层, Transport 层
  • AMQP命令:Connection.Start, Connection.Start-OK, Connection.Tue, Connection.Tune-OK, Connection.Open, Connection.Open-OK, Connection.Close, Connection.Close-OK, Channel.Open, Channel.Open-OK, Channel.Close, Channel.Close-OK, Exchange.Declare, Exchange.Declare-Ok, Exchange.Delete, Exchange.Delete-OK, Exchange.Bind, Exchange.Bind-OK, Exchange.Unbind, Exchange.Unbind-OK, Queue.Declare, Queue,Declare-OK, Queue.Bind, Queue.Bind-OK, Queue.Purge, Queue.Purge-OK, Queue.Delete, Queue.Delete-OK, Queue.Unbind, Queue.Unbind-OK, Basic.Qos, Basic.Qos-OK, Basic.Consume, Basic.Consume-OK, Basic.Cancel, Basic.Cancel-OK

2 代码

  • 引入RabbitMQ 客户端

      <dependency>
          <groupId>com.rabbitmq</groupId>
          <artifactId>amqp-client</artifactId>
          <version>5.16.0</version>
      </dependency>
    
  • 发布者

import com.rabbitmq.client.*;

import java.io.IOException; import java.util.concurrent.TimeoutException;

public static void main(String[] args) throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.2.127");
    factory.setPort(5672); //rabbitmq默认端口号5672
    factory.setUsername("root");
    factory.setPassword("root");
    Connection connection = factory.newConnection(); //创建连接
    Channel channel = connection.createChannel(); //创建信道
    
    //创建交换器
    channel.exchangeDeclare("demo1Ex", BuiltinExchangeType.DIRECT, true, false, null);
    
    //创建队列
    channel.queueDeclare("demo1Q", true, false, false, null);
    
    //绑定
    channel.queueBind("demo1Q", "demo1Ex", "demo1Key");
    
    //发消息
    channel.basicPublish("demo1Ex", "demo1Key", MessageProperties.PERSISTENT_TEXT_PLAIN, "666".getBytes());

    //关闭资源
    channel.close();
    connection.close();

    System.out.println("Hello world!");
}
  • 消费者

import com.rabbitmq.client.*;

import java.io.IOException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException;

public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("root");
    factory.setPassword("root");
    Connection connection = factory.newConnection(new Address[]{new Address("192.168.2.127", 5672)});
    Channel channel = connection.createChannel();
    channel.basicQos(64);
    Consumer consumer = new DefaultConsumer(channel){
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            System.out.println("msg: " + new String(body));
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {

            }
            System.out.println(envelope.getDeliveryTag());
            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    };
    channel.basicConsume("demo1Q", consumer);
    TimeUnit.SECONDS.sleep(5);
    channel.close();
    connection.close(); //在connection关闭的时候, channel也会自动关闭

    System.out.println(connection.getCloseReason());
}