WANG 6 年之前
父节点
当前提交
455383690a

+ 11 - 5
examcloud-task-api-provider/src/main/java/cn/com/qmth/examcloud/task/api/provider/DataSyncCloudServiceProvider.java

@@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import cn.com.qmth.examcloud.commons.base.util.PropertiesUtil;
 import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.task.api.DataSyncCloudService;
 import cn.com.qmth.examcloud.task.api.request.SyncCourseReq;
@@ -40,6 +41,11 @@ public class DataSyncCloudServiceProvider extends ControllerSupport
 	@Autowired
 	DataSyncService dataSyncService;
 
+	private Boolean async() {
+		boolean async = PropertiesUtil.getBoolean("$sync.async", true);
+		return async;
+	}
+
 	@PostMapping("syncCourse")
 	@Override
 	public SyncCourseResp syncCourse(@RequestBody SyncCourseReq req) {
@@ -51,7 +57,7 @@ public class DataSyncCloudServiceProvider extends ControllerSupport
 		r.setName(req.getName());
 		r.setRootOrgId(req.getRootOrgId());
 		r.setSyncType(req.getSyncType());
-		dataSyncService.sync("syncCourse", r, true);
+		dataSyncService.sync("syncCourse", r, async(), true);
 
 		SyncCourseResp resp = new SyncCourseResp();
 		return resp;
@@ -67,7 +73,7 @@ public class DataSyncCloudServiceProvider extends ControllerSupport
 		r.setParentId(req.getParentId());
 		r.setRootId(req.getRootId());
 		r.setSyncType(req.getSyncType());
-		dataSyncService.sync("syncOrg", r, true);
+		dataSyncService.sync("syncOrg", r, async(), true);
 
 		SyncOrgResp resp = new SyncOrgResp();
 		return resp;
@@ -92,7 +98,7 @@ public class DataSyncCloudServiceProvider extends ControllerSupport
 		r.setStudentCode(req.getStudentCode());
 		r.setSyncType(req.getSyncType());
 
-		dataSyncService.sync("syncStudent", r, true);
+		dataSyncService.sync("syncStudent", r, async(), true);
 
 		SyncStudentResp resp = new SyncStudentResp();
 		return resp;
@@ -122,7 +128,7 @@ public class DataSyncCloudServiceProvider extends ControllerSupport
 		r.setRootOrgName(req.getRootOrgName());
 		r.setSyncType(req.getSyncType());
 
-		dataSyncService.sync("syncExam", r, true);
+		dataSyncService.sync("syncExam", r, async(), true);
 		SyncExamResp resp = new SyncExamResp();
 		return resp;
 	}
@@ -154,7 +160,7 @@ public class DataSyncCloudServiceProvider extends ControllerSupport
 		r.setInfoCollector(req.getInfoCollector());
 		r.setExamSite(req.getExamSite());
 
-		dataSyncService.sync("syncExamStudent", r, true);
+		dataSyncService.sync("syncExamStudent", r, async(), true);
 		SyncExamStudentResp resp = new SyncExamStudentResp();
 		return resp;
 	}

+ 3 - 2
examcloud-task-service/src/main/java/cn/com/qmth/examcloud/task/service/DataSyncService.java

@@ -20,7 +20,7 @@ public interface DataSyncService {
 	 * @param retry
 	 * @return
 	 */
-	boolean sync(String methodName, SyncBaseRequest req, Boolean retry);
+	void sync(String methodName, SyncBaseRequest req, Boolean async, Boolean retry);
 
 	/**
 	 * 同步
@@ -32,6 +32,7 @@ public interface DataSyncService {
 	 * @param retry
 	 * @return
 	 */
-	Boolean sync(String component, String methodName, SyncBaseRequest req, Boolean retry);
+	void sync(String component, String methodName, SyncBaseRequest req, Boolean async,
+			Boolean retry);
 
 }

+ 6 - 14
examcloud-task-service/src/main/java/cn/com/qmth/examcloud/task/service/impl/DataSyncServiceImpl.java

@@ -34,7 +34,7 @@ public class DataSyncServiceImpl implements DataSyncService {
 	HandleSyncCloudService handleSyncCloudService;
 
 	@Override
-	public boolean sync(String methodName, SyncBaseRequest req, Boolean retry) {
+	public void sync(String methodName, SyncBaseRequest req, Boolean async, Boolean retry) {
 		String group = PropertiesUtil.getString("$sync." + methodName + ".group");
 		if (StringUtils.isBlank(group)) {
 			throw new StatusException("T-001001", "group is not configured");
@@ -42,25 +42,17 @@ public class DataSyncServiceImpl implements DataSyncService {
 
 		List<String> componentList = RegExpUtil.findAll(group, "[^\\,]+");
 
-		boolean successful = true;
 		for (String component : componentList) {
-			boolean result = sync(component, methodName, req, retry);
-			if (!result) {
-				successful = false;
-			}
+			sync(component, methodName, req, async, retry);
 		}
-
-		return successful;
 	}
 
 	@Override
-	public Boolean sync(String component, String methodName, SyncBaseRequest req, Boolean retry) {
+	public void sync(String component, String methodName, SyncBaseRequest req, Boolean async,
+			Boolean retry) {
 		String url = PropertiesUtil.getString("$sync." + methodName + ".component." + component);
 		req.setUrl(url);
 
-		boolean async = PropertiesUtil.getBoolean("$sync.async", true);
-		boolean successful = false;
-
 		if (async) {
 			DataSyncEntity entity = new DataSyncEntity();
 			entity.setComponent(component);
@@ -74,7 +66,6 @@ public class DataSyncServiceImpl implements DataSyncService {
 				Method method = handleSyncCloudService.getClass().getMethod(methodName,
 						req.getClass());
 				method.invoke(handleSyncCloudService, req);
-				successful = true;
 			} catch (Exception e) {
 				if (retry) {
 					DataSyncEntity entity = new DataSyncEntity();
@@ -84,11 +75,12 @@ public class DataSyncServiceImpl implements DataSyncService {
 					entity.setSyncNum(0);
 					entity.setParamJson(JsonUtil.toJson(req));
 					dataSyncRepo.saveAndFlush(entity);
+				} else {
+					throw new RuntimeException(e);
 				}
 			}
 		}
 
-		return successful;
 	}
 
 }

+ 3 - 6
examcloud-task-service/src/main/java/cn/com/qmth/examcloud/task/service/job/DataSyncTask.java

@@ -59,19 +59,16 @@ public class DataSyncTask extends AbstractTask {
 				SyncBaseRequest req = (SyncBaseRequest) obj;
 
 				String methodName = cur.getMethodName();
-				boolean result = dataSyncService.sync(component, methodName, req, false);
-
-				if (result) {
+				try {
+					dataSyncService.sync(component, methodName, req, false, false);
 					dataSyncRepo.delete(cur);
-				} else {
+				} catch (Exception e) {
 					dataSyncRepo.increaseSyncNum(cur.getId());
 					throw new StatusException("T-102001", "同步失败");
 				}
-
 			}
 
 		}
-
 	}
 
 }