一、Erlang和rabbitMQ的安装

略。

二、网页端(http://127.0.0.1:15672)操作

1.创建Virtual Host

 

 2.设置user

1.点击host名称

 

 2.设置权限

 

 3.创建队列

 

 三、编写Java代码

1.pom.xml

        <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.12.0</version>
        </dependency>

 

2.RabbitMQConnection

 1 package com.miaoshaProject.mq;
 2 
 3 import com.rabbitmq.client.AMQP;
 4 import com.rabbitmq.client.Connection;
 5 import com.rabbitmq.client.ConnectionFactory;
 6 
 7 import java.io.IOException;
 8 import java.util.concurrent.TimeoutException;
 9 
10 /**
11  * @Author wangshuo
12  * @Date 2022/5/6, 16:51
13  * Please add a comment
14  */
15 public class RabbitMQConnection {
16 
17     //获取连接
18     public static Connection getConnection() throws IOException, TimeoutException {
19 
20         ConnectionFactory factory = new ConnectionFactory();
21         //指明VirtualHost
22         factory.setVirtualHost("/MuPing");
23         //设置账号和密码
24         factory.setUsername("guest");
25         factory.setPassword("guest");
26         factory.setHost("127.0.0.1");
27         factory.setPort(5672);
28         return factory.newConnection();
29     }
30 }

3.RabbitMQProducer

 1 package com.miaoshaProject.mq;
 2 
 3 import com.rabbitmq.client.Channel;
 4 import com.rabbitmq.client.Connection;
 5 
 6 import java.io.IOException;
 7 import java.util.concurrent.TimeoutException;
 8 
 9 /**
10  * @Author wangshuo
11  * @Date 2022/5/6, 16:57
12  * Please add a comment
13  */
14 public class RabbitMQProducer {
15 
16     private static final String QUEUE_NAME = "queue1";
17 
18     public static void main(String[] args) throws IOException, TimeoutException {
19 
20         //创建连接
21         Connection connection = RabbitMQConnection.getConnection();
22         //创建通道
23         Channel channel = connection.createChannel();
24         String msg = "消息测试1";
25         channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
26         //关闭通道和连接
27         channel.close();
28         connection.close();
29     }
30 }

4.RabbitMQConsumer

 1 package com.miaoshaProject.mq;
 2 
 3 import com.rabbitmq.client.*;
 4 
 5 import java.io.IOException;
 6 import java.util.concurrent.TimeoutException;
 7 
 8 /**
 9  * @Author wangshuo
10  * @Date 2022/5/6, 17:05
11  * Please add a comment
12  */
13 public class RabbitMQConsumer {
14 
15     private static final String QUEUE_NAME = "queue1";
16 
17     public static void main(String[] args) throws IOException, TimeoutException {
18 
19         //获取连接
20         Connection connection = RabbitMQConnection.getConnection();
21         //创建通道
22         Channel channel = connection.createChannel();
23         //自定义一个Consumer
24         DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
25 
26             //重写获取后动作
27             @Override
28             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
29 
30                 String s = new String(body, "UTF-8");
31                 System.out.println("消费者收到消息" + s);
32             }
33         };
34         //监听队列 第二个参数autoAck-true:自动签收消息 一般我们会设置为false
35         channel.basicConsume(QUEUE_NAME, true, defaultConsumer);
36     }
37 }

 四、Java代码保证消息不丢失

1.RabbitMQProducer v1.1

 1 package com.miaoshaProject.mq;
 2 
 3 import com.rabbitmq.client.Channel;
 4 import com.rabbitmq.client.Connection;
 5 
 6 import java.io.IOException;
 7 import java.util.concurrent.TimeoutException;
 8 
 9 /**
10  * @Author wangshuo
11  * @Date 2022/5/6, 16:57
12  * Please add a comment
13  */
14 public class RabbitMQProducer {
15 
16     private static final String QUEUE_NAME = "queue1";
17 
18     public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
19 
20         //demo1
21         //test1();
22 
23         //使用消息确认机制
24         //test2();
25 
26         //使用事务机制
27         test3();
28     }
29 
30     private static void test3() throws IOException, TimeoutException {
31 
32         //创建连接
33         Connection connection = RabbitMQConnection.getConnection();
34         //创建通道
35         Channel channel = connection.createChannel();
36         channel.txSelect();
37         try {
38             //发送消息
39             String msg = "消息测试1";
40             channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
41             channel.txCommit();
42         } catch (Exception e){
43             //打印错误信息,回滚
44             System.out.println(e);
45             channel.txRollback();
46         }finally {
47             //关闭通道和连接
48             channel.close();
49             connection.close();
50         }
51     }
52 
53     private static void test2() throws IOException, TimeoutException, InterruptedException {
54 
55         //创建连接
56         Connection connection = RabbitMQConnection.getConnection();
57         //创建通道
58         Channel channel = connection.createChannel();
59         String msg = "消息测试2";
60         //Select Confirms
61         channel.confirmSelect();
62         channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
63         //消息确认
64         boolean b = channel.waitForConfirms();
65         if (b)
66             System.out.println("投递成功");
67         else
68             System.out.println("投递失败");//抛出错误
69         //关闭通道和连接
70         channel.close();
71         connection.close();
72     }
73 
74     private static void test1() throws IOException, TimeoutException {
75         //创建连接
76         Connection connection = RabbitMQConnection.getConnection();
77         //创建通道
78         Channel channel = connection.createChannel();
79         String msg = "消息测试1";
80         channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
81         //关闭通道和连接
82         channel.close();
83         connection.close();
84     }
85 }

2.RabbitMQConsumer v1.1

 1 package com.miaoshaProject.mq;
 2 
 3 import com.rabbitmq.client.*;
 4 
 5 import java.io.IOException;
 6 import java.util.concurrent.TimeoutException;
 7 
 8 /**
 9  * @Author wangshuo
10  * @Date 2022/5/6, 17:05
11  * Please add a comment
12  */
13 public class RabbitMQConsumer {
14 
15     private static final String QUEUE_NAME = "queue1";
16 
17     public static void main(String[] args) throws IOException, TimeoutException {
18 
19         //获取连接
20         Connection connection = RabbitMQConnection.getConnection();
21         //创建通道
22         Channel channel = connection.createChannel();
23         //自定义一个Consumer
24         DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
25 
26             //重写获取后动作
27             @Override
28             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
29 
30                 String s = new String(body, "UTF-8");
31                 System.out.println("消费者收到消息" + s);
32                 //消费完成,通知MQ删除消息
33                 channel.basicAck(envelope.getDeliveryTag(), false);
34             }
35         };
36         //监听队列 第二个参数autoAck-true:自动签收消息 一般我们会设置为false
37         //channel.basicConsume(QUEUE_NAME, true, defaultConsumer);
38 
39         channel.basicConsume(QUEUE_NAME, false, defaultConsumer);
40     }
41 }