# rabbitmq-test **Repository Path**: pangzidemeng/rabbitmq-test ## Basic Information - **Project Name**: rabbitmq-test - **Description**: 联系rabbitmq - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-26 - **Last Updated**: 2022-06-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # rabbitmq-test #### 介绍 练习rabbit框架 #### 软件架构 rabbitmq 是一个队列 属于中间件多用两个服务之间 rabbitmq 就是一个相对来说比较复杂的队列 这个队列有很多模式 有一个入口,有一个出口 入口的方式有很多种 #### 使用说明 本章列举例子比较详细的说明rabbitmq的使用方式 0. base-knowledge rabbitmq的基础知识 1. simple01 最基础的使用方式 2. xxxx 3. xxxx #### 参与贡献 liang #### 特技 ## base-knowledge 官网教程:https://www.rabbitmq.com/tutorials/tutorial-one-java.html ![代表生产者](image/image-20200926093621714.png) 生产者 ![image-20200926093756599](image/image-20200926093756599.png) 队列 ![image-20200926093907458](image/image-20200926093907458.png) 消费者 ![image-20200926155531971](image/image-20200926155531971.png) exchange 交换机 hello 是一个体验版 ```java /** * 发送方 */ public class Send { public static String queueName = "hello"; public static String host = "192.168.1.12"; public static int port = 5672; public static String username = "admin"; public static String password = "admin"; public static String message = "hello world"; public static void main(String[] args) { //创建一个工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); factory.setPort(port); factory.setUsername(username); factory.setPassword(password); //创建链接 和通道 //通道使用做主要事情的 用于发送 和session很像 try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(queueName, false, false, false, null); channel.basicPublish("", queueName, null, message.getBytes()); System.out.println("send " + message); } catch (TimeoutException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 接受方 */ public class Recv { public static void main(String[] args) throws IOException, TimeoutException { //相同流程,在对rabbitmq进行操作的时候使用channel ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(Send.host); connectionFactory.setPort(Send.port); connectionFactory.setUsername(Send.username); connectionFactory.setPassword(Send.password); final Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); f2(channel); } public static void f2(Channel channel) throws IOException { DeliverCallback deliverCallback = new DeliverCallback() { @Override public void handle(String s, Delivery delivery) throws IOException { System.out.println(s); System.out.println(new String(delivery.getBody(), "UTF-8")); } }; channel.basicConsume(Send.queueName, true, deliverCallback, item -> { }); } } ``` 直接发送到队列中,直接消费 ```java /** * 接受方 */ public class Recv { private static final String TASK_QUEUE_NAME = "task_queue"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RabbitMqConfig.host); factory.setPassword(RabbitMqConfig.password); factory.setUsername(RabbitMqConfig.username); factory.setPort(RabbitMqConfig.port); final Connection connection = factory.newConnection(); final Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); try { doWork(message); } finally { System.out.println(" [x] Done"); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }; channel.basicConsume(TASK_QUEUE_NAME, false, deliverCallback, consumerTag -> { }); } private static void doWork(String task) { for (char ch : task.toCharArray()) { if (ch == '.') { try { Thread.sleep(1000); } catch (InterruptedException _ignored) { Thread.currentThread().interrupt(); } } } } } /** * 发送方 **/ public class Send { private static final String TASK_QUEUE_NAME = "task_queue"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RabbitMqConfig.host); factory.setPassword(RabbitMqConfig.password); factory.setUsername(RabbitMqConfig.username); factory.setPort(RabbitMqConfig.port); argv= new String[]{"hello", "word", "check"}; try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); String message = String.join(" ", argv); channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); } } } ``` ## simple01