Commit 8e47d0f6 by HlQ

[chg]

1.定时任务添加至配置文件,可配置定时任务执行时间
2.修改打包时,jar包的名字
1 parent cf921cf7
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
</dependencies> </dependencies>
<build> <build>
<finalName>VVAS-DataCenter-Weather</finalName>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
......
...@@ -19,7 +19,7 @@ public class WeatherController { ...@@ -19,7 +19,7 @@ public class WeatherController {
@GetMapping("/getWeather") @GetMapping("/getWeather")
public Object get() { public Object get() {
System.out.println("手动调用天气"); System.out.println("手动调用,获取今天的天气!");
try { try {
weatherService.getWeatherInfo(); weatherService.getWeatherInfo();
} catch (IOException e) { } catch (IOException e) {
......
package com.viontech.job; package com.viontech.job;
import java.io.IOException;
import com.viontech.service.impl.WeatherService; import com.viontech.service.impl.WeatherService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
/** /**
* @author zhaibolin * @author zhaibolin
*/ */
...@@ -16,7 +16,7 @@ public class SchedulerTask { ...@@ -16,7 +16,7 @@ public class SchedulerTask {
@Autowired @Autowired
private WeatherService weatherService; private WeatherService weatherService;
@Scheduled(cron = "${schedules}") @Scheduled(cron = "${schedules:0 0 8 * * ?}")
private void process() throws IOException { private void process() throws IOException {
weatherService.getWeatherInfo(); weatherService.getWeatherInfo();
} }
......
...@@ -6,10 +6,8 @@ import com.viontech.model.SimpleWeather; ...@@ -6,10 +6,8 @@ import com.viontech.model.SimpleWeather;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.IOException; import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
...@@ -34,7 +32,9 @@ public class WeatherService { ...@@ -34,7 +32,9 @@ public class WeatherService {
private CityService cityService; private CityService cityService;
public void getWeatherInfo() throws IOException { public void getWeatherInfo() throws IOException {
Date date = java.sql.Date.valueOf(LocalDate.now()); // 摆子哥之前为啥这样写?
// Date date = java.sql.Date.valueOf(LocalDate.now());
Date date = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println(LocalDateTime.now() + "天气服务调用开始"); System.out.println(LocalDateTime.now() + "天气服务调用开始");
List<Mall> mallList = cityService.selectMallList(); List<Mall> mallList = cityService.selectMallList();
List<City> cityList = cityService.selectCityList(mallList); List<City> cityList = cityService.selectCityList(mallList);
...@@ -57,6 +57,7 @@ public class WeatherService { ...@@ -57,6 +57,7 @@ public class WeatherService {
SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode); SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode);
if (simpleWeather == null) { if (simpleWeather == null) {
System.out.println(weatherCode); System.out.println(weatherCode);
continue;
} }
jdbcTemplate.update( jdbcTemplate.update(
...@@ -70,7 +71,7 @@ public class WeatherService { ...@@ -70,7 +71,7 @@ public class WeatherService {
Integer.parseInt(simpleWeather.getAqi()), Integer.parseInt(simpleWeather.getAqi()),
Integer.parseInt(simpleWeather.getFengli().substring(0, 1)) Integer.parseInt(simpleWeather.getFengli().substring(0, 1))
); );
System.out.println("" + bcity.getCityName() + "天气插入成功"); System.out.println(bcity.getCityName() + "天气插入成功");
} }
} }
System.out.println("天气服务调用结束"); System.out.println("天气服务调用结束");
......
...@@ -30,6 +30,7 @@ spring: ...@@ -30,6 +30,7 @@ spring:
jackson: jackson:
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8 time-zone: GMT+8
## 自定义配置项 ##
# 天气服务调用的定时任务,默认每天早上8点
schedules: schedules:
0 0 8 * * ? 0 0 8 * * ?
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!