“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”
Open source address
Gitee.com/tech-famer/…
introduce
SpringBoot Is a SpringBoot reliable message component based on the Confirm and return callback mechanisms of RabbitMQ. It supports transaction messages, message rollback, failure retransmission and other functions.
Install the tutorial
Components can be installed in three ways
- Using the maven command:
mvn clean package deploy
Publish the project to a private repository and add component dependencies to the used project as follows:
<depandency> <groupId>com.farmer.reliablemessage</groupId> <artifactId>reliablemessage-spring-boot-starter</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / depandency >Copy the code
- Using the maven command:
mvn clean package install
Publish the project to the local repository and add component dependencies to the local project used as follows:
<depandency> <groupId>com.farmer.reliablemessage</groupId> <artifactId>reliablemessage-spring-boot-starter</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / depandency >Copy the code
- Copy the project source code directly to the required project for direct use.
Directions for use
- The component takes the message to the library, the primary key of the message database is of type VARCHAR(), and the consumer needs to provide the Spring bean that generates the primary key, which needs to implement the interface
com.farmer.reliablemessage.api.IDApi
; The component has a default primary key generation class built incom.farmer.reliablemessage.DefaultIDApi
, the algorithm is UUID, the user can realize the primary key generation algorithm, such as snowflake algorithm. The following are only examples:
@Configuration public class IdConfig { @Bean public IDApi idApi(){ return new IDApi() { @Override public String nextID() { return UUID.randomUUID().toString().replace("-",""); }}; }}Copy the code
- Introduce the MQTemplate provided by the reliable message component in the class that needs to send the message as follows:
@Service public class TestService { @Autowired private MQTemplate mqTemplate; @Transactional public void test(){ mqTemplate.saveAndSendMessage("exchangeName","routingKey","payload"); }}Copy the code
The component provides only one method, saveAndSendMessage, with parameter 1 as the switch name, parameter 2 as the routingKey, and parameter 3 as the message body.
Scene demonstration
- Ordinary message
A message sent using a component in a normal method is a normal message. Normal messages are immediately delivered to the message middleware and cannot be rolled back. Such as:
@Service public class TestService { @Autowired private MQTemplate mqTemplate; public void test(){ mqTemplate.saveAndSendMessage("exchangeName","routingKey","payload"); // TODO other code}}Copy the code
If an exception occurs in other code, the message will still be delivered to the messaging middleware.
- Transaction message
As the name implies, messages support transactions. In a transaction, a method sends a message using a component. The message is not delivered immediately, but only after the transaction commits, and if the transaction rolls back, the message is rolled back without being delivered.
2.1 Simple transaction method, as follows:
@Service public class TestService { @Autowired private MQTemplate mqTemplate; @Transactional public void test(){ mqTemplate.saveAndSendMessage("exchangeName","routingKey","payload"); // TODO other code}}Copy the code
The message is delivered to the message-oriented middleware after the transaction is committed by other code. When a runtime exception occurs in other code, the message is rolled back instead of being delivered to the messaging middleware.
2.2 Transactional methods call non-transactional methods as follows:
@Service public class TestService { @Autowired private MQTemplate mqTemplate; @ Transactional public void test () {/ / message 1 mqTemplate. SaveAndSendMessage (" exchangeName ", "routingKey", "content"); orther(); / / TODO other code} public void orther () {/ / message 2 mqTemplate. SaveAndSendMessage (" exchangeName ", "routingKey", "content"); }}Copy the code
Messages 1 and 2 are delivered to the message-oriented middleware after the test method executes the transaction commit. When a runtime exception occurs in other code or in the Orther method, the message is rolled back instead of being delivered to the messaging middleware.
Case 1 of transaction method invocation is as follows:
@Service public class TestService { @Autowired private MQTemplate mqTemplate; @Autowired @Lazy private TestService testService; @ Transactional public void test () {/ / message 1 mqTemplate. SaveAndSendMessage (" exchangeName ", "routingKey", "content"); testService.orther(); } @transactional (Propagation = Propagation.REQUIRED) public void orther(){// Message 2 mqTemplate.saveAndSendMessage("exchangeName","routingKey","payload"); }}Copy the code
Messages 1 and 2 are delivered to the message-oriented middleware after the test method executes the transaction commit. When a runtime exception occurs in other code or in the Orther method, the message is rolled back instead of being delivered to the messaging middleware.
2.4 Transaction method call transaction method case 2, as follows:
@Service public class TestService { @Autowired private MQTemplate mqTemplate; @Autowired @Lazy private TestService testService; @ Transactional public void test () {/ / message 1 mqTemplate. SaveAndSendMessage (" exchangeName ", "routingKey", "content"); testService.orther(); } @transactional (Propagation = propagation.required_new) public void orther(){// Message 2 mqTemplate.saveAndSendMessage("exchangeName","routingKey","payload"); }}Copy the code
Message 2 is delivered to the messaging middleware after the orther method completes the transaction commit. Message 1 is delivered to the messaging middleware after the test method executes the transaction commit. When a runtime exception occurs in other code or in the Orther method, the message is rolled back instead of being delivered to the messaging middleware.
2.5 Message Rollback
@Service public class TestService { @Autowired private MQTemplate mqTemplate; @Autowired @Lazy private TestService testService; @ Transactional public void test () {/ / message 1 mqTemplate. SaveAndSendMessage (" exchangeName ", "routingKey", "content"); testService.orther(); } @transactional (Propagation = Propagation.REQUIRED) public void orther(){// Message 2 mqTemplate.saveAndSendMessage("exchangeName","routingKey","payload"); int i = 1/0; }}Copy the code
Statement int I = 1/0; The runtime exception causes the transaction to roll back, and messages 1 and 2 are rolled back with the transaction and not delivered to the messaging middleware.