由内向外同步

master
wj 2 years ago
parent c807bfe86e
commit f6fae6f462

@ -0,0 +1,38 @@
package com.glxp.sale.admin.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 线
*/
@Configuration
@EnableAsync
public class TaskPoolConfig {
@Bean(name = "taskExecutor")
public ThreadPoolTaskExecutor taskExecutor() {
// 获取当前主机的cpu核心数
int threadCount = Runtime.getRuntime().availableProcessors();
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//核心池的大小
taskExecutor.setCorePoolSize(threadCount);
//线程池最大线程数
taskExecutor.setMaxPoolSize(threadCount * 2);
//队列最大长度
taskExecutor.setQueueCapacity(200);
//线程空闲时间
taskExecutor.setKeepAliveSeconds(60);
//配置线程前缀
taskExecutor.setThreadNamePrefix("custom_executor");
//配置拒绝策略
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//执行初始化
taskExecutor.initialize();
return taskExecutor;
}
}

@ -17,6 +17,11 @@ public enum BasicExportTypeEnum {
*/
COUNTRY_DI_DATA("country_di_data", "国家库DI数据"),
/**
*
*/
DOCUMENT_TYPE_DATA("document_type_data", "单据类型"),
/**
*
*/

@ -1,15 +1,17 @@
package com.glxp.sale.admin.controller.sync;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.glxp.sale.admin.constant.*;
import com.glxp.sale.admin.entity.param.SystemParamConfigEntity;
import com.glxp.sale.admin.entity.sync.*;
import com.glxp.sale.admin.http.SpGetHttp;
import com.glxp.sale.admin.http.SpPostHttp;
import com.glxp.sale.admin.idc.utils.UriUtils;
import com.glxp.sale.admin.req.sync.SpsSyncDataRequest;
import com.glxp.sale.admin.req.sync.SyncUpLoadRequest;
import com.glxp.sale.admin.res.sync.*;
import com.glxp.sale.admin.service.param.SystemParamConfigService;
import com.glxp.sale.admin.service.sync.BasicExportService;
@ -22,22 +24,28 @@ import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class SpsSyncDataController {
private final UriUtils uriUtils;
@Resource
RedisUtil redisUtil;
@Resource
@ -291,5 +299,67 @@ public class SpsSyncDataController {
return ResultVOUtils.error(500, "更新失败!");
}
@PostMapping("sps/sync/upload/data")
public Object uploadData(HttpServletRequest request, @RequestBody SyncUpLoadRequest upLoadRequest) {
String uri = uriUtils.parseUri(request.getRequestURL().toString());
String upperServerHost = systemParamConfigService.selectValueByParamKey("upper_server_ip");
if (StrUtil.isNotEmpty(upperServerHost)) {
uri = upperServerHost + "/" + uri;
}
JSONObject jsonObject = redirect(request, uri, upLoadRequest.getExportType());
return jsonObject;
}
private JSONObject redirect(HttpServletRequest request, String uri, BasicExportTypeEnum exportType) {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> httpEntity = uriUtils.buildHeader(request);
String taskId = request.getHeader("taskId");
ResponseEntity<JSONObject> responseBody = null;
if (request.getMethod().equals("POST")) {
//写入日志
writeUploadLog(taskId, httpEntity.getBody(), exportType);
responseBody = restTemplate.postForEntity(uri, httpEntity, JSONObject.class);
} else if (request.getMethod().equals("GET")) {
writeUploadLog(taskId, httpEntity.getBody(), exportType);
responseBody = restTemplate.exchange(uri, HttpMethod.GET, httpEntity, JSONObject.class);
}
JSONObject result = responseBody.getBody();
return result;
}
@Async
public void writeUploadLog(String taskId, String content, BasicExportTypeEnum exportType) {
String key = gennerOrderUtils.getOrders();
BasicUploadStatusEntity basicUploadStatusEntity = new BasicUploadStatusEntity();
basicUploadStatusEntity.setId(key);
basicUploadStatusEntity.setTaskId(taskId);
basicUploadStatusEntity.setStartTime(DateUtil.getDateTime());
basicUploadStatusEntity.setIdDatas(exportType.getKey());
basicUploadStatusEntity.setType(exportType.getRemark());
basicUploadStatusEntity.setScheduleType(BasicProcessStatus.SCHEDULE_NORMAL);
basicUploadService.insertDownloadStatus(basicUploadStatusEntity);
FileUtils.makeDirectory(filePath + "upload/");
String fileName = filePath + "upload/" + exportType.getRemark() + "-" + taskId + ".udi";
FileUtils.SaveFileAs(content, fileName);
}
@Async
public void writeDownloadLog(String taskId, String content, BasicExportTypeEnum exportType) {
String key = gennerOrderUtils.getOrders();
BasicExportStatusEntity myEntity = new BasicExportStatusEntity();
myEntity.setId(key);
myEntity.setUpdateTime(new Date());
myEntity.setType(exportType.getRemark());
myEntity.setIdDatas(exportType.getKey());
myEntity.setStartTime(DateUtil.getDateTime());
myEntity.setTaskId(taskId);
myEntity.setStatus(Constant.SYNC_STATUS_WAIT);
basicExportService.insertExportStatus(myEntity);
FileUtils.makeDirectory(filePath + "download/");
String fileName = filePath + "/download/" + BasicProcessStatus.NEW_ALL_ORDER + "-" + taskId + ".udi";
FileUtils.SaveFileAs(content, fileName);
}
}

@ -0,0 +1,19 @@
package com.glxp.sale.admin.req.sync;
import com.glxp.sale.admin.constant.BasicExportTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SyncUpLoadRequest {
private BasicExportTypeEnum exportType;
private Object data;
}
Loading…
Cancel
Save