Skip to content

Commit d44d674

Browse files
author
boyunkai
committed
[feature]添加重试以及多线程并发策略
1 parent 0ce4b5a commit d44d674

4 files changed

Lines changed: 11 additions & 16 deletions

File tree

09mq/activemq-demo/src/main/java/io/order/QueueApplication.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package io.order;
22

3-
import java.util.concurrent.ExecutorService;
4-
import java.util.concurrent.Executors;
5-
import java.util.concurrent.locks.ReentrantLock;
6-
73
import javax.annotation.Resource;
84

95
import org.springframework.boot.ApplicationArguments;
@@ -35,7 +31,7 @@ public static void main(String[] args) {
3531
}
3632

3733
@Override
38-
public void run(ApplicationArguments args) throws Exception {
34+
public void run(ApplicationArguments args) {
3935
for (int i = 0; i < 100; i++) {
4036
log.info(queueProducer.sendMessage(String.valueOf(i)) + "<=======入队");
4137
}

09mq/activemq-demo/src/main/java/io/order/model/Order.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import lombok.Data;
55

66
/**
7+
* 订单表
8+
*
79
* @author boyunkai <boyunkai@kuaishou.com>
810
* Created on 2021-02-07
911
*/
10-
11-
/**
12-
* 订单表
13-
*/
1412
@Data
1513
@AllArgsConstructor
1614
public class Order {

09mq/activemq-demo/src/main/java/io/order/service/QueueConsumer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.Objects;
55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
7-
import java.util.concurrent.locks.ReentrantLock;
87

98
import javax.annotation.Resource;
109

@@ -42,7 +41,10 @@ public void receiveMessage() {
4241
executorService.submit(() -> {
4342
try {
4443
// 重试策略
45-
retryBuss(order);
44+
int success = retryBuss(order);
45+
if (success <= 0) {
46+
log.info("订单" + order.getOrderId() + "=======>出队失败");
47+
}
4648
} catch (InterruptedException exception) {
4749
log.info("进程被打断");
4850
Thread.currentThread().interrupt();
@@ -52,14 +54,14 @@ public void receiveMessage() {
5254
}
5355
}
5456

55-
private void retryBuss(Order order) throws InterruptedException {
57+
private int retryBuss(Order order) throws InterruptedException {
5658
int retryNum = 1;
5759
while (retryNum <= TRY_TIMES) {
5860
try {
5961
// 加锁解决并发,效率降低
6062
int success = updateOrder(order);
6163
if (success > 0L) {
62-
break;
64+
return 1;
6365
}
6466
retryNum++;
6567
} catch (Exception e) {
@@ -68,6 +70,7 @@ private void retryBuss(Order order) throws InterruptedException {
6870
continue;
6971
}
7072
}
73+
return 0;
7174
}
7275

7376
private synchronized int updateOrder(Order order) throws IllegalAccessException, InterruptedException {

09mq/activemq-demo/src/main/java/io/order/service/QueueProducer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.order.service;
22

3-
import java.util.UUID;
4-
53
import javax.annotation.Resource;
64

75
import org.springframework.stereotype.Service;
@@ -22,7 +20,7 @@ public class QueueProducer {
2220
@Resource
2321
OrderMapper orderMapper;
2422

25-
public String sendMessage(String orderId) throws IllegalAccessException {
23+
public String sendMessage(String orderId) {
2624
// STEP 1: 生成订单
2725
Order order = new Order(0, orderId, 0);
2826
// STEP 2: 发送订单消息

0 commit comments

Comments
 (0)