**
调用esb接口,实现数据定时同步
**
springboot项目中的定时器
1.yml文件的配置
#========= Scheduled 配置 ==========#
jobs:
sendGasJob:
corn: "0 0 1 * * ?"
2.Scheduler类
@Component("sendGasJob")
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
@PropertySource("classpath:/application-test.yml") // 3.配置文件位置
public class Scheduler {
@Scheduled(cron = "${jobs.sendGasJob.corn}")
public void doThis() throws IOException {
this.callOtherInterface();
}
}
3.同步数据
private Integer ZERO = 0;
@Autowired
private IOrganizationService service;
@Autowired
private IUserInfoService userInfoService;
@Autowired
private IYhzhJgbmService yhzhJgbmService;
@Autowired
private Constant constant;
private static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* post请求传输json数据
*
* @return
* @throws IOException
*/
public String callOtherInterface() throws IOException{
String url = this.constant.URL;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
calendar.add(Calendar.DAY_OF_MONTH,-1);
String str = sdf.format(calendar.getTime());
List<UserInfo> result = new ArrayList<>();
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
List<Organization> list = service.getJgbmbm();
List<UserInfo> userInfoList = userInfoService.selectUserAll();
for(int i = 0;i < list.size();i++) {
JSONObject json = new JSONObject(