Commit 1ba251dd by HlQ

[add]:添加手动调用天气服务接口(插入数据库当天的天气)

[change]:定时任务在配置文件在可配置
1 parent c5a4e904
...@@ -45,6 +45,11 @@ ...@@ -45,6 +45,11 @@
<version>42.2.2</version> <version>42.2.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
<version>1.0.19</version> <version>1.0.19</version>
......
package com.viontech; package com.viontech;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
...@@ -8,11 +10,19 @@ import org.springframework.scheduling.annotation.EnableScheduling; ...@@ -8,11 +10,19 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.viontech.*"})
@EnableScheduling @EnableScheduling
public class Application { public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); @Value("${schedules}")
} private String schedules;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... strings) throws Exception {
System.out.println("定时任务执行时间:" + schedules);
}
} }
package com.viontech.controller;
import com.viontech.service.impl.WeatherService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author HlQ
* @date 2021/8/4
*/
@RestController
public class WeatherController {
@Resource
private WeatherService weatherService;
@GetMapping("/getWeather")
public Object get() {
System.out.println("手动调用天气");
try {
weatherService.getWeatherInfo();
} catch (IOException e) {
e.printStackTrace();
}
return "手动调用成功";
}
}
...@@ -16,7 +16,7 @@ public class SchedulerTask { ...@@ -16,7 +16,7 @@ public class SchedulerTask {
@Autowired @Autowired
private WeatherService weatherService; private WeatherService weatherService;
@Scheduled(cron = "0 30 5 * * ? ") @Scheduled(cron = "${schedules}")
private void process() throws IOException { private void process() throws IOException {
weatherService.getWeatherInfo(); weatherService.getWeatherInfo();
} }
......
...@@ -34,10 +34,10 @@ public class CityService { ...@@ -34,10 +34,10 @@ public class CityService {
public List<City> selectCityList(List<Mall> mallList) { public List<City> selectCityList(List<Mall> mallList) {
if (mallList.isEmpty()) { if (mallList.isEmpty()) {
System.out.println("获取不到商场列表"); System.out.println("获取不到商场列表");
return null; return Collections.emptyList();
} }
List<Integer> cityIdList = mallList.stream().filter(x -> x.getCity_id() != null).map(Mall::getCity_id).collect(Collectors.toList()); List<Integer> cityIdList = mallList.stream().filter(x -> x.getCity_id() != null).map(Mall::getCity_id).collect(Collectors.toList());
Map<String, Object> paramMap = new HashMap<>(); Map<String, Object> paramMap = new HashMap<>(1);
paramMap.put("cityIds", cityIdList); paramMap.put("cityIds", cityIdList);
return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class)); return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class));
} }
......
...@@ -29,23 +29,25 @@ public class JsonService { ...@@ -29,23 +29,25 @@ public class JsonService {
private Json jsonclass; private Json jsonclass;
public SimpleWeather getWeatherInfo(int cityId) throws IOException { public SimpleWeather getWeatherInfo(int cityId) throws IOException {
String json = getWeatherJson(cityId); String json = getWeatherJson(cityId);
ObjectMapper mapper = new ObjectMapper(); try {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
// if(!weatherMessage.getCode().equals("0")){ WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class);
// throw new RuntimeException(weatherMessage.getMsg()); String jsons = getAqiJson(cityId);
// } ObjectMapper mappers = new ObjectMapper();
String jsons = getAqiJson(cityId); mappers.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ObjectMapper mappers = new ObjectMapper(); mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
mappers.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class);
mappers.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
WeatherMessage weatherMessage2 = mappers.readValue(jsons, WeatherMessage.class); SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2);
return simpleWeather;
SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2); } catch (IOException e) {
return simpleWeather; System.out.println("json解析出错!");
System.out.println(json);
}
return null;
} }
private String getWeatherJson(int cityId) throws IOException { private String getWeatherJson(int cityId) throws IOException {
...@@ -64,7 +66,6 @@ public class JsonService { ...@@ -64,7 +66,6 @@ public class JsonService {
try { try {
response = HttpUtils.doPost(host, path, method, headers, querys, bodys); response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
return EntityUtils.toString(response.getEntity()); return EntityUtils.toString(response.getEntity());
......
...@@ -6,13 +6,17 @@ import com.viontech.model.SimpleWeather; ...@@ -6,13 +6,17 @@ 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.sql.Timestamp;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* @author zhaibolin * @author zhaibolin
...@@ -30,16 +34,12 @@ public class WeatherService { ...@@ -30,16 +34,12 @@ public class WeatherService {
private CityService cityService; private CityService cityService;
public void getWeatherInfo() throws IOException { public void getWeatherInfo() throws IOException {
Date now = new Date(); Date date = java.sql.Date.valueOf(LocalDate.now());
LocalDate localDate = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(LocalDateTime.now() + "天气服务调用开始");
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<Mall> mallList = cityService.selectMallList();
List<City> cityList = cityService.selectCityList(mallList); List<City> cityList = cityService.selectCityList(mallList);
List<Integer> cityIds = cityService.selectTodayWeather(date); Set<Integer> cityIds = new HashSet<>(cityService.selectTodayWeather(date));
if (cityList != null) { if (!cityList.isEmpty()) {
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() + "天气已经存在");
...@@ -54,9 +54,9 @@ public class WeatherService { ...@@ -54,9 +54,9 @@ public class WeatherService {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
continue; continue;
} }
SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode.intValue()); SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode);
if (simpleWeather == null) { if (simpleWeather == null) {
System.out.println(weatherCode.intValue()); System.out.println(weatherCode);
} }
jdbcTemplate.update( jdbcTemplate.update(
......
...@@ -13,7 +13,7 @@ import javax.net.ssl.SSLContext; ...@@ -13,7 +13,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
...@@ -292,14 +292,17 @@ public class HttpUtils { ...@@ -292,14 +292,17 @@ public class HttpUtils {
try { try {
SSLContext ctx = SSLContext.getInstance("TLS"); SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() { X509TrustManager tm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() { public X509Certificate[] getAcceptedIssuers() {
return null; return null;
} }
@Override
public void checkClientTrusted(X509Certificate[] xcs, String str) { public void checkClientTrusted(X509Certificate[] xcs, String str) {
} }
@Override
public void checkServerTrusted(X509Certificate[] xcs, String str) { public void checkServerTrusted(X509Certificate[] xcs, String str) {
} }
......
json: json:
host1: http://aliv14.data.moji.com host1: http://aliv13.data.moji.com
path1: /whapi/json/alicityweather/briefforecast6days path1: /whapi/json/alicityweather/briefforecast6days
host2: http://aliv14.data.moji.com host2: http://aliv13.data.moji.com
path2: /whapi/json/alicityweather/aqi path2: /whapi/json/alicityweather/aqi
appcode: 98261c0416a944aa812174cc312335bf appcode: 98261c0416a944aa812174cc312335bf
server: server:
...@@ -9,9 +9,9 @@ server: ...@@ -9,9 +9,9 @@ server:
spring: spring:
datasource: datasource:
driverClassName: org.postgresql.Driver driverClassName: org.postgresql.Driver
url: jdbc:postgresql://pgm-2ze197c18ro6p1r1fo.pg.rds.aliyuncs.com:3433/ShoppingMall_retail2.0 url: jdbc:postgresql://36.112.68.214:5432/VionCount
username: vion username: postgres
password: cdmqYwBq9uAdvLJb password: vion
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5 initialSize: 5
minIdle: 5 minIdle: 5
...@@ -29,4 +29,7 @@ spring: ...@@ -29,4 +29,7 @@ spring:
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
jackson: jackson:
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
\ No newline at end of file \ No newline at end of file
time-zone: GMT+8
schedules:
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!