File tree Expand file tree Collapse file tree
09mq/activemq-demo/src/main/java/io/order Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package io .order ;
22
3- import java .util .concurrent .ExecutorService ;
4- import java .util .concurrent .Executors ;
5- import java .util .concurrent .locks .ReentrantLock ;
6-
73import javax .annotation .Resource ;
84
95import 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 }
Original file line number Diff line number Diff line change 44import 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
1614public class Order {
Original file line number Diff line number Diff line change 44import java .util .Objects ;
55import java .util .concurrent .ExecutorService ;
66import java .util .concurrent .Executors ;
7- import java .util .concurrent .locks .ReentrantLock ;
87
98import 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 {
Original file line number Diff line number Diff line change 11package io .order .service ;
22
3- import java .util .UUID ;
4-
53import javax .annotation .Resource ;
64
75import 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: 发送订单消息
You can’t perform that action at this time.
0 commit comments