使用原生Api操作rabbitmq
发送的消息
rabbitmq控制台查看到的消息
消息的消费有两种形式,一种是拉取消息,一种推送消息
拉取消息
推送消息
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.java</groupId> <artifactId>rabbitmq-server</artifactId> <packaging>pom</packaging> <version> 1.0 -SNAPSHOT</version> <modules> <module>rabbitmq-provider</module> <module>rabbitmq-consumer</module> <module>rabbitmq-original</module> <module>rabbitmq-springboot</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.5 . 7 </version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version> 1.8 </java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> <version> 2.5 . 7 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 2.5 . 7 </version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> <!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12 </version> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version> 5.12 . 0 </version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version> 2.9 . 2 </version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version> 2.9 . 2 </version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> <version> 3.2 . 5 </version> </dependency> <!-- <dependency>--> <!-- <groupId>org.springframework.cloud</groupId>--> <!-- <artifactId>spring-cloud-stream-binder-kafka</artifactId>--> <!-- <version> 2.2 . 1 .RELEASE</version>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>org.springframework.cloud</groupId>--> <!-- <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>--> <!-- <version> 0.2 . 2 .RELEASE</version>--> <!-- </dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
上面的演示部分是直接和队列交互的,下面演示客户端,交换机,队列三者交互
代码部分
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId>rabbitmq-server</artifactId> <groupId>com.java</groupId> <version> 1.0 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>rabbitmq-original</artifactId> <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.java.util; import java.text.SimpleDateFormat; import java.util.Date; /** * 作者 yourheart * 时间: 2020/5/27 14:35 * 描述 */ public class TimeUtils { //获取当前时间 public static String getTime() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); String format = sdf.format(date); return format; } /** * 获取哪一天 * @return */ public static String getDay() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); String format = sdf.format(date); return format; } public static String getExactTime() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" ); String format = sdf.format(date); return format; } /** * 获取具体是哪一天的时间 * @return */ public static int getSpecificTime(){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); String format = sdf.format(date); String[] split = format.split( "-" ); int parseInt = Integer.parseInt(split[ 2 ]); return parseInt; } /** * 获取哪一年 * @return */ public static String getYear() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy" ); String format = sdf.format(date); return format; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package com.java; import com.java.util.TimeUtils; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import org.junit.Test; import java.util.HashMap; import java.util.Map; /** * @Description: 原生rabbitmq发送消息 * @Author: qiuxie * @Create: 2023/7/20 17:00 */ public class SendMessageTest { private final String QUEUE_NAME= "hello 20230721" ; private final String QUORUM_QUEUE_NAME= "hello quorum 20230721" ; private final String STREAM_QUEUE_NAME= "stream" ; @Test public void totalTest() throws Exception { // test002(); //test001(); test(); } public void test002() throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); while ( true ){ try { Thread.sleep(1000L); } catch (InterruptedException e) { System.out.println( "休眠异常" ); } //这里申明的是Stream队列 Map<String,Object> param= new HashMap<>( 16 ); param.put( "x-queue-type" , "stream" ); //20GB param.put( "x-max-length-bytes" ,20_000_000_000L); //100MB param.put( "x-stream-max-segment-size-bytes" ,100_000_000); channel.queueDeclare(STREAM_QUEUE_NAME, true , false , false ,param); String message= "hello qiuxie" + TimeUtils.getTime(); channel.basicPublish( "" , STREAM_QUEUE_NAME, null ,message.getBytes( "UTF-8" )); System.out.println( "发送的消息为:" +message); } } public void test001() throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); while ( true ){ try { Thread.sleep(1000L); } catch (InterruptedException e) { System.out.println( "休眠异常" ); } //这里申明的是Quorum队列 Map<String,Object> param= new HashMap<>( 16 ); param.put( "x-queue-type" , "quorum" ); channel.queueDeclare(QUORUM_QUEUE_NAME, true , false , false ,param); String message= "hello qiuxie" + TimeUtils.getTime(); channel.basicPublish( "" , QUORUM_QUEUE_NAME, null ,message.getBytes( "UTF-8" )); System.out.println( "发送的消息为:" +message); } } public void test() throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); while ( true ){ try { Thread.sleep(1000L); } catch (InterruptedException e) { System.out.println( "休眠异常" ); } //这里申明的是经典队列 channel.queueDeclare(QUEUE_NAME, false , false , false , null ); String message= "hello qiuxie " + TimeUtils.getTime(); channel.basicPublish( "" , QUEUE_NAME, null ,message.getBytes( "UTF-8" )); System.out.println( "发送的消息为:" +message); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.java; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; /** * @Description: * @Author: qiuxie * @Create: 2023/7/20 17:05 */ public class RabbitMqUtil { private static Connection connection; private static final String HOST_NAME= "192.168.0.106" ; private static final int HOST_PORT= 5672 ; public static final String QUEUE_HELLO= "hello" ; public static final String QUEUE_WORK= "work" ; public static final String QUEUE_PUBLISH= "publish" ; public static Connection getConnection() throws Exception { if ( null == connection) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_NAME); factory.setPort(HOST_PORT); factory.setUsername( "root" ); factory.setPassword( "qwejkl1992" ); connection = factory.newConnection(); } return connection; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.java; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.GetResponse; import org.junit.Test; import java.util.HashMap; import java.util.Map; /** * @Description: 拉取消息 * @Author: qiuxie * @Create: 2023/7/20 17:25 */ public class PullMessageTest { private final String QUEUE_NAME= "hello 20230721" ; private final String QUORUM_QUEUE_NAME= "hello quorum 20230721" ; @Test public void test() throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); channel.basicQos( 100 ); Map<String,Object> params = new HashMap<>( 16 ); System.out.println( " [*] Waiting for messages. To exit press CTRL+C" ); GetResponse response = channel.basicGet(QUEUE_NAME, true ); if ( null != response){ System.out.println( new String(response.getBody(), "UTF-8" )); } GetResponse response2 = channel.basicGet(QUEUE_NAME, true ); if ( null != response2){ System.out.println( new String(response2.getBody(), "UTF-8" )); } channel.close(); connection.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.java; import com.rabbitmq.client.*; import java.io.IOException; import java.util.Map; /** * @Description: 推送消息 * @Author: qiuxie * @Create: 2023/7/20 17:39 */ public class PushMessageTest { private final static String QUEUE_NAME= "hello 20230721" ; private final static String QUORUM_QUEUE_NAME= "hello quorum 20230721" ; private final static String STREAM_QUEUE_NAME= "stream" ; public static void main(String[] args) throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte [] body) throws IOException { String routingKey = envelope.getRoutingKey(); System.out.println( "routingKey >" +routingKey); String contentType = properties.getContentType(); System.out.println( "contentType >" +contentType); long deliveryTag = envelope.getDeliveryTag(); System.out.println( "deliveryTag >" +deliveryTag); System.out.println( "content:" + new String(body, "UTF-8" )); System.out.println( "messageId:" +properties.getMessageId()); Map<String, Object> headers = properties.getHeaders(); if (headers!= null ){ properties.getHeaders().forEach((key,value)-> System.out.println( "key: " +key + "; value: " +value)); } //消息处理完后,进行答复。答复过的消息,服务器就不会再次转发。 //没有答复过的消息,服务器会一直不停转发。 channel.basicAck(deliveryTag, false ); } }; //进入消费状态、手动ACK channel.basicConsume(QUEUE_NAME, false , consumer); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.java.exchange; import com.java.RabbitMqUtil; import com.java.util.TimeUtils; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import org.junit.Test; import java.io.IOException; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 0:16 */ public class EmitLogDirect { private final String EXCHANGE_NAME = "directExchange 20230722" ; private final String TOPIC_EXCHANGE_NAME = "topicExchange 20230722" ; @Test public void messageTest() throws Exception { while ( true ) { try { Thread.sleep(1000L); } catch (InterruptedException e) { System.out.println( "休眠异常" ); } test(); } } public void test001() throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); //发送者只管往exchange里发消息,而不用关心具体发到哪些queue里。 channel.exchangeDeclare(TOPIC_EXCHANGE_NAME, "topic" ); String message = "LOG INFO" ; channel.basicPublish(TOPIC_EXCHANGE_NAME, "anonymous.info" , null , message.getBytes()); // channel.basicPublish(EXCHANGE_NAME, "tuling.loulan.debug", null, message.getBytes()); } public void test() throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct" ); String message = "LOG INFO 44444 " + TimeUtils.getTime(); channel.basicPublish(EXCHANGE_NAME, "debug" , null , message.getBytes()); // channel.close(); // connection.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.java.exchange; import com.java.RabbitMqUtil; import com.rabbitmq.client.*; import java.io.IOException; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 0:23 */ public class ReceiveLogsDirect { private static final String EXCHANGE_NAME = "directExchange 20230722" ; public static void main(String[] args) throws Exception { Connection connection = RabbitMqUtil.getConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct" ); String queueName = channel.queueDeclare().getQueue(); System.out.println( "队列名称:" +queueName); channel.queueBind(queueName, EXCHANGE_NAME, "info" ); channel.queueBind(queueName, EXCHANGE_NAME, "debug" ); Consumer myconsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte [] body) throws IOException { System.out.println( "========================" ); String routingKey = envelope.getRoutingKey(); System.out.println( "routingKey >" + routingKey); String contentType = properties.getContentType(); System.out.println( "contentType >" + contentType); long deliveryTag = envelope.getDeliveryTag(); System.out.println( "deliveryTag >" + deliveryTag); System.out.println( "content:" + new String(body, "UTF-8" )); //消息处理完后,进行答复。答复过的消息,服务器就不会再次转发。 //没有答复过的消息,服务器会一直不停转发。 // channel.basicAck(deliveryTag, false); } }; channel.basicConsume(queueName, true , myconsumer); } } |
以上就是原生mq操作的全部过程,rabbitmq安装教程https://www.cnblogs.com/q202105271618/p/16290851.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异