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 ad71b23a
authored
Nov 11, 2021
by
袁津
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[chg]增加天气接口调用失败后三次重试机制
1 parent
8e47d0f6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
65 deletions
pom.xml
src/main/java/com/viontech/service/impl/JsonService.java
src/main/java/com/viontech/service/impl/WeatherService.java
pom.xml
View file @
ad71b23
...
...
@@ -59,6 +59,11 @@
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context-support
</artifactId>
</dependency>
<dependency>
<groupId>
com.github.rholder
</groupId>
<artifactId>
guava-retrying
</artifactId>
<version>
2.0.0
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/viontech/service/impl/JsonService.java
View file @
ad71b23
package
com
.
viontech
.
service
.
impl
;
import
java.io.IOException
;
import
java.text.SimpleDateFormat
;
import
java.util.HashMap
;
import
java.util.Map
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.github.rholder.retry.*
;
import
com.google.common.base.Predicates
;
import
com.viontech.model.*
;
import
com.viontech.utils.HttpUtils
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.util.EntityUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.viontech.utils.HttpUtils
;
import
org.springframework.stereotype.Service
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.TimeUnit
;
/**
* @author zhaibolin
*/
...
...
@@ -25,76 +26,119 @@ import org.springframework.stereotype.Service;
@Service
public
class
JsonService
{
private
static
final
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss.SSS"
);
@Autowired
private
Json
jsonclass
;
public
SimpleWeather
getWeatherInfo
(
int
cityId
)
throws
IOException
{
String
json
=
getWeatherJson
(
cityId
);
try
{
public
SimpleWeather
getWeatherInfo
(
int
cityId
)
{
System
.
out
.
println
(
"城市["
+
cityId
+
"]获取天气信息"
);
Map
<
String
,
WeatherMessage
>
dataMap
=
new
HashMap
<>();
// 调用weather接口
Callable
<
Boolean
>
weatherCallable
=
()
->
{
String
host
=
jsonclass
.
getHost1
();
String
path
=
jsonclass
.
getPath1
();
String
method
=
"POST"
;
String
appcode
=
jsonclass
.
getAppcode
();
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"Authorization"
,
"APPCODE "
+
appcode
);
headers
.
put
(
"Content-Type"
,
"application/x-www-form-urlencoded; charset=UTF-8"
);
Map
<
String
,
String
>
querys
=
new
HashMap
<>();
Map
<
String
,
String
>
bodys
=
new
HashMap
<>();
bodys
.
put
(
"cityId"
,
String
.
valueOf
(
cityId
));
HttpResponse
response
=
HttpUtils
.
doPost
(
host
,
path
,
method
,
headers
,
querys
,
bodys
);
String
json
=
EntityUtils
.
toString
(
response
.
getEntity
());
System
.
out
.
println
(
sdf
.
format
(
new
Date
())
+
"调用weather接口返回json:"
+
json
);
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
);
String
jsons
=
getAqiJson
(
cityId
);
ObjectMapper
mappers
=
new
ObjectMapper
();
mappers
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
mappers
.
setDateFormat
(
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
));
WeatherMessage
weatherMessage2
=
mappers
.
readValue
(
jsons
,
WeatherMessage
.
class
);
SimpleWeather
simpleWeather
=
weathers2SimpleWeather
(
weatherMessage
,
weatherMessage2
);
return
simpleWeather
;
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"json解析出错!"
);
System
.
out
.
println
(
json
);
}
return
null
;
}
if
(
null
!=
weatherMessage
)
{
dataMap
.
put
(
"WEATHER"
,
weatherMessage
);
return
true
;
}
return
false
;
};
Retryer
<
Boolean
>
weatherRetryer
=
RetryerBuilder
.<
Boolean
>
newBuilder
()
// 发生异常时重试
.
retryIfException
()
// 返回false时重试
.
retryIfResult
(
Predicates
.
equalTo
(
false
))
// 每次重试等待时间
.
withWaitStrategy
(
WaitStrategies
.
fixedWait
(
3
,
TimeUnit
.
SECONDS
))
// 最大重试次数
.
withStopStrategy
(
StopStrategies
.
stopAfterAttempt
(
3
))
// 记录第几次执行
.
withRetryListener
(
new
RetryListener
()
{
@Override
public
<
V
>
void
onRetry
(
Attempt
<
V
>
attempt
)
{
System
.
out
.
println
(
"[weather retry]times="
+
attempt
.
getAttemptNumber
());
}
})
.
build
();
private
String
getWeatherJson
(
int
cityId
)
throws
IOException
{
String
host
=
jsonclass
.
getHost1
();
String
path
=
jsonclass
.
getPath1
();
String
method
=
"POST"
;
String
appcode
=
jsonclass
.
getAppcode
();
Map
<
String
,
String
>
headers
=
new
HashMap
<
String
,
String
>();
headers
.
put
(
"Authorization"
,
"APPCODE "
+
appcode
);
headers
.
put
(
"Content-Type"
,
"application/x-www-form-urlencoded; charset=UTF-8"
);
Map
<
String
,
String
>
querys
=
new
HashMap
<
String
,
String
>();
Map
<
String
,
String
>
bodys
=
new
HashMap
<
String
,
String
>();
bodys
.
put
(
"cityId"
,
String
.
valueOf
(
cityId
));
HttpResponse
response
=
null
;
try
{
response
=
HttpUtils
.
doPost
(
host
,
path
,
method
,
headers
,
querys
,
bodys
);
weatherRetryer
.
call
(
weatherCallable
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"城市["
+
cityId
+
"]调用Weather接口失败"
);
return
null
;
}
return
EntityUtils
.
toString
(
response
.
getEntity
());
}
private
String
getAqiJson
(
int
cityId
)
throws
IOException
{
String
host
=
jsonclass
.
getHost2
();
String
path
=
jsonclass
.
getPath2
();
String
method
=
"POST"
;
String
appcode
=
jsonclass
.
getAppcode
();
Map
<
String
,
String
>
headers
=
new
HashMap
<
String
,
String
>();
headers
.
put
(
"Authorization"
,
"APPCODE "
+
appcode
);
headers
.
put
(
"Content-Type"
,
"application/x-www-form-urlencoded; charset=UTF-8"
);
Map
<
String
,
String
>
querys
=
new
HashMap
<
String
,
String
>();
Map
<
String
,
String
>
bodys
=
new
HashMap
<
String
,
String
>();
bodys
.
put
(
"cityId"
,
String
.
valueOf
(
cityId
));
// 调用aqi接口
Callable
<
Boolean
>
aqiCallable
=
()
->
{
String
host
=
jsonclass
.
getHost2
();
String
path
=
jsonclass
.
getPath2
();
String
method
=
"POST"
;
String
appcode
=
jsonclass
.
getAppcode
();
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"Authorization"
,
"APPCODE "
+
appcode
);
headers
.
put
(
"Content-Type"
,
"application/x-www-form-urlencoded; charset=UTF-8"
);
Map
<
String
,
String
>
querys
=
new
HashMap
<>();
Map
<
String
,
String
>
bodys
=
new
HashMap
<>();
bodys
.
put
(
"cityId"
,
String
.
valueOf
(
cityId
));
HttpResponse
response
=
HttpUtils
.
doPost
(
host
,
path
,
method
,
headers
,
querys
,
bodys
);
String
json
=
EntityUtils
.
toString
(
response
.
getEntity
());
System
.
out
.
println
(
sdf
.
format
(
new
Date
())
+
"调用AQI接口返回json:"
+
json
);
ObjectMapper
mapper
=
new
ObjectMapper
();
mapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
mapper
.
setDateFormat
(
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
));
WeatherMessage
aqiMessage
=
mapper
.
readValue
(
json
,
WeatherMessage
.
class
);
if
(
null
!=
aqiMessage
)
{
dataMap
.
put
(
"AQI"
,
aqiMessage
);
return
true
;
}
return
false
;
};
Retryer
<
Boolean
>
aqiRetryer
=
RetryerBuilder
.<
Boolean
>
newBuilder
()
// 发生异常时重试
.
retryIfException
()
// 返回false时重试
.
retryIfResult
(
Predicates
.
equalTo
(
false
))
// 每次重试等待时间
.
withWaitStrategy
(
WaitStrategies
.
fixedWait
(
3
,
TimeUnit
.
SECONDS
))
// 最大重试次数
.
withStopStrategy
(
StopStrategies
.
stopAfterAttempt
(
3
))
// 记录第几次执行
.
withRetryListener
(
new
RetryListener
()
{
@Override
public
<
V
>
void
onRetry
(
Attempt
<
V
>
attempt
)
{
System
.
out
.
println
(
"[aqi retry]times="
+
attempt
.
getAttemptNumber
());
}
})
.
build
();
HttpResponse
response
=
null
;
try
{
response
=
HttpUtils
.
doPost
(
host
,
path
,
method
,
headers
,
querys
,
bodys
);
aqiRetryer
.
call
(
aqiCallable
);
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
()
;
System
.
out
.
println
(
"城市["
+
cityId
+
"]调用AQI接口失败"
);
return
null
;
}
// return response.toString();
return
EntityUtils
.
toString
(
response
.
getEntity
());
return
weathers2SimpleWeather
(
dataMap
.
get
(
"WEATHER"
),
dataMap
.
get
(
"AQI"
));
}
private
SimpleWeather
weathers2SimpleWeather
(
WeatherMessage
weatherMessage
,
WeatherMessage
weatherMessage2
)
{
...
...
src/main/java/com/viontech/service/impl/WeatherService.java
View file @
ad71b23
...
...
@@ -56,7 +56,7 @@ public class WeatherService {
}
SimpleWeather
simpleWeather
=
jsonService
.
getWeatherInfo
(
weatherCode
);
if
(
simpleWeather
==
null
)
{
System
.
out
.
println
(
weatherCode
);
//
System.out.println(weatherCode);
continue
;
}
...
...
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