Commit 66f352a4 by HlQ

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

1 parent 2c49603c
......@@ -73,9 +73,9 @@ public class DruidDBConfiguration {
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
@ConditionalOnExpression("#{'${spring.datasource.driverClassName:false}'!='false'}")
public DataSource dataSource(){
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
if("driverClassName".equals(driverClassName)){
if ("driverClassName".equals(driverClassName)) {
return null;
}
......
......@@ -25,77 +25,15 @@ import com.viontech.service.impl.JsonService;
* @author zhaibolin
*/
@Component
public class SchedulerTask implements CommandLineRunner{
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private JsonService jsonService;
@Autowired
private CityService cityService;
public class SchedulerTask {
@Autowired
private WeatherService weatherService;
@Scheduled(cron="0 0 8 * * ?")
private void process() throws IOException{
@Scheduled(cron = "0 30 5 * * ? ")
private void process() throws IOException {
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
......@@ -24,19 +24,17 @@ public class CityService {
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired
private JsonService jsonService;
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=?";
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(){
List<Mall> mallList= jdbcTemplate.query(SQL_SELECT_MALL,new BeanPropertyRowMapper<>(Mall.class));
public List<Mall> selectMallList() {
List<Mall> mallList = jdbcTemplate.query(SQL_SELECT_MALL, new BeanPropertyRowMapper<>(Mall.class));
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("获取不到商场列表");
return null;
}
......@@ -46,8 +44,8 @@ public class CityService {
return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class));
}
public List<Integer> selectTodayWeather(Date date){
List<Integer> cityIdList=jdbcTemplate.queryForList(SQL_SELECT_WEATHER,new Object[]{date},Integer.class);
public List<Integer> selectTodayWeather(Date date) {
List<Integer> cityIdList = jdbcTemplate.queryForList(SQL_SELECT_WEATHER, new Object[]{date}, Integer.class);
return cityIdList;
}
......
......@@ -29,8 +29,6 @@ import org.springframework.web.client.RestTemplate;
@Service
public class JsonService {
@Resource
private JdbcTemplate jdbcTemplate;
@Autowired
private Json jsonclass;
......@@ -50,11 +48,11 @@ public class JsonService {
mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class);
SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage,weatherMessage2);
SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2);
return simpleWeather;
}
private String getWeatherJson(int cityId) throws IOException{
private String getWeatherJson(int cityId) throws IOException {
String host = jsonclass.getHost1();
String path = jsonclass.getPath1();
String method = "POST";
......@@ -73,11 +71,11 @@ public class JsonService {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return response.toString();
return EntityUtils.toString(response.getEntity());
}
private String getAqiJson(int cityId) throws IOException{
private String getAqiJson(int cityId) throws IOException {
String host = jsonclass.getHost2();
String path = jsonclass.getPath2();
......@@ -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();
if(data == null){
if (data == null) {
return null;
}
data.getCity();
data.getForecast();
City city = data.getCity();
if(city == null){
if (city == null) {
return null;
}
Forecast forecast = data.getForecast().get(0);
if(forecast == null){
if (forecast == null) {
return null;
}
......@@ -124,9 +122,9 @@ public class JsonService {
simpleWeather.setData_date(forecast.getPredictDate());
simpleWeather.setHtemp(forecast.getTempDay());
simpleWeather.setLtemp(forecast.getTempNight());
if(weatherMessage2.getData().getAqi()==null){
if (weatherMessage2.getData().getAqi() == null) {
simpleWeather.setAqi("0");
}else{
} else {
simpleWeather.setAqi(weatherMessage2.getData().getAqi().getValue());
}
simpleWeather.setFengli(forecast.getWindLevelDay());
......@@ -134,7 +132,8 @@ public class JsonService {
return simpleWeather;
}
private int getWeatherCode(String weather){
private int getWeatherCode(String weather) {
switch (weather) {
case "晴":
case "大部晴朗":
......@@ -203,6 +202,6 @@ public class JsonService {
default:
return -1;
}
}
}
}
......@@ -31,21 +31,21 @@ public class WeatherService {
public void getWeatherInfo() throws IOException {
Date now = new Date();
LocalDate localDate=now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Date date=java.sql.Date.valueOf(localDate);
long s=System.currentTimeMillis();
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+"天气服务调用开始");
List<Mall> mallList=cityService.selectMallList();
List<City> cityList=cityService.selectCityList(mallList);
List<Integer> cityIds=cityService.selectTodayWeather(date);
if(cityList != null) {
System.out.println(date + "天气服务调用开始");
List<Mall> mallList = cityService.selectMallList();
List<City> cityList = cityService.selectCityList(mallList);
List<Integer> cityIds = cityService.selectTodayWeather(date);
if (cityList != null) {
for (City bcity : cityList) {
if(cityIds.contains(bcity.getCityId())){
System.out.println(""+bcity.getCityName()+"天气已经存在");
if (cityIds.contains(bcity.getCityId())) {
System.out.println("" + bcity.getCityName() + "天气已经存在");
continue;
}
Integer weatherCode= null;
Integer weatherCode = null;
try {
if (bcity.getWeatherCode() == null) {
continue;
......@@ -54,8 +54,8 @@ public class WeatherService {
} catch (NumberFormatException e) {
continue;
}
SimpleWeather simpleWeather= jsonService.getWeatherInfo(weatherCode.intValue());
if(simpleWeather == null){
SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode.intValue());
if (simpleWeather == null) {
System.out.println(weatherCode.intValue());
}
......@@ -68,9 +68,9 @@ public class WeatherService {
Integer.parseInt(simpleWeather.getLtemp()),
Integer.parseInt(simpleWeather.getHtemp()),
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("天气服务调用结束");
......
......@@ -163,6 +163,7 @@ public class HttpUtils {
/**
* Put String
*
* @param host
* @param path
* @param method
......@@ -193,6 +194,7 @@ public class HttpUtils {
/**
* Put stream
*
* @param host
* @param path
* @param method
......@@ -293,14 +295,16 @@ public class HttpUtils {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(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);
// ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
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!