Commit 66f352a4 by HlQ

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

1 parent 2c49603c
......@@ -14,96 +14,96 @@ import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DruidDBConfiguration {
@Configuration
public class DruidDBConfiguration {
private Logger logger = LoggerFactory.getLogger(DruidDBConfiguration.class);
@Value("${spring.datasource.url:127.0.0.1}")
private String dbUrl;
@Value("${spring.datasource.username:postgresql}")
private String username;
@Value("${spring.datasource.password:password}")
private String password;
@Value("${spring.datasource.driverClassName:driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize:0}")
private int initialSize;
@Value("${spring.datasource.minIdle:0}")
private int minIdle;
@Value("${spring.datasource.maxActive:1}")
private int maxActive;
@Value("${spring.datasource.maxWait:0}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis:0}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis:0}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery:0}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle:false}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow:false}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn:false}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements:false}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize:0}")
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
@Value("${spring.datasource.connectionProperties:connectionProperties}")
private String connectionProperties;
@Value("${spring.datasource.url:127.0.0.1}")
private String dbUrl;
@Value("${spring.datasource.username:postgresql}")
private String username;
@Value("${spring.datasource.password:password}")
private String password;
@Value("${spring.datasource.driverClassName:driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize:0}")
private int initialSize;
@Value("${spring.datasource.minIdle:0}")
private int minIdle;
@Value("${spring.datasource.maxActive:1}")
private int maxActive;
@Value("${spring.datasource.maxWait:0}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis:0}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis:0}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery:0}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle:false}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow:false}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn:false}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements:false}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize:0}")
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
@Value("${spring.datasource.connectionProperties:connectionProperties}")
private String connectionProperties;
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
@ConditionalOnExpression("#{'${spring.datasource.driverClassName:false}'!='false'}")
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
if("driverClassName".equals(driverClassName)){
return null;
}
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
if ("driverClassName".equals(driverClassName)) {
return null;
}
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
\ No newline at end of file
......@@ -25,77 +25,15 @@ import com.viontech.service.impl.JsonService;
* @author zhaibolin
*/
@Component
public class SchedulerTask implements CommandLineRunner{
public class SchedulerTask {
@Autowired
private JdbcTemplate jdbcTemplate;
private WeatherService weatherService;
@Autowired
private JsonService jsonService;
@Autowired
private CityService cityService;
@Autowired
private WeatherService weatherService;
@Scheduled(cron="0 0 8 * * ?")
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("天气服务调用结束");
@Scheduled(cron = "0 30 5 * * ? ")
private void process() throws IOException {
weatherService.getWeatherInfo();
}
//@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
process();
}
}
\ No newline at end of file
......@@ -20,23 +20,21 @@ import java.util.stream.Collectors;
public class CityService {
@Autowired
private JdbcTemplate jdbcTemplate;
private JdbcTemplate jdbcTemplate;
@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;
}
......
......@@ -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("天气服务调用结束");
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!