杨杰 4 年之前
父節點
當前提交
296d202938

+ 3 - 1
src/main/java/com/llisoft/pay/Application.java

@@ -4,13 +4,15 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
 
+@EnableAsync // 启动异步
 @SpringBootApplication
 public class Application {
 	
 	private static Logger logger = LoggerFactory.getLogger(Application.class);
 	
-	
+
 	public static void main(String[] args) {
 		SpringApplication.run(Application.class, args);
 		logger.info("service is running...");

+ 14 - 15
src/main/java/com/llisoft/pay/service/AppService.java

@@ -2,7 +2,10 @@ package com.llisoft.pay.service;
 
 import java.util.Objects;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import com.llisoft.pay.dao.AppDao;
@@ -10,10 +13,13 @@ import com.llisoft.pay.dao.ItemDao;
 import com.llisoft.pay.dao.OrderDao;
 import com.llisoft.pay.entity.App;
 import com.llisoft.pay.entity.Order;
+import com.llisoft.pay.util.HttpUtil;
 
 @Service
 public class AppService {
 	
+	private static Logger logger = LoggerFactory.getLogger(AppService.class);
+	
 	@Autowired
 	private AppDao appDao;
 	@Autowired
@@ -37,23 +43,16 @@ public class AppService {
 	}
 	
 	/**
-	 * 获取异步通知地址
+	 * 发送异步通知
 	 * @param appId
-	 * @param orderNum
-	 * @return
-	 */
-	public String notifyUrl(int appId, String orderNum) {
-		return packUrl(appDao.select(appId).getNotifyUrl(), orderNum);
-	}
-	
-	/**
-	 * 获取同步通知地址
-	 * @param appId
-	 * @param orderNum
-	 * @return
+	 * @param body
 	 */
-	public String returnUrl(int appId, String orderNum) {
-		return packUrl(appDao.select(appId).getReturnUrl(), orderNum);
+	@Async
+	public void notify(int appId, String orderNum, String body) {
+		String notifyUrl = this.packUrl(appDao.select(appId).getNotifyUrl(), orderNum);
+		logger.debug("异步通知: 请求地址: {}, 参数: {}", notifyUrl, body);
+		String result = HttpUtil.postJson(notifyUrl, body);
+		logger.debug("异步通知: 返回信息: {}", result);
 	}
 	
 	/**

+ 1 - 7
src/main/java/com/llisoft/pay/service/OrderService.java

@@ -12,7 +12,6 @@ import com.llisoft.pay.dao.OrderDao;
 import com.llisoft.pay.entity.App;
 import com.llisoft.pay.entity.Order;
 import com.llisoft.pay.util.CodeUtil;
-import com.llisoft.pay.util.HttpUtil;
 import com.llisoft.pay.util.JsonUtil;
 
 
@@ -115,12 +114,7 @@ public class OrderService {
 		logger.debug("订单处理成功, 状态更新为已支付: {}", orderId);
 		// 发送支付成功异步通知
 		Order order = orderDao.select(orderId);
-		String notifyUrl = appService.notifyUrl(order.getAppId(), order.getOrderNum());
-		if(Objects.nonNull(notifyUrl) && !notifyUrl.trim().isEmpty()) {
-			logger.debug("订单完成: 异步通知业务地址: {}, 参数: {}", notifyUrl, JsonUtil.toJson(order));
-			String result = HttpUtil.postJson(notifyUrl, JsonUtil.toJson(order));
-			logger.debug("订单完成: 业务端返回信息: {}", result);
-		}
+		appService.notify(order.getAppId(), order.getOrderNum(), JsonUtil.toJson(order));
 		return true;
 	}