前言
一、RabbitMQ事务控制是什么?
说到事务,大家脑海里自然会想到数据库的事务控制。事务的四大性质:原则性,隔离性,一致性,持久性。自然RabbitMQ也有一样的想法。接下来请看简单入门案例
二、使用步骤
1.引入库
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2.将RabbitMQ工厂对象注入到Spring容器中。
package com.hh.rabbitmq.config;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitFactoryConfig {
@Bean
public ConnectionFactory connectionFactory() {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.10.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("root");
connectionFactory.setPassword("123456");
return connectionFactory;
}
}
3.接下来创建接口测试
package com.hh.rabbitmq.controller;
import com.hh.rabbitmq.config.CustomConfirmCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RestController
public class RabbitController {
@Autowired
private ConnectionFactory connectionFactory;
@RequestMapping(value = "sendTransactionMessage", method = RequestMethod.POST)
public String sendTransactionMessage(@RequestBody String message) throws Exception{
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
try {
channel.txSelect(); // 表示开启事务控制
channel.exchangeDeclare("spring.channel.exchange", "direct",true);
channel.queueDeclare("spring.channel.queue",false,false,false,null);
channel.queueBind("spring.channel.queue","spring.channel.exchange","spring.channel.routing");
channel.basicPublish("spring.channel.exchange","spring.channel.routing",null,message.getBytes());
// int i = 1/0;
channel.txCommit(); // 代码无误的时候进行事务提交。
} catch (Exception e) {
e.printStackTrace();
channel.txRollback(); // 当运行过程中出现错误的时候,数据进行回滚,当前数据会被丢失。不会进入队列中。
return "fail";
}
return "ok";
}
@RequestMapping(value = "getTransactionMessage", method = RequestMethod.GET)
public String getTransactionMessage() throws Exception{
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
try {
// 第二个boolean值表示是否自动应答,true表示自动应答,false表示手动应答。
GetResponse getResponse = channel.basicGet("spring.channel.queue", false);
String message = new String(getResponse.getBody());
System.out.println(message);
// Ack表示消费消息,直接从队列中拉取。
// channel.basicAck(getResponse.getEnvelope().getDeliveryTag(),true);
// Nack表示消息发送后,进行消费数据,如果第三个参数设置为true的时候数据是不会被消费的。而是会将消息先放到Nnacked中,然后放回Ready队列中。
channel.basicNack(getResponse.getEnvelope().getDeliveryTag(),false,true);
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "ok";
}
}
总结
人生物语:善于领悟人生的人,懂得如何思考和行动,能够从碎屑的事物中发现闪光的契机。