新增定时下载国家库数据任务代码

ww
anthonywj 2 years ago
parent 02e2819ec1
commit a5ab536508

@ -0,0 +1,50 @@
package com.glxp.api.task;
import com.glxp.api.constant.AsyncDiDlHelper;
import com.glxp.api.entity.basic.ProductInfoEntity;
import com.glxp.api.service.basic.ProductInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class AsyncDiDlService {
@Value("${UDI_SERVER_URL}")
private String udiUrl;
@Resource
ProductInfoService productInfoService;
@Resource
AsyncDiDlHelper asyncDiDlHelper;
private static final Logger logger = LoggerFactory.getLogger(AsyncDiDlService.class);
@Async
public void asyncDiByTime(String updateTime) {
int page = 1;
int limit = 200;
while (true) {
logger.info("更新时间:" + updateTime + "----" + page + "----" + limit);
List<ProductInfoEntity> productInfoEntityList = asyncDiDlHelper.dlByTime(udiUrl, page, limit, updateTime);
if (productInfoEntityList != null && productInfoEntityList.size() > 0) {
productInfoService.insertProductInfos(productInfoEntityList);
if (productInfoEntityList.size() < limit) {
break;
} else {
page++;
}
} else {
break;
}
}
logger.info("更新时间:" + updateTime + "----" + "下载结束");
}
}

@ -0,0 +1,63 @@
package com.glxp.api.task;
import com.glxp.api.dao.schedule.ScheduledDao;
import com.glxp.api.entity.system.ScheduledEntity;
import com.glxp.api.req.system.ScheduledRequest;
import com.glxp.api.util.DateUtil;
import com.glxp.api.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
@Component
@EnableScheduling
public class AsyncDiDlTask implements SchedulingConfigurer {
final Logger logger = LoggerFactory.getLogger(AsyncDiDlTask.class);
@Resource
RedisUtil redisUtil;
@Resource
private ScheduledDao scheduledDao;
@Resource
AsyncDiDlService asyncDiDlService;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(() -> process(),
triggerContext -> {
ScheduledRequest scheduledRequest = new ScheduledRequest();
scheduledRequest.setCronName("syncDi");
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
if (scheduledEntity == null) {
return null;
}
String cron = scheduledEntity.getCron();//"0 55 5 * * ?";
if (cron.isEmpty()) {
logger.error("cron is null");
}
return new CronTrigger(cron).nextExecutionTime(triggerContext);
});
}
private void process() {
String day = DateUtil.getLastDayFormat(-1);
asyncDiDlService.asyncDiByTime(day);
String lastUpDiTime = (String) redisUtil.get("lastDiUpTime");
if (lastUpDiTime == null) {
lastUpDiTime = DateUtil.getLastDayFormat(-10);
}
asyncDiDlService.asyncDiByTime(lastUpDiTime);
redisUtil.set("lastDiUpTime", DateUtil.formatDate(new Date()));
}
}

@ -4,11 +4,7 @@ import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
@ -577,7 +573,7 @@ public class DateUtil extends DateUtils {
}
/**
/**
* Date
*
* @param date
@ -684,4 +680,13 @@ public class DateUtil extends DateUtils {
Date dt1 = rightNow.getTime();
return dt1;
}
public static String getLastDayFormat(int amount) {
SimpleDateFormat smdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(calendar.DATE, amount);
String formatDate = smdate.format(calendar.getTime());
return formatDate;
}
}

Loading…
Cancel
Save