RabbitMQ——quick start

Hello World

RabbitMQ工作图示:

(P) -> [|||] -> (C)

如上图:

P代表Producing,也就是消息的发送程序

中间的队列代表消息将会被存到对应的队列中。可以多个Producers向同一个队列发送消息,也可以多个Consumer接收同一个队列的消息

C代表Consumer,也就是等待接收消息的程序

需要的依赖:

<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

程序实例:

Producers

private final static String QUEEN_NAME = "hello";

public static void main(String[] args) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("101.33.239.187");
    // Connection 负责版本协商以及身份认证工作
    // Chanel 包含了大部分任务所需的API
    try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
        channel.queueDeclare(QUEEN_NAME, false, false, false, null);
        channel.basicPublish("", QUEEN_NAME, null, ("Hello RabbitMQ!" + Math.random()).getBytes(StandardCharsets.UTF_8));
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
    }
}

Consumers

 private final static String QUEEN_NAME = "hello";

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("101.33.239.187");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEEN_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback callback = (consumerTag, delivery) -> {
        String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(" [x] Received '" + msg + "'");
    };
    channel.basicConsume(QUEEN_NAME, true, callback, (consumerTag) -> {});
}

channel.basicConsume会阻塞进程有消息通知时会调用callback

PS:备注

  1. 如果有多个消费者,那么队列中的消息将会按照消费者队列进行依次递送

  2. rabbitmqctl list_queues 可以展示所有的队列信息

Hello World

RabbitMQ工作图示:

(P) -> [|||] -> (C)

如上图:

P代表Producing,也就是消息的发送程序

中间的队列代表消息将会被存到对应的队列中。可以多个Producers向同一个队列发送消息,也可以多个Consumer接收同一个队列的消息

C代表Consumer,也就是等待接收消息的程序

需要的依赖:

<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

程序实例:

Producers

private final static String QUEEN_NAME = "hello";

public static void main(String[] args) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("101.33.239.187");
    // Connection 负责版本协商以及身份认证工作
    // Chanel 包含了大部分任务所需的API
    try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
        channel.queueDeclare(QUEEN_NAME, false, false, false, null);
        channel.basicPublish("", QUEEN_NAME, null, ("Hello RabbitMQ!" + Math.random()).getBytes(StandardCharsets.UTF_8));
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
    }
}

Consumers

 private final static String QUEEN_NAME = "hello";

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("101.33.239.187");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEEN_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback callback = (consumerTag, delivery) -> {
        String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(" [x] Received '" + msg + "'");
    };
    channel.basicConsume(QUEEN_NAME, true, callback, (consumerTag) -> {});
}

channel.basicConsume会阻塞进程有消息通知时会调用callback

PS:备注

  1. 如果有多个消费者,那么队列中的消息将会按照消费者队列进行依次递送

  2. rabbitmqctl list_queues 可以展示所有的队列信息

Hello World

RabbitMQ工作图示:

(P) -> [|||] -> (C)

如上图:

P代表Producing,也就是消息的发送程序

中间的队列代表消息将会被存到对应的队列中。可以多个Producers向同一个队列发送消息,也可以多个Consumer接收同一个队列的消息

C代表Consumer,也就是等待接收消息的程序

需要的依赖:

<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

程序实例:

Producers

private final static String QUEEN_NAME = "hello";

public static void main(String[] args) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("101.33.239.187");
    // Connection 负责版本协商以及身份认证工作
    // Chanel 包含了大部分任务所需的API
    try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
        channel.queueDeclare(QUEEN_NAME, false, false, false, null);
        channel.basicPublish("", QUEEN_NAME, null, ("Hello RabbitMQ!" + Math.random()).getBytes(StandardCharsets.UTF_8));
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
    }
}

Consumers

 private final static String QUEEN_NAME = "hello";

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("101.33.239.187");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEEN_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback callback = (consumerTag, delivery) -> {
        String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(" [x] Received '" + msg + "'");
    };
    channel.basicConsume(QUEEN_NAME, true, callback, (consumerTag) -> {});
}

channel.basicConsume会阻塞进程有消息通知时会调用callback

PS:备注

  1. 如果有多个消费者,那么队列中的消息将会按照消费者队列进行依次递送

  2. rabbitmqctl list_queues 可以展示所有的队列信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值