1、准备工具


2、依赖

  1. 原生(生产消费依赖相同)
    1. 核心依赖 amqp-clinet
      1. <dependencies>
      2. <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
      3. <dependency>
      4. <groupId>com.rabbitmq</groupId>
      5. <artifactId>amqp-client</artifactId>
      6. <version>5.12.0</version>
      7. </dependency>
      8. <dependency>
      9. <groupId>org.springframework.boot</groupId>
      10. <artifactId>spring-boot-starter-logging</artifactId>
      11. </dependency>
      12. </dependencies>

3、普通模式

  1. https://www.rabbitmq.com/tutorials/tutorial-one-python.html

Hello World!
image.png
生产者

  1. package com.lixin.producer;
  2. import com.rabbitmq.client.Channel;
  3. import com.rabbitmq.client.Connection;
  4. import com.rabbitmq.client.ConnectionFactory;
  5. import java.io.IOException;
  6. import java.util.concurrent.TimeoutException;
  7. /**
  8. * "简单模式" - 生产者
  9. *
  10. * @author EX-lixin028
  11. * @date 2021-04-29 17:14
  12. */
  13. public class Producer01 {
  14. /**
  15. * 队列名称
  16. */
  17. private static final String QUEUE = "hello_world";
  18. public static void main(String[] args) throws IOException, TimeoutException {
  19. Connection connection = null;
  20. Channel channel = null;
  21. try {
  22. ConnectionFactory connectionFactory = new ConnectionFactory();
  23. connectionFactory.setHost("127.0.0.1");
  24. connectionFactory.setPort(5672);
  25. connectionFactory.setUsername("guest");
  26. connectionFactory.setPassword("guest");
  27. // rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
  28. connectionFactory.setVirtualHost("/");
  29. // 创建与RabbitMQ服务的TCP连接
  30. connection = connectionFactory.newConnection();
  31. // 创建与Exchange的通道,每个连接可以创建多个通道,每个通道代表一个会话任务
  32. channel = connection.createChannel();
  33. /***
  34. * 声明队列,如果Rabbit中没有此队列将自动创建
  35. * param1:队列名称
  36. * param2:是否持久化
  37. * param3:队列是否独占此连接
  38. * param4:队列不再使用时是否自动删除此队列
  39. * param5:队列参数
  40. * */
  41. channel.queueDeclare(QUEUE, true, false, false, null);
  42. String message = "hello_world小明:[" + System.currentTimeMillis() + "]";
  43. /*** 消息发布方法
  44. * param1:Exchange的名称,如果没有指定,则使用Default Exchange
  45. * param2:routingKey,消息的路由Key,是用于Exchange(交换机)将消息转发到指定的消息队列
  46. * param3:消息包含的属性
  47. * param4:消息体
  48. * */
  49. /***
  50. * 这里没有指定交换机,消息将发送给默认交换机,每个队列也会绑定那个默认的交换机,但是不能显 示绑定或解除绑定
  51. * 默认的交换机,routingKey等于队列名称
  52. * */
  53. channel.basicPublish("", QUEUE, null, message.getBytes());
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. } finally {
  57. if (channel != null) {
  58. channel.close();
  59. }
  60. if (connection != null) {
  61. connection.close();
  62. }
  63. }
  64. }
  65. }

消费者

  1. package com.lixin.consumer;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.concurrent.TimeoutException;
  6. /**
  7. * "简单模式" - 消费者
  8. *
  9. * @author EX-lixin028
  10. * @date 2021-04-30 1:31
  11. */
  12. public class Consumer01 {
  13. private static final String QUEUE = "hello_world";
  14. public static void main(String[] args) throws IOException, TimeoutException {
  15. ConnectionFactory connectionFactory = new ConnectionFactory();
  16. connectionFactory.setHost("127.0.0.1");
  17. connectionFactory.setPort(5672);
  18. connectionFactory.setUsername("guest");
  19. connectionFactory.setPassword("guest");
  20. connectionFactory.setVirtualHost("/");
  21. Connection connection = connectionFactory.newConnection();
  22. Channel channel = connection.createChannel();
  23. channel.queueDeclare(QUEUE, true, false, false, null);
  24. DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
  25. @Override
  26. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  27. // 获取交换机
  28. String exchange = envelope.getExchange();
  29. // 消息id,mq在channel中用来标记消息的id,可用于确认消息已接收
  30. long deliveryTag = envelope.getDeliveryTag();
  31. // 消息内容
  32. String message = new String(body, StandardCharsets.UTF_8);
  33. System.out.println("消费消息:" + message);
  34. }
  35. };
  36. channel.basicConsume(QUEUE, true, defaultConsumer);
  37. }
  38. }

4、工作模式

4.1 Work Queues

  1. https://www.rabbitmq.com/tutorials/tutorial-two-python.html

image.png
image.png
image.png
image.png

4.2 Publish/Subscribe

  1. https://www.rabbitmq.com/tutorials/tutorial-three-python.html

image.png
image.png
image.png
image.png
image.png
image.png
生产者

  1. package com.lixin.producer;
  2. import com.rabbitmq.client.BuiltinExchangeType;
  3. import com.rabbitmq.client.Channel;
  4. import com.rabbitmq.client.Connection;
  5. import com.rabbitmq.client.ConnectionFactory;
  6. import java.io.IOException;
  7. import java.util.concurrent.TimeoutException;
  8. /**
  9. * "public/subscribe模式" - 生产者
  10. *
  11. * @author EX-lixin028
  12. * @date 2021-04-29 17:14
  13. */
  14. public class Producer02Publish {
  15. /**
  16. * email队列名称
  17. */
  18. private static final String QUEUE_EMAIL = "queue_email";
  19. /**
  20. * sms队列名称
  21. */
  22. private static final String QUEUE_SMS = "queue_sms";
  23. /**
  24. * 交换机
  25. */
  26. private static final String EXCHANGE_FANOUT = "exchange_fanout";
  27. public static void main(String[] args) throws IOException, TimeoutException {
  28. Connection connection = null;
  29. Channel channel = null;
  30. try {
  31. ConnectionFactory connectionFactory = new ConnectionFactory();
  32. connectionFactory.setHost("127.0.0.1");
  33. connectionFactory.setPort(5672);
  34. connectionFactory.setUsername("guest");
  35. connectionFactory.setPassword("guest");
  36. // rabbitmq默认虚拟机名称为“/”,虚拟机相当于一个独立的mq服务器
  37. connectionFactory.setVirtualHost("/");
  38. // 创建与RabbitMQ服务的TCP连接
  39. connection = connectionFactory.newConnection();
  40. // 创建与Exchange的通道,每个连接可以创建多个通道,每个通道代表一个会话任务
  41. channel = connection.createChannel();
  42. /***
  43. * 声明队列,如果Rabbit中没有此队列将自动创建
  44. * param1:队列名称
  45. * param2:是否持久化
  46. * param3:队列是否独占此连接
  47. * param4:队列不再使用时是否自动删除此队列
  48. * param5:队列参数
  49. * */
  50. // 邮箱
  51. channel.queueDeclare(QUEUE_EMAIL, true, false, false, null);
  52. // 短信
  53. channel.queueDeclare(QUEUE_SMS, true, false, false, null);
  54. /**
  55. * 声明交换机
  56. * String name, String type
  57. * name: 交换机名称
  58. * type: 交换机类型
  59. */
  60. channel.exchangeDeclare(EXCHANGE_FANOUT, BuiltinExchangeType.FANOUT);
  61. // 将队列绑定到交换机上
  62. channel.queueBind(QUEUE_EMAIL, EXCHANGE_FANOUT, "");
  63. channel.queueBind(QUEUE_SMS, EXCHANGE_FANOUT, "");
  64. /*** 消息发布方法
  65. * param1:Exchange的名称,如果没有指定,则使用Default Exchange
  66. * param2:routingKey,消息的路由Key,是用于Exchange(交换机)将消息转发到指定的消息队列
  67. * param3:消息包含的属性
  68. * param4:消息体
  69. * */
  70. /***
  71. * 这里没有指定交换机,消息将发送给默认交换机,每个队列也会绑定那个默认的交换机,但是不能显 示绑定或解除绑定
  72. * 默认的交换机,routingKey等于队列名称
  73. * */
  74. for (int i = 0; i < 5; i++) {
  75. String message = "hello_world"+ i +"小明:[" + System.currentTimeMillis() + "]";
  76. channel.basicPublish(EXCHANGE_FANOUT, "", null, message.getBytes());
  77. System.out.println("消息发送:" + message);
  78. }
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. } finally {
  82. if (channel != null) {
  83. channel.close();
  84. }
  85. if (connection != null) {
  86. connection.close();
  87. }
  88. }
  89. }
  90. }

消费者

  1. package com.lixin.consumer;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.concurrent.TimeoutException;
  6. /**
  7. * "简单模式" - 消费者
  8. *
  9. * @author EX-lixin028
  10. * @date 2021-04-30 1:31
  11. */
  12. public class Consumer02 {
  13. private static final String QUEUE = "hello_world";
  14. public static void main(String[] args) throws IOException, TimeoutException {
  15. ConnectionFactory connectionFactory = new ConnectionFactory();
  16. connectionFactory.setHost("127.0.0.1");
  17. connectionFactory.setPort(5672);
  18. connectionFactory.setUsername("guest");
  19. connectionFactory.setPassword("guest");
  20. connectionFactory.setVirtualHost("/");
  21. Connection connection = connectionFactory.newConnection();
  22. Channel channel = connection.createChannel();
  23. channel.queueDeclare(QUEUE, true, false, false, null);
  24. DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
  25. @Override
  26. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  27. // 获取交换机
  28. String exchange = envelope.getExchange();
  29. // 消息id,mq在channel中用来标记消息的id,可用于确认消息已接收
  30. long deliveryTag = envelope.getDeliveryTag();
  31. // 消息内容
  32. String message = new String(body, StandardCharsets.UTF_8);
  33. System.out.println("消费消息:" + message);
  34. }
  35. };
  36. channel.basicConsume(QUEUE, true, defaultConsumer);
  37. }
  38. }

4.3 Routing

  1. https://www.rabbitmq.com/tutorials/tutorial-four-python.html

Direct exchange
image.png
Multiple bindings
image.png
Putting it all together
image.png
image.png
image.png
image.png
image.png
image.png
生产者

  1. package com.lixin.router;
  2. import com.rabbitmq.client.BuiltinExchangeType;
  3. import com.rabbitmq.client.Channel;
  4. import com.rabbitmq.client.Connection;
  5. import com.rabbitmq.client.ConnectionFactory;
  6. import java.io.IOException;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.concurrent.TimeoutException;
  9. /**
  10. * "路由模式" - 生产者
  11. *
  12. * @author EX-lixin028
  13. * @date 2021-04-30 14:45
  14. */
  15. public class ProducerRouter {
  16. /**
  17. * email队列名称
  18. */
  19. private static final String QUEUE_EMAIL = "queue_email";
  20. /**
  21. * sms队列名称
  22. */
  23. private static final String QUEUE_SMS = "queue_sms";
  24. /**
  25. * 交换机
  26. */
  27. private static final String EXCHANGE_DIRECT = "exchange_direct";
  28. /**
  29. * 路由_sms
  30. */
  31. private static final String ROUTER_SMS = "router_sms";
  32. /**
  33. * 路由_email
  34. */
  35. private static final String ROUTER_EMAIL = "router_email";
  36. public static void main(String[] args) throws IOException, TimeoutException {
  37. ConnectionFactory connectionFactory = new ConnectionFactory();
  38. connectionFactory.setHost("127.0.0.1");
  39. connectionFactory.setPort(5672);
  40. connectionFactory.setUsername("guest");
  41. connectionFactory.setPassword("guest");
  42. connectionFactory.setVirtualHost("/");
  43. Connection connection = connectionFactory.newConnection();
  44. Channel channel = connection.createChannel();
  45. channel.queueDeclare(QUEUE_EMAIL, true, false, false, null);
  46. channel.queueDeclare(QUEUE_SMS, true, false, false, null);
  47. channel.exchangeDeclare(EXCHANGE_DIRECT, BuiltinExchangeType.DIRECT);
  48. // 队列和交换机绑定
  49. channel.queueBind(QUEUE_EMAIL, EXCHANGE_DIRECT, "info");
  50. channel.queueBind(QUEUE_EMAIL, EXCHANGE_DIRECT, ROUTER_EMAIL);
  51. channel.queueBind(QUEUE_SMS, EXCHANGE_DIRECT, "info");
  52. channel.queueBind(QUEUE_SMS, EXCHANGE_DIRECT, ROUTER_SMS);
  53. String message = "交换机模式测试数据....[" + System.currentTimeMillis() + "]";
  54. for (int i = 0; i < 5; i++) {
  55. channel.basicPublish(EXCHANGE_DIRECT, "info",null, message.getBytes(StandardCharsets.UTF_8));
  56. System.out.println(message);
  57. }
  58. System.out.println(message);
  59. channel.close();
  60. connection.close();
  61. }
  62. }

消费者

  1. package com.lixin.router;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.concurrent.TimeoutException;
  6. /**
  7. * "路由模式" - 消费者_email
  8. *
  9. * @author EX-lixin028
  10. * @date 2021-04-30 1:31
  11. */
  12. public class ConsumerEmail {
  13. /**
  14. * email队列名称
  15. */
  16. private static final String QUEUE_EMAIL = "queue_email";
  17. /**
  18. * 交换机
  19. */
  20. private static final String EXCHANGE_DIRECT = "exchange_direct";
  21. /**
  22. * 路由_email
  23. */
  24. private static final String ROUTER_EMAIL = "router_email";
  25. public static void main(String[] args) throws IOException, TimeoutException {
  26. ConnectionFactory connectionFactory = new ConnectionFactory();
  27. connectionFactory.setHost("127.0.0.1");
  28. connectionFactory.setPort(5672);
  29. connectionFactory.setUsername("guest");
  30. connectionFactory.setPassword("guest");
  31. connectionFactory.setVirtualHost("/");
  32. Connection connection = connectionFactory.newConnection();
  33. Channel channel = connection.createChannel();
  34. channel.exchangeDeclare(EXCHANGE_DIRECT, BuiltinExchangeType.DIRECT);
  35. channel.queueDeclare(QUEUE_EMAIL, true, false, false, null);
  36. channel.queueBind(QUEUE_EMAIL,EXCHANGE_DIRECT,"info");
  37. channel.queueBind(QUEUE_EMAIL,EXCHANGE_DIRECT,ROUTER_EMAIL);
  38. DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
  39. @Override
  40. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  41. // 获取交换机
  42. String exchange = envelope.getExchange();
  43. // 消息id,mq在channel中用来标记消息的id,可用于确认消息已接收
  44. long deliveryTag = envelope.getDeliveryTag();
  45. // 消息内容
  46. String message = new String(body, StandardCharsets.UTF_8);
  47. System.out.println("email消费消息:" + message);
  48. }
  49. };
  50. channel.basicConsume(QUEUE_EMAIL, true, defaultConsumer);
  51. }
  52. }
  1. package com.lixin.router;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.concurrent.TimeoutException;
  6. /**
  7. * "路由模式" - 消费者_sms
  8. *
  9. * @author EX-lixin028
  10. * @date 2021-04-30 1:31
  11. */
  12. public class ConsumerSms {
  13. /**
  14. * sms队列名称
  15. */
  16. private static final String QUEUE_SMS = "queue_sms";
  17. /**
  18. * 交换机
  19. */
  20. private static final String EXCHANGE_DIRECT = "exchange_direct";
  21. /**
  22. * 路由_sms
  23. */
  24. private static final String ROUTER_SMS = "router_sms";
  25. public static void main(String[] args) throws IOException, TimeoutException {
  26. ConnectionFactory connectionFactory = new ConnectionFactory();
  27. connectionFactory.setHost("127.0.0.1");
  28. connectionFactory.setPort(5672);
  29. connectionFactory.setUsername("guest");
  30. connectionFactory.setPassword("guest");
  31. connectionFactory.setVirtualHost("/");
  32. Connection connection = connectionFactory.newConnection();
  33. Channel channel = connection.createChannel();
  34. channel.exchangeDeclare(EXCHANGE_DIRECT, BuiltinExchangeType.DIRECT);
  35. channel.queueDeclare(QUEUE_SMS, true, false, false, null);
  36. channel.queueBind(QUEUE_SMS,EXCHANGE_DIRECT,"info");
  37. channel.queueBind(QUEUE_SMS,EXCHANGE_DIRECT,ROUTER_SMS);
  38. DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
  39. @Override
  40. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  41. // 获取交换机
  42. String exchange = envelope.getExchange();
  43. // 消息id,mq在channel中用来标记消息的id,可用于确认消息已接收
  44. long deliveryTag = envelope.getDeliveryTag();
  45. // 消息内容
  46. String message = new String(body, StandardCharsets.UTF_8);
  47. System.out.println("sms消费消息:" + message);
  48. }
  49. };
  50. channel.basicConsume(QUEUE_SMS, true, defaultConsumer);
  51. }
  52. }

4.4 Topics

  1. https://www.rabbitmq.com/tutorials/tutorial-five-python.html

Topic exchange
image.png
image.png
image.png
image.png
image.png
image.png

生产者

  1. package com.lixin.topic;
  2. import com.rabbitmq.client.BuiltinExchangeType;
  3. import com.rabbitmq.client.Channel;
  4. import com.rabbitmq.client.Connection;
  5. import com.rabbitmq.client.ConnectionFactory;
  6. import java.io.IOException;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.concurrent.TimeoutException;
  9. /**
  10. * "topic模式" - 生产者
  11. *
  12. * @author EX-lixin028
  13. * @date 2021-04-30 14:45
  14. */
  15. public class ProducerTopic {
  16. /**
  17. * email队列名称
  18. */
  19. private static final String TOPIC_EMAIL = "topic_email";
  20. /**
  21. * sms队列名称
  22. */
  23. private static final String TOPIC_SMS = "topic_sms";
  24. /**
  25. * 交换机
  26. */
  27. private static final String EXCHANGE_TOPIC = "exchange_topic";
  28. /**
  29. * 路由_sms
  30. */
  31. private static final String ROUTER_SMS = "topic.#.sms.#";
  32. /**
  33. * 路由_email
  34. */
  35. private static final String ROUTER_EMAIL = "topic.#.email.#";
  36. public static void main(String[] args) throws IOException, TimeoutException {
  37. ConnectionFactory connectionFactory = new ConnectionFactory();
  38. connectionFactory.setHost("127.0.0.1");
  39. connectionFactory.setPort(5672);
  40. connectionFactory.setUsername("guest");
  41. connectionFactory.setPassword("guest");
  42. connectionFactory.setVirtualHost("/");
  43. Connection connection = connectionFactory.newConnection();
  44. Channel channel = connection.createChannel();
  45. channel.queueDeclare(TOPIC_EMAIL, true, false, false, null);
  46. channel.queueDeclare(TOPIC_SMS, true, false, false, null);
  47. channel.exchangeDeclare(EXCHANGE_TOPIC, BuiltinExchangeType.TOPIC);
  48. // 队列和交换机绑定
  49. channel.queueBind(TOPIC_EMAIL, EXCHANGE_TOPIC, ROUTER_EMAIL);
  50. channel.queueBind(TOPIC_SMS, EXCHANGE_TOPIC, ROUTER_SMS);
  51. // sms
  52. for (int i = 0; i < 5; i++) {
  53. String message = "交换机sms测试数据....[" + System.currentTimeMillis() + "]";
  54. channel.basicPublish(EXCHANGE_TOPIC, "topic.sms",null, message.getBytes(StandardCharsets.UTF_8));
  55. System.out.println(message);
  56. }
  57. // email
  58. for (int i = 0; i < 5; i++) {
  59. String message = "交换机email测试数据....[" + System.currentTimeMillis() + "]";
  60. channel.basicPublish(EXCHANGE_TOPIC, "topic.email",null, message.getBytes(StandardCharsets.UTF_8));
  61. System.out.println(message);
  62. }
  63. //
  64. // // sms + email
  65. // for (int i = 0; i < 5; i++) {
  66. // String message = "交换机sms+ email测试数据....[" + System.currentTimeMillis() + "]";
  67. // channel.basicPublish(EXCHANGE_TOPIC, "topic.sms.email",null, message.getBytes(StandardCharsets.UTF_8));
  68. // System.out.println(message);
  69. // }
  70. channel.close();
  71. connection.close();
  72. }
  73. }

消费者

  1. package com.lixin.topic;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.concurrent.TimeoutException;
  6. /**
  7. * "topic模式" - 消费者_email
  8. *
  9. * @author EX-lixin028
  10. * @date 2021-04-30 1:31
  11. */
  12. public class ConsumerEmail {
  13. /**
  14. * email队列名称
  15. */
  16. private static final String TOPIC_EMAIL = "topic_email";
  17. /**
  18. * 交换机
  19. */
  20. private static final String EXCHANGE_TOPIC = "exchange_topic";
  21. /**
  22. * 路由_email
  23. */
  24. private static final String ROUTER_EMAIL = "topic.#.email.#";
  25. public static void main(String[] args) throws IOException, TimeoutException {
  26. ConnectionFactory connectionFactory = new ConnectionFactory();
  27. connectionFactory.setHost("127.0.0.1");
  28. connectionFactory.setPort(5672);
  29. connectionFactory.setUsername("guest");
  30. connectionFactory.setPassword("guest");
  31. connectionFactory.setVirtualHost("/");
  32. Connection connection = connectionFactory.newConnection();
  33. Channel channel = connection.createChannel();
  34. channel.exchangeDeclare(EXCHANGE_TOPIC, BuiltinExchangeType.TOPIC);
  35. channel.queueDeclare(TOPIC_EMAIL, true, false, false, null);
  36. channel.queueBind(TOPIC_EMAIL,EXCHANGE_TOPIC,ROUTER_EMAIL);
  37. DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
  38. @Override
  39. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  40. // 获取交换机
  41. String exchange = envelope.getExchange();
  42. // 消息id,mq在channel中用来标记消息的id,可用于确认消息已接收
  43. long deliveryTag = envelope.getDeliveryTag();
  44. // 消息内容
  45. String message = new String(body, StandardCharsets.UTF_8);
  46. System.out.println("email消费消息:" + message);
  47. }
  48. };
  49. channel.basicConsume(TOPIC_EMAIL, true, defaultConsumer);
  50. }
  51. }
  1. package com.lixin.topic;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.concurrent.TimeoutException;
  6. /**
  7. * "topic模式" - 消费者_sms
  8. *
  9. * @author EX-lixin028
  10. * @date 2021-04-30 1:31
  11. */
  12. public class ConsumerSms {
  13. /**
  14. * email队列名称
  15. */
  16. private static final String TOPIC_SMS = "topic_sms";
  17. /**
  18. * 交换机
  19. */
  20. private static final String EXCHANGE_TOPIC = "exchange_topic";
  21. /**
  22. * 路由_email
  23. */
  24. private static final String ROUTER_SMS = "topic.#.sms.#";
  25. public static void main(String[] args) throws IOException, TimeoutException {
  26. ConnectionFactory connectionFactory = new ConnectionFactory();
  27. connectionFactory.setHost("127.0.0.1");
  28. connectionFactory.setPort(5672);
  29. connectionFactory.setUsername("guest");
  30. connectionFactory.setPassword("guest");
  31. connectionFactory.setVirtualHost("/");
  32. Connection connection = connectionFactory.newConnection();
  33. Channel channel = connection.createChannel();
  34. channel.exchangeDeclare(EXCHANGE_TOPIC, BuiltinExchangeType.TOPIC);
  35. channel.queueDeclare(TOPIC_SMS, true, false, false, null);
  36. channel.queueBind(TOPIC_SMS,EXCHANGE_TOPIC,ROUTER_SMS);
  37. DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
  38. @Override
  39. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  40. // 获取交换机
  41. String exchange = envelope.getExchange();
  42. // 消息id,mq在channel中用来标记消息的id,可用于确认消息已接收
  43. long deliveryTag = envelope.getDeliveryTag();
  44. // 消息内容
  45. String message = new String(body, StandardCharsets.UTF_8);
  46. System.out.println("email消费消息:" + message);
  47. }
  48. };
  49. channel.basicConsume(TOPIC_SMS, true, defaultConsumer);
  50. }
  51. }

4.5 Remote procedure call (RPC)

Summary
image.png

4.6 Publisher Confirms