spring job

1.
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.support.DaoSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;

import java.lang.reflect.Member;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class ProjectJob {

private final static Logger logger = LoggerFactory.getLogger(ProjectJob.class);

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Autowired
private ProjectService projectService;

@Value("${synchronizeNum}")
private Integer synchronizeNum;

@Value("${prefix_url}")
private String prefix_url;

@Value("${project_url}")
private String project_url;

@Value("${startTime}")
private String start_time;

@Scheduled(cron = "0/10 * * * * ?")
public void fetchData() {
Long time = System.currentTimeMillis();
Long refTime =time+10*1000*60;
logger.info("项目同步开始 :" + df.format(new Date()));
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put("startTime", start_time);
Integer sum = projectService.sum(map);
if (sum < 1) {
return;
}
PageObject pageObj = new PageObject();
pageObj.setTotalCount(sum);
pageObj.setPageSize(synchronizeNum);
int pageNum = pageObj.getTotalCount();
String token = AccessTokenUtil.getByHttpsAccessToken();
if (StringUtils.isEmpty(token)) {
logger.error("同步项目无法获取到token->" + token);
return;
}
for (int i = 0; i < pageNum; i++) {
pageObj.setPage(i);
map.put("startIndex", 0);
map.put("endIndex", synchronizeNum);
List<ProjectVo> list = projectService.findList(map);
if (list.isEmpty()) {
return;
}else{
addInitMembers(list);
}
Long currTime=System.currentTimeMillis();
if(currTime>refTime){
refTime=refTime+10*1000*60;
token = AccessTokenUtil.getByHttpsAccessToken();
if (StringUtils.isEmpty(token)) {
logger.error("重新刷新token->" + token);
return;
}
logger.error("重新刷新项目token时间==="+df.format(new Date()));
}
sendPro(token, JSON.toJSONString(list,
SerializerFeature.WriteMapNullValue));
}
} catch (Exception e) {
logger.error("同步项目数据报错->", e);
}
logger.info("项目同步结束 :" + df.format(new Date()));
}

public void sendPro(String token, String jsonData) {
String finalUrl = prefix_url + project_url;
Map<String, Object> param = Maps.newHashMap();
try {
param.put("accessToken", token);
param.put("data", jsonData);
ResponseMessage rspMessage = SendHttpUtils.sendHttpsRequest(
finalUrl, param);
resultProc(rspMessage, jsonData);
} catch (Exception e) {
e.printStackTrace();
logger.error("同步项目数据报错->{}", e);
}
}

public void resultProc(ResponseMessage rspMsg, String jsonData)
throws Exception {
if (rspMsg.getStatusCode() == 200) {
String resultContent = rspMsg.getContent();
// 序列化方法使用Jackson为底包,可自行封装
Map<String, Object> resultMap = JSON.parseObject(resultContent,
new TypeReference<Map<String, Object>>() {
});
Integer code = (Integer) resultMap.get("code");
if (code == 0) {
List<ProjectVo> list = JSON.parseArray(jsonData, ProjectVo.class);
HashMap param = Maps.newHashMap();
for (ProjectVo pro : list) {
param.put("fprojectNo", pro.getProjectId());
projectService.add(param);
}
} else {
logger.error("同步项目数据报错:->{}", resultMap);
}
}

}

public void addInitMembers( List<ProjectVo> list){
for (int i=0;i<list.size();i++) {
ProjectVo vo=list.get(i);
List<MemberVo> faes=projectService.initFae(vo.getProjectId());
if(!faes.isEmpty()){
list.get(i).getInitMembers().addAll(faes);
}
List<MemberVo> tseList=projectService.initTSE(vo.getProjectId());
if(!tseList.isEmpty()){
list.get(i).getInitMembers().addAll(tseList);
}
List<MemberVo> csList=projectService.initCS(vo.getProjectId());
if(!csList.isEmpty()){
list.get(i).getInitMembers().addAll(csList);
}
}
}

}


2.
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@EnableScheduling
@SpringBootApplication
@MapperScan("xxxxx.dao")
public class SynchroApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(SynchroApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SynchroApplication.class);
}


@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
return taskScheduler;

}
}



posted @ 2019-08-03 21:31  albert_think  阅读(405)  评论(0编辑  收藏  举报