maven配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringbootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringbootTest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
工具类
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class MqChanneUtils {
public static Channel getChannel() throws Exception{
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
factory.setConnectionTimeout(20000);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
return channel;
}
}
public class SleepUtils {
public static void sleeper(int i){
try {
Thread.sleep(i*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
生产者
?
public class MessProductor {
public static void main(String[] args) throws Exception{
Channel channel = MqChanneUtils.getChannel();
for(int i = 0; i < 10; i++){
System.out.println("生产消息:" + i);
String msg = "Hello RabbitMQ" + i;
channel.basicPublish("", "test01", null, msg.getBytes());
}
//5.记得关闭相关的连接
channel.close();
}
}
创建消费者01和02。代码逻辑都是一样的。只不过一个睡眠30s,一个睡眠2秒。这样做为了更好的看到应答效果。在一个消息没应答之前,是无法继续消费消息的。当一个消费者出现问题,比如我停掉01消费者。这时候消费会被02去消费。这样就保证了消息是在被消费后才能从生产者移除。
public class ConsumeConfirm {
@SneakyThrows
public static void main(String[] args) {
Channel channel = MqChanneUtils.getChannel();
DeliverCallback deliverCallback =(a, delivery)->{
String receivedMessage = new String(delivery.getBody());
SleepUtils.sleeper(30); //这里改下时间
System.out.println(" 接收到消息:"+receivedMessage);
//应答一个消息,最后false代表只应答当前消息。
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
};
//将通道的应答机制设置手动应答
boolean autoAck = false;
channel.basicConsume("test01",autoAck,deliverCallback,consumer->{});
}
}
?整个效果,01我消费第一条消息后就停掉服务,发现剩下的消息都被02消费了。
|