Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
胡立强
/
VVAS-Weather
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 1ba251dd
authored
Aug 04, 2021
by
HlQ
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[add]:添加手动调用天气服务接口(插入数据库当天的天气)
[change]:定时任务在配置文件在可配置
1 parent
c5a4e904
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
83 additions
and
27 deletions
pom.xml
src/main/java/com/viontech/Application.java
src/main/java/com/viontech/controller/WeatherController.java
src/main/java/com/viontech/job/SchedulerTask.java
src/main/java/com/viontech/service/impl/CityService.java
src/main/java/com/viontech/service/impl/JsonService.java
src/main/java/com/viontech/service/impl/WeatherService.java
src/main/java/com/viontech/utils/HttpUtils.java
src/main/resources/application.yml
pom.xml
View file @
1ba251d
...
...
@@ -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>
...
...
src/main/java/com/viontech/Application.java
View file @
1ba251d
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
);
}
}
src/main/java/com/viontech/controller/WeatherController.java
0 → 100644
View file @
1ba251d
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
"手动调用成功"
;
}
}
src/main/java/com/viontech/job/SchedulerTask.java
View file @
1ba251d
...
...
@@ -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
();
}
...
...
src/main/java/com/viontech/service/impl/CityService.java
View file @
1ba251d
...
...
@@ -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
));
}
...
...
src/main/java/com/viontech/service/impl/JsonService.java
View file @
1ba251d
...
...
@@ -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
());
...
...
src/main/java/com/viontech/service/impl/WeatherService.java
View file @
1ba251d
...
...
@@ -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
(
...
...
src/main/java/com/viontech/utils/HttpUtils.java
View file @
1ba251d
...
...
@@ -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.lang
3
.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
)
{
}
...
...
src/main/resources/application.yml
View file @
1ba251d
json
:
host1
:
http://aliv1
4
.data.moji.com
host1
:
http://aliv1
3
.data.moji.com
path1
:
/whapi/json/alicityweather/briefforecast6days
host2
:
http://aliv1
4
.data.moji.com
host2
:
http://aliv1
3
.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
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment