Commit 1ba251dd by HlQ

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

[change]:定时任务在配置文件在可配置
1 parent c5a4e904
......@@ -45,6 +45,11 @@
<version>42.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
......
package com.viontech;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -8,11 +10,19 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.viontech.*"})
@EnableScheduling
public class Application {
public class Application implements CommandLineRunner {
@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 {
@Autowired
private WeatherService weatherService;
@Scheduled(cron = "0 30 5 * * ? ")
@Scheduled(cron = "${schedules}")
private void process() throws IOException {
weatherService.getWeatherInfo();
}
......
......@@ -34,10 +34,10 @@ public class CityService {
public List<City> selectCityList(List<Mall> mallList) {
if (mallList.isEmpty()) {
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());
Map<String, Object> paramMap = new HashMap<>();
Map<String, Object> paramMap = new HashMap<>(1);
paramMap.put("cityIds", cityIdList);
return namedParameterJdbcTemplate.query(SQL_SELECT_CITY, paramMap, new BeanPropertyRowMapper<>(City.class));
}
......
......@@ -29,15 +29,12 @@ public class JsonService {
private Json jsonclass;
public SimpleWeather getWeatherInfo(int cityId) throws IOException {
String json = getWeatherJson(cityId);
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
WeatherMessage weatherMessage = mapper.readValue(json, WeatherMessage.class);
// if(!weatherMessage.getCode().equals("0")){
// throw new RuntimeException(weatherMessage.getMsg());
// }
String jsons = getAqiJson(cityId);
ObjectMapper mappers = new ObjectMapper();
mappers.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
......@@ -46,6 +43,11 @@ public class JsonService {
SimpleWeather simpleWeather = weathers2SimpleWeather(weatherMessage, weatherMessage2);
return simpleWeather;
} catch (IOException e) {
System.out.println("json解析出错!");
System.out.println(json);
}
return null;
}
private String getWeatherJson(int cityId) throws IOException {
......@@ -64,7 +66,6 @@ public class JsonService {
try {
response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EntityUtils.toString(response.getEntity());
......
......@@ -6,13 +6,17 @@ import com.viontech.model.SimpleWeather;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author zhaibolin
......@@ -30,16 +34,12 @@ public class WeatherService {
private CityService cityService;
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();
System.out.println(new Timestamp(s));
System.out.println(date + "天气服务调用开始");
Date date = java.sql.Date.valueOf(LocalDate.now());
System.out.println(LocalDateTime.now() + "天气服务调用开始");
List<Mall> mallList = cityService.selectMallList();
List<City> cityList = cityService.selectCityList(mallList);
List<Integer> cityIds = cityService.selectTodayWeather(date);
if (cityList != null) {
Set<Integer> cityIds = new HashSet<>(cityService.selectTodayWeather(date));
if (!cityList.isEmpty()) {
for (City bcity : cityList) {
if (cityIds.contains(bcity.getCityId())) {
System.out.println("" + bcity.getCityName() + "天气已经存在");
......@@ -54,9 +54,9 @@ public class WeatherService {
} catch (NumberFormatException e) {
continue;
}
SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode.intValue());
SimpleWeather simpleWeather = jsonService.getWeatherInfo(weatherCode);
if (simpleWeather == null) {
System.out.println(weatherCode.intValue());
System.out.println(weatherCode);
}
jdbcTemplate.update(
......
......@@ -13,7 +13,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
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.NameValuePair;
import org.apache.http.client.HttpClient;
......@@ -292,14 +292,17 @@ public class HttpUtils {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
......
json:
host1: http://aliv14.data.moji.com
host1: http://aliv13.data.moji.com
path1: /whapi/json/alicityweather/briefforecast6days
host2: http://aliv14.data.moji.com
host2: http://aliv13.data.moji.com
path2: /whapi/json/alicityweather/aqi
appcode: 98261c0416a944aa812174cc312335bf
server:
......@@ -9,9 +9,9 @@ server:
spring:
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://pgm-2ze197c18ro6p1r1fo.pg.rds.aliyuncs.com:3433/ShoppingMall_retail2.0
username: vion
password: cdmqYwBq9uAdvLJb
url: jdbc:postgresql://36.112.68.214:5432/VionCount
username: postgres
password: vion
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
......@@ -30,3 +30,6 @@ spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
schedules:
0 0 8 * * ?
\ 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!