Commit 66f352a4 by HlQ

修改查询天气定时任务的执行时间(从每天8点调整到每天5点半)

1 parent 2c49603c
...@@ -73,9 +73,9 @@ public class DruidDBConfiguration { ...@@ -73,9 +73,9 @@ public class DruidDBConfiguration {
@Bean //声明其为Bean实例 @Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource @Primary //在同样的DataSource中,首先使用被标注的DataSource
@ConditionalOnExpression("#{'${spring.datasource.driverClassName:false}'!='false'}") @ConditionalOnExpression("#{'${spring.datasource.driverClassName:false}'!='false'}")
public DataSource dataSource(){ public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource(); DruidDataSource datasource = new DruidDataSource();
if("driverClassName".equals(driverClassName)){ if ("driverClassName".equals(driverClassName)) {
return null; return null;
} }
......
...@@ -25,77 +25,15 @@ import com.viontech.service.impl.JsonService; ...@@ -25,77 +25,15 @@ import com.viontech.service.impl.JsonService;
* @author zhaibolin * @author zhaibolin
*/ */
@Component @Component
public class SchedulerTask implements CommandLineRunner{ public class SchedulerTask {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private JsonService jsonService;
@Autowired
private CityService cityService;
@Autowired @Autowired
private WeatherService weatherService; private WeatherService weatherService;
@Scheduled(cron="0 0 8 * * ?") @Scheduled(cron = "0 30 5 * * ? ")
private void process() throws IOException{ private void process() throws IOException {
weatherService.getWeatherInfo(); weatherService.getWeatherInfo();
// Date now = new Date();
// LocalDate localDate=now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// Date date=java.sql.Date.valueOf(localDate);
// long s=System.currentTimeMillis();
// System.out.println(new Timestamp(s));
// System.out.println(date+"天气服务调用开始");
//// RowMapper<City> rowMapper = new BeanPropertyRowMapper<>(City.class);
//// List<City> Citys = jdbcTemplate.query("select city_id,province_id,weather_code,city_name,province_name from b_city where weather_code is not null and weather_code!='';", rowMapper);
//
//// RowMapper<SimpleWeather> rowMappers = new BeanPropertyRowMapper<>(SimpleWeather.class);
//// List<SimpleWeather> Weathers=jdbcTemplate.query("select province_id,city_id,type,data_date,ltemp,htemp,aqi,wind_intensity from d_weather where data_date='"+date+"';", rowMappers);
//// List<Integer> cityIds = Weathers.stream().map(SimpleWeather::getCity_id).collect(Collectors.toList());
//
// if(cityList.isEmpty()){
// System.out.println("请先给您所在城市添加城市编码");
// }else{
// for (City bcity : cityList) {
// if(cityIds.contains(bcity.getCityId())){
// System.out.println(""+bcity.getProvinceName()+bcity.getCityName()+"天气已经存在");
// continue;
// }
// Integer weatherCode= null;
// try {
// weatherCode = new Integer(bcity.getWeatherCode().trim());
// } catch (NumberFormatException e) {
// continue;
// }
// SimpleWeather simpleWeather= jsonService.getWeatherInfo(weatherCode.intValue());
// if(simpleWeather == null){
// System.out.println(weatherCode.intValue());
// }
//
// jdbcTemplate.update(
// "insert into d_weather(province_id,city_id,type,data_date,ltemp,htemp,aqi,modify_time,create_time,wind_intensity) values(?,?,?,?,?,?,?,NOW(),NOW(),?);",
// bcity.getProvinceId(),
// bcity.getCityId(),
// String.valueOf(simpleWeather.getType()),
// date,
// Integer.parseInt(simpleWeather.getLtemp()),
// Integer.parseInt(simpleWeather.getHtemp()),
// Integer.parseInt(simpleWeather.getAqi()),
// Integer.parseInt(simpleWeather.getFengli().substring(0,1))
// );
// System.out.println(""+bcity.getProvinceName()+bcity.getCityName()+"天气插入成功");
// }
// }
// System.out.println("天气服务调用结束");
} }
//@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
process();
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -24,19 +24,17 @@ public class CityService { ...@@ -24,19 +24,17 @@ public class CityService {
@Autowired @Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired private String SQL_SELECT_MALL = "select name,status,province_id,city_id from b_mall";
private JsonService jsonService; private String SQL_SELECT_CITY = "select area_name as cityName,province_id,weather_code,city_id from b_city where city_id in (:cityIds)";
private String SQL_SELECT_WEATHER = "select city_id from d_weather where data_date=?";
private String SQL_SELECT_MALL="select name,status,province_id,city_id from b_mall";
private String SQL_SELECT_CITY="select area_name as cityName,province_id,weather_code,city_id from b_city where city_id in (:cityIds)";
private String SQL_SELECT_WEATHER="select city_id from d_weather where data_date=?";
public List<Mall> selectMallList(){ public List<Mall> selectMallList() {
List<Mall> mallList= jdbcTemplate.query(SQL_SELECT_MALL,new BeanPropertyRowMapper<>(Mall.class)); List<Mall> mallList = jdbcTemplate.query(SQL_SELECT_MALL, new BeanPropertyRowMapper<>(Mall.class));
return mallList; return mallList;
} }
public List<City> selectCityList(List<Mall> mallList){
if(mallList.isEmpty()){ public List<City> selectCityList(List<Mall> mallList) {
if (mallList.isEmpty()) {
System.out.println("获取不到商场列表"); System.out.println("获取不到商场列表");
return null; return null;
} }
...@@ -46,8 +44,8 @@ public class CityService { ...@@ -46,8 +44,8 @@ public class CityService {
return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class)); return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class));
} }
public List<Integer> selectTodayWeather(Date date){ public List<Integer> selectTodayWeather(Date date) {
List<Integer> cityIdList=jdbcTemplate.queryForList(SQL_SELECT_WEATHER,new Object[]{date},Integer.class); List<Integer> cityIdList = jdbcTemplate.queryForList(SQL_SELECT_WEATHER, new Object[]{date}, Integer.class);
return cityIdList; return cityIdList;
} }
......
...@@ -29,8 +29,6 @@ import org.springframework.web.client.RestTemplate; ...@@ -29,8 +29,6 @@ import org.springframework.web.client.RestTemplate;
@Service @Service
public class JsonService { public class JsonService {
@Resource
private JdbcTemplate jdbcTemplate;
@Autowired @Autowired
private Json jsonclass; private Json jsonclass;
...@@ -50,11 +48,11 @@ public class JsonService { ...@@ -50,11 +48,11 @@ public class JsonService {
mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class); WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class);
SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage,weatherMessage2); SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2);
return simpleWeather; return simpleWeather;
} }
private String getWeatherJson(int cityId) throws IOException{ private String getWeatherJson(int cityId) throws IOException {
String host = jsonclass.getHost1(); String host = jsonclass.getHost1();
String path = jsonclass.getPath1(); String path = jsonclass.getPath1();
String method = "POST"; String method = "POST";
...@@ -73,11 +71,11 @@ public class JsonService { ...@@ -73,11 +71,11 @@ public class JsonService {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
// return response.toString();
return EntityUtils.toString(response.getEntity()); return EntityUtils.toString(response.getEntity());
} }
private String getAqiJson(int cityId) throws IOException{
private String getAqiJson(int cityId) throws IOException {
String host = jsonclass.getHost2(); String host = jsonclass.getHost2();
String path = jsonclass.getPath2(); String path = jsonclass.getPath2();
...@@ -102,20 +100,20 @@ public class JsonService { ...@@ -102,20 +100,20 @@ public class JsonService {
} }
private SimpleWeather weathers2SimpleWeather(WeatherMessage weatherMessage,WeatherMessage weatherMessage2){ private SimpleWeather weathers2SimpleWeather(WeatherMessage weatherMessage, WeatherMessage weatherMessage2) {
Data data = weatherMessage.getData(); Data data = weatherMessage.getData();
if(data == null){ if (data == null) {
return null; return null;
} }
data.getCity(); data.getCity();
data.getForecast(); data.getForecast();
City city = data.getCity(); City city = data.getCity();
if(city == null){ if (city == null) {
return null; return null;
} }
Forecast forecast = data.getForecast().get(0); Forecast forecast = data.getForecast().get(0);
if(forecast == null){ if (forecast == null) {
return null; return null;
} }
...@@ -124,9 +122,9 @@ public class JsonService { ...@@ -124,9 +122,9 @@ public class JsonService {
simpleWeather.setData_date(forecast.getPredictDate()); simpleWeather.setData_date(forecast.getPredictDate());
simpleWeather.setHtemp(forecast.getTempDay()); simpleWeather.setHtemp(forecast.getTempDay());
simpleWeather.setLtemp(forecast.getTempNight()); simpleWeather.setLtemp(forecast.getTempNight());
if(weatherMessage2.getData().getAqi()==null){ if (weatherMessage2.getData().getAqi() == null) {
simpleWeather.setAqi("0"); simpleWeather.setAqi("0");
}else{ } else {
simpleWeather.setAqi(weatherMessage2.getData().getAqi().getValue()); simpleWeather.setAqi(weatherMessage2.getData().getAqi().getValue());
} }
simpleWeather.setFengli(forecast.getWindLevelDay()); simpleWeather.setFengli(forecast.getWindLevelDay());
...@@ -134,7 +132,8 @@ public class JsonService { ...@@ -134,7 +132,8 @@ public class JsonService {
return simpleWeather; return simpleWeather;
} }
private int getWeatherCode(String weather){
private int getWeatherCode(String weather) {
switch (weather) { switch (weather) {
case "晴": case "晴":
case "大部晴朗": case "大部晴朗":
...@@ -203,6 +202,6 @@ public class JsonService { ...@@ -203,6 +202,6 @@ public class JsonService {
default: default:
return -1; return -1;
} }
} }
} }
...@@ -31,21 +31,21 @@ public class WeatherService { ...@@ -31,21 +31,21 @@ public class WeatherService {
public void getWeatherInfo() throws IOException { public void getWeatherInfo() throws IOException {
Date now = new Date(); Date now = new Date();
LocalDate localDate=now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate localDate = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Date date=java.sql.Date.valueOf(localDate); Date date = java.sql.Date.valueOf(localDate);
long s=System.currentTimeMillis(); long s = System.currentTimeMillis();
System.out.println(new Timestamp(s)); System.out.println(new Timestamp(s));
System.out.println(date+"天气服务调用开始"); System.out.println(date + "天气服务调用开始");
List<Mall> mallList=cityService.selectMallList(); List<Mall> mallList = cityService.selectMallList();
List<City> cityList=cityService.selectCityList(mallList); List<City> cityList = cityService.selectCityList(mallList);
List<Integer> cityIds=cityService.selectTodayWeather(date); List<Integer> cityIds = cityService.selectTodayWeather(date);
if(cityList != null) { if (cityList != null) {
for (City bcity : cityList) { for (City bcity : cityList) {
if(cityIds.contains(bcity.getCityId())){ if (cityIds.contains(bcity.getCityId())) {
System.out.println(""+bcity.getCityName()+"天气已经存在"); System.out.println("" + bcity.getCityName() + "天气已经存在");
continue; continue;
} }
Integer weatherCode= null; Integer weatherCode = null;
try { try {
if (bcity.getWeatherCode() == null) { if (bcity.getWeatherCode() == null) {
continue; continue;
...@@ -54,8 +54,8 @@ public class WeatherService { ...@@ -54,8 +54,8 @@ public class WeatherService {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
continue; continue;
} }
SimpleWeather simpleWeather= jsonService.getWeatherInfo(weatherCode.intValue()); SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode.intValue());
if(simpleWeather == null){ if (simpleWeather == null) {
System.out.println(weatherCode.intValue()); System.out.println(weatherCode.intValue());
} }
...@@ -68,9 +68,9 @@ public class WeatherService { ...@@ -68,9 +68,9 @@ public class WeatherService {
Integer.parseInt(simpleWeather.getLtemp()), Integer.parseInt(simpleWeather.getLtemp()),
Integer.parseInt(simpleWeather.getHtemp()), Integer.parseInt(simpleWeather.getHtemp()),
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("天气服务调用结束");
......
...@@ -163,6 +163,7 @@ public class HttpUtils { ...@@ -163,6 +163,7 @@ public class HttpUtils {
/** /**
* Put String * Put String
*
* @param host * @param host
* @param path * @param path
* @param method * @param method
...@@ -193,6 +194,7 @@ public class HttpUtils { ...@@ -193,6 +194,7 @@ public class HttpUtils {
/** /**
* Put stream * Put stream
*
* @param host * @param host
* @param path * @param path
* @param method * @param method
...@@ -293,14 +295,16 @@ public class HttpUtils { ...@@ -293,14 +295,16 @@ public class HttpUtils {
public X509Certificate[] getAcceptedIssuers() { public X509Certificate[] getAcceptedIssuers() {
return null; return null;
} }
public void checkClientTrusted(X509Certificate[] xcs, String str) { public void checkClientTrusted(X509Certificate[] xcs, String str) {
} }
public void checkServerTrusted(X509Certificate[] xcs, String str) { public void checkServerTrusted(X509Certificate[] xcs, String str) {
} }
}; };
ctx.init(null, new TrustManager[] { tm }, null); ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx); SSLSocketFactory ssf = new SSLSocketFactory(ctx);
// ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpClient.getConnectionManager(); ClientConnectionManager ccm = httpClient.getConnectionManager();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!