Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
windows
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhanhai
windows
Commits
c90453d7
Commit
c90453d7
authored
3 weeks ago
by
LSL
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
推送天思
parent
d9f217f0
Changes
14
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
1969 additions
and
0 deletions
+1969
-0
SyncBuildComponent.java
...n/java/com/jqtx/windows/component/SyncBuildComponent.java
+12
-0
Contant.java
src/main/java/com/jqtx/windows/component/config/Contant.java
+41
-0
FeignUrlConfig.java
...ava/com/jqtx/windows/component/config/FeignUrlConfig.java
+22
-0
SyncBuildComponentImpl.java
...m/jqtx/windows/component/impl/SyncBuildComponentImpl.java
+119
-0
SyncRetryJob.java
src/main/java/com/jqtx/windows/job/SyncRetryJob.java
+77
-0
WindowsRetry.java
...java/com/jqtx/windows/repository/entity/WindowsRetry.java
+96
-0
WindowsRetryExample.java
...m/jqtx/windows/repository/entity/WindowsRetryExample.java
+791
-0
WindowsRetryMapper.java
...om/jqtx/windows/repository/mapper/WindowsRetryMapper.java
+30
-0
SyncDataService.java
src/main/java/com/jqtx/windows/service/SyncDataService.java
+21
-0
SyncDataServiceImpl.java
...va/com/jqtx/windows/service/impl/SyncDataServiceImpl.java
+35
-0
DateUtils.java
src/main/java/com/jqtx/windows/utils/DateUtils.java
+17
-0
HttpClientPools.java
...ain/java/com/jqtx/windows/utils/http/HttpClientPools.java
+242
-0
HttpUtil.java
src/main/java/com/jqtx/windows/utils/http/HttpUtil.java
+191
-0
WindowsRetryMapper.xml
src/main/resources/mybatis/mapper/WindowsRetryMapper.xml
+275
-0
No files found.
src/main/java/com/jqtx/windows/component/SyncBuildComponent.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
component
;
import
com.jqtx.windows.component.tsmodel.AddCaseRequest
;
public
interface
SyncBuildComponent
{
/**
* 分装提醒推送
* @param planNo
* @return
*/
AddCaseRequest
buildAddCaseRequest
(
String
planNo
);
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/component/config/Contant.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
component
.
config
;
/**
* @program: asgard
* @description:
* @author: zq
* @create: 2019-12-17 20:23
**/
public
class
Contant
{
/**
* 提醒推送url
*/
public
static
final
String
REMIND_URL
=
"/qtong/public/case/add"
;
/**
* 结果推送url
*/
public
static
final
String
RESULT_URL
=
"/qtong/public/repaymentIntention/sync"
;
/**
* 逾期还款
*/
public
static
final
String
OVER_BACKING_URL
=
"/qtong/public/repayRecord/add"
;
/**
* 新增提前结清数据
*/
public
static
final
String
BACKING_URL
=
"/qtong/public/synAdvanceSettle/add"
;
/**
* 逾期金额增加
*/
public
static
final
String
OVER_AMT_ADD
=
"/qtong/public/caseOverdue/sync"
;
public
static
final
String
FEED_URL
=
"/qtong/public/uploadCase/addAttachments"
;
public
static
final
String
REFUND
=
"/qtong/public/repayRecord/add"
;
public
static
final
String
REPAYLOG
=
"/qtong/public/repayLog/add"
;
public
static
final
String
GETFOLLOWCLIENT
=
"/qtong/public/case/queryCollectionCompanyByLoanNo"
;
public
static
final
String
QUERYCLIENT
=
"/qtong/public/case/queryClient"
;
public
static
final
String
RISK
=
"/qtong/public/uploadCase/uploadRisk"
;
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/component/config/FeignUrlConfig.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
component
.
config
;
import
lombok.Data
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
/**
* @Description:
* @Author: liyongen
* @Date: 2019/7/16 20:45
*/
@Data
@Configuration
@ConfigurationProperties
(
prefix
=
"tiansicase"
)
public
class
FeignUrlConfig
{
/** 天思 **/
private
String
url
;
private
String
secretKey
;
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/component/impl/SyncBuildComponentImpl.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
component
.
impl
;
import
com.alibaba.fastjson2.JSONObject
;
import
com.jqtx.infrastructure.util.LoanMobileAESUtil
;
import
com.jqtx.infrastructure.util.SecurityUtils
;
import
com.jqtx.windows.component.LoanPlanComponent
;
import
com.jqtx.windows.component.SyncBuildComponent
;
import
com.jqtx.windows.component.model.CreditModel
;
import
com.jqtx.windows.component.model.LoanModel
;
import
com.jqtx.windows.component.model.LoanPlanModel
;
import
com.jqtx.windows.component.tsmodel.AddCaseRequest
;
import
com.jqtx.windows.component.tsmodel.ContactRequest
;
import
com.jqtx.windows.component.tsmodel.SimpleAttachRequest
;
import
com.jqtx.windows.component.tsmodel.UrgentContactRequest
;
import
com.jqtx.windows.utils.DateUtils
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.List
;
@Service
public
class
SyncBuildComponentImpl
implements
SyncBuildComponent
{
@Value
(
"${tiansi.bizChannel}"
)
private
Long
bizChannel
;
@Autowired
private
LoanPlanComponent
loanPlanComponent
;
@Autowired
private
LoanComponentImpl
loanComponent
;
@Autowired
private
CreditComponentImpl
creditComponent
;
@Override
public
AddCaseRequest
buildAddCaseRequest
(
String
planNo
)
{
//
LoanPlanModel
planRemindModel
=
loanPlanComponent
.
getPlanNoByPlanNo
(
planNo
);
if
(
planRemindModel
==
null
)
{
return
null
;
}
//借款信息
LoanModel
loanModel
=
loanComponent
.
getByLoanNo
(
planRemindModel
.
getLoanNo
());
//查询用户信息
CreditModel
creditModel
=
creditComponent
.
getByOrderNo
(
loanModel
.
getOrderNo
());
String
bizType
=
"ZN"
;
AddCaseRequest
request
=
new
AddCaseRequest
();
request
.
setBizChannel
(
bizChannel
);
/** 客户基础信息 */
request
.
setClientName
(
SecurityUtils
.
decryptAES
(
creditModel
.
getClientName
()));
request
.
setClientIdCard
(
SecurityUtils
.
decryptAES
(
creditModel
.
getCertNo
()));
request
.
setClientMobile
(
LoanMobileAESUtil
.
decrypt
(
creditModel
.
getClientCell
()));
/** 案件基础信息 */
request
.
setLoanBankCard
(
""
);
/** 还款卡号 */
request
.
setLoanName
(
"小花钱包-浙农项目"
);
//获取配置
request
.
setProductTypeName
(
"小花钱包-浙农项目"
);
/** 产品类型名称*/
request
.
setProductTypeCode
(
"XIAO_ZN"
);
/** 产品类型 */
request
.
setLoanNo
(
loanModel
.
getLoanNo
());
/** 借款编号 */
request
.
setLoanCaseNo
(
planNo
);
/** 委托方案件编号 */
request
.
setLoanDate
(
DateUtils
.
toDate
(
loanModel
.
getRaiseDate
()));
/** 放款日期 */
request
.
setLoanBackDate
(
DateUtils
.
toDate
(
planRemindModel
.
getPlanDate
()));
/** 还款日期 */
request
.
setLoanTotalPeriod
(
Integer
.
parseInt
(
loanModel
.
getLoanTerm
()));
/** 总期数 */
request
.
setLoanLeftPeriod
(
Integer
.
parseInt
(
loanModel
.
getLoanTerm
())
-
planRemindModel
.
getPlanIndex
());
/** 剩余期数 */
request
.
setLoanCurrentPeriod
(
planRemindModel
.
getPlanIndex
());
/** 当前期数 */
request
.
setLoanCurrentPeriodDays
(
30
);
/** 当期天数 */
request
.
setLoanDelegateTimes
(
1
);
/** 委外手数 */
BigDecimal
amt
=
planRemindModel
.
getPlanAmt
().
add
(
planRemindModel
.
getPlanFa
()).
subtract
(
planRemindModel
.
getBackedBen
()).
subtract
(
planRemindModel
.
getBackedFu
()).
subtract
(
planRemindModel
.
getBackedXi
()).
subtract
(
planRemindModel
.
getBackedFa
())
.
subtract
(
planRemindModel
.
getBackedBurden
()).
subtract
(
planRemindModel
.
getBackedSpeaccount
());
request
.
setLoanDelegateAmt
(
amt
);
/** 委案金额 */
request
.
setLoanTotalAmt
(
loanModel
.
getRaiseAmt
());
/** 本次借款总金额 */
/** 合计金额 */
BigDecimal
backedAmt
=
loanPlanComponent
.
countBackedAmtByLoanNo
(
loanModel
.
getLoanNo
());
request
.
setTotalShouldRepay
(
loanModel
.
getLoanAmt
());
/** 全部应还金额*/
request
.
setTotalBackedRepay
(
backedAmt
);
/** 全部已还金额*/
request
.
setTotalUnBackedRepay
(
loanModel
.
getLoanAmt
().
subtract
(
backedAmt
));
/** 全部未还金额*/
// request.setCapitalTotalShouldRepay(loan.getLoanAmt());/** 全部应还本金*/
// request.setCapitalTotalBackedRepay(loanCollect.getPeriodAmt());/** 全部已还本金*/
request
.
setCapitalPeriodShouldRepay
(
planRemindModel
.
getPlanBen
());
/** 当期应还本金*/
request
.
setCapitalPeriodBackedRepay
(
planRemindModel
.
getBackedBen
());
/** 当期已还本金*/
// request.setInterestTotalShouldRepay(loan.getRateAmt());/** 全部应还利息*/
// request.setInterestTotalBackedRepay(BigDecimal.ZERO);/** 全部已还利息*/
request
.
setInterestPeriodShouldRepay
(
planRemindModel
.
getPlanXi
());
/** 当期应还利息*/
request
.
setInterestPeriodBackedRepay
(
planRemindModel
.
getBackedXi
());
/** 当期已还利息*/
// request.setFeeTotalShouldRepay(loan.getServiceAmt());/** 全部应还服务费*/
// request.setFeeTotalBackedRepay(BigDecimal.ZERO);/** 全部已还服务费*/
BigDecimal
FuAmt
=
planRemindModel
.
getPlanFu
().
add
(
planRemindModel
.
getPlanBurden
()).
add
(
planRemindModel
.
getPlanSpeaccount
());
request
.
setFeePeriodShouldRepay
(
FuAmt
);
/** 当期应还服务费*/
BigDecimal
backFu
=
planRemindModel
.
getBackedFu
().
add
(
planRemindModel
.
getBackedBurden
()).
add
(
planRemindModel
.
getBackedSpeaccount
());
request
.
setFeePeriodBackedRepay
(
backFu
);
/** 当期已还服务费*/
// request.setPunishTotalShouldRepay(BigDecimal.ZERO);/** 全部应还罚息*/
// request.setPunishTotalBackedRepay(BigDecimal.ZERO);/** 全部已还罚息*/
request
.
setPunishPeriodShouldRepay
(
planRemindModel
.
getPlanFa
());
/** 当期应还罚息*/
request
.
setPunishPeriodBackedRepay
(
planRemindModel
.
getBackedFa
());
/** 当期已还罚息*/
/** 通讯录 */
List
<
ContactRequest
>
contactRequests
=
new
ArrayList
<>();
ContactRequest
contactRequest
=
new
ContactRequest
();
contactRequest
.
setContactMobile
(
LoanMobileAESUtil
.
decrypt
(
creditModel
.
getClientCell
()));
contactRequest
.
setContactName
(
SecurityUtils
.
decryptAES
(
creditModel
.
getClientName
()));
contactRequest
.
setRelation
(
"无"
);
contactRequests
.
add
(
contactRequest
);
request
.
setContactRequests
(
contactRequests
);
/** 附件处理*/
List
<
SimpleAttachRequest
>
attachRequests
=
new
ArrayList
<>();
request
.
setAttachments
(
attachRequests
);
request
.
setMemo
(
"小花钱包-浙农项目"
);
request
.
setFundChannel
(
"小花钱包-浙农项目"
);
return
request
;
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/job/SyncRetryJob.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
job
;
import
com.alibaba.fastjson2.JSON
;
import
com.jqtx.windows.component.LoanPlanComponent
;
import
com.jqtx.windows.component.SyncBuildComponent
;
import
com.jqtx.windows.component.config.Contant
;
import
com.jqtx.windows.component.config.FeignUrlConfig
;
import
com.jqtx.windows.component.model.LoanPlanModel
;
import
com.jqtx.windows.component.tsmodel.AddCaseRequest
;
import
com.jqtx.windows.service.SyncDataService
;
import
com.jqtx.windows.web.response.JsonResult
;
import
com.xxl.job.core.context.XxlJobHelper
;
import
com.xxl.job.core.handler.annotation.XxlJob
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.text.MessageFormat
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.List
;
/**
* @Description: 重新推送案件(重试机制推送)
* @Date: 2019/12/04 10:55
*/
@Component
@Slf4j
public
class
SyncRetryJob
{
@Autowired
private
FeignUrlConfig
feignUrlConfig
;
@Autowired
private
SyncDataService
syncDataService
;
@Autowired
private
LoanPlanComponent
loanPlanComponent
;
@Autowired
private
SyncBuildComponent
syncBuildComponent
;
@XxlJob
(
value
=
"syncRetryJobHandler"
)
private
void
process
()
{
String
param
=
XxlJobHelper
.
getJobParam
();
try
{
LocalDateTime
executeStartTime
=
LocalDateTime
.
now
();
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
);
log
.
info
(
"任务开始时间-[{}]"
,
executeStartTime
.
format
(
formatter
));
log
.
info
(
"需要重新推送案件处理开始..."
);
Long
li1
=
System
.
currentTimeMillis
();
List
<
LoanPlanModel
>
list
=
loanPlanComponent
.
getByLoan
(
param
);
for
(
LoanPlanModel
planModel
:
list
)
{
sendTs
(
syncBuildComponent
.
buildAddCaseRequest
(
planModel
.
getPlanNo
()));
}
Long
li2
=
System
.
currentTimeMillis
();
log
.
info
(
"任务结束时间-[{}],执行-[{}]"
,
executeStartTime
.
format
(
formatter
),
li2
-
li1
);
}
catch
(
Exception
e
)
{
log
.
error
(
MessageFormat
.
format
(
"需要重新推送案件处理异常:{0}"
,
e
.
getMessage
()),
e
);
}
log
.
info
(
"需要重新推送案件处理结束..."
);
}
public
Boolean
sendTs
(
AddCaseRequest
addCaseRequest
)
{
String
url
=
feignUrlConfig
.
getUrl
()
+
Contant
.
REMIND_URL
;
String
body
=
com
.
alibaba
.
fastjson
.
JSON
.
toJSONString
(
addCaseRequest
);
log
.
info
(
"同步提醒案件到天思请求:{}"
,
body
);
JsonResult
<
String
>
synResult
=
syncDataService
.
synResult
(
url
,
body
);
log
.
info
(
"同步提醒案件天思返回:{}"
,
synResult
);
if
(
synResult
.
isSuccess
())
{
return
true
;
}
return
false
;
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/repository/entity/WindowsRetry.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
repository
.
entity
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
public
class
WindowsRetry
{
private
Integer
id
;
private
String
bizType
;
private
String
bizCode
;
private
String
bizState
;
private
LocalDate
pushDate
;
private
LocalDateTime
gmtCreated
;
private
LocalDateTime
gmtModified
;
private
String
memo
;
private
String
repayType
;
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
public
String
getBizType
()
{
return
bizType
;
}
public
void
setBizType
(
String
bizType
)
{
this
.
bizType
=
bizType
==
null
?
null
:
bizType
.
trim
();
}
public
String
getBizCode
()
{
return
bizCode
;
}
public
void
setBizCode
(
String
bizCode
)
{
this
.
bizCode
=
bizCode
==
null
?
null
:
bizCode
.
trim
();
}
public
String
getBizState
()
{
return
bizState
;
}
public
void
setBizState
(
String
bizState
)
{
this
.
bizState
=
bizState
==
null
?
null
:
bizState
.
trim
();
}
public
LocalDate
getPushDate
()
{
return
pushDate
;
}
public
void
setPushDate
(
LocalDate
pushDate
)
{
this
.
pushDate
=
pushDate
;
}
public
LocalDateTime
getGmtCreated
()
{
return
gmtCreated
;
}
public
void
setGmtCreated
(
LocalDateTime
gmtCreated
)
{
this
.
gmtCreated
=
gmtCreated
;
}
public
LocalDateTime
getGmtModified
()
{
return
gmtModified
;
}
public
void
setGmtModified
(
LocalDateTime
gmtModified
)
{
this
.
gmtModified
=
gmtModified
;
}
public
String
getMemo
()
{
return
memo
;
}
public
void
setMemo
(
String
memo
)
{
this
.
memo
=
memo
==
null
?
null
:
memo
.
trim
();
}
public
String
getRepayType
()
{
return
repayType
;
}
public
void
setRepayType
(
String
repayType
)
{
this
.
repayType
=
repayType
==
null
?
null
:
repayType
.
trim
();
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/repository/entity/WindowsRetryExample.java
0 → 100644
View file @
c90453d7
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/repository/mapper/WindowsRetryMapper.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
repository
.
mapper
;
import
com.jqtx.windows.repository.entity.WindowsRetry
;
import
com.jqtx.windows.repository.entity.WindowsRetryExample
;
import
java.util.List
;
import
org.apache.ibatis.annotations.Param
;
public
interface
WindowsRetryMapper
{
long
countByExample
(
WindowsRetryExample
example
);
int
deleteByExample
(
WindowsRetryExample
example
);
int
deleteByPrimaryKey
(
Integer
id
);
int
insert
(
WindowsRetry
row
);
int
insertSelective
(
WindowsRetry
row
);
List
<
WindowsRetry
>
selectByExample
(
WindowsRetryExample
example
);
WindowsRetry
selectByPrimaryKey
(
Integer
id
);
int
updateByExampleSelective
(
@Param
(
"row"
)
WindowsRetry
row
,
@Param
(
"example"
)
WindowsRetryExample
example
);
int
updateByExample
(
@Param
(
"row"
)
WindowsRetry
row
,
@Param
(
"example"
)
WindowsRetryExample
example
);
int
updateByPrimaryKeySelective
(
WindowsRetry
row
);
int
updateByPrimaryKey
(
WindowsRetry
row
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/service/SyncDataService.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
service
;
import
com.jqtx.windows.web.response.JsonResult
;
import
java.util.concurrent.Future
;
/**
* @Description:
* @Author: liyongen
* @Date: 2019/6/27 12:00
*/
public
interface
SyncDataService
{
/**
* 同步结果
* @param url
* @param body
* @return
*/
JsonResult
synResult
(
String
url
,
String
body
);
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/service/impl/SyncDataServiceImpl.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
service
.
impl
;
import
com.alibaba.fastjson2.JSONObject
;
import
com.jqtx.windows.component.config.FeignUrlConfig
;
import
com.jqtx.windows.service.SyncDataService
;
import
com.jqtx.windows.utils.http.HttpUtil
;
import
com.jqtx.windows.web.response.JsonResult
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @Description:
* @Author: wuwei
* @Date: 2019/12/04 12:02
*/
@Service
public
class
SyncDataServiceImpl
implements
SyncDataService
{
@Autowired
private
FeignUrlConfig
feignUrlConfig
;
@Override
public
JsonResult
synResult
(
String
url
,
String
body
)
{
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
headers
.
put
(
"secretKey"
,
feignUrlConfig
.
getSecretKey
());
String
resp
=
HttpUtil
.
doPost
(
url
,
headers
,
body
);
return
JSONObject
.
parseObject
(
resp
,
JsonResult
.
class
);
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/utils/DateUtils.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
utils
;
import
java.sql.Timestamp
;
import
java.time.*
;
import
java.util.Date
;
public
class
DateUtils
{
public
static
Date
toDate
(
LocalDateTime
localDateTime
)
{
Instant
instant
=
localDateTime
.
atZone
(
ZoneId
.
systemDefault
()).
toInstant
();
return
Date
.
from
(
instant
);
}
public
static
Date
toDate
(
LocalDate
localDate
)
{
Timestamp
timestamp
=
Timestamp
.
valueOf
(
localDate
.
atTime
(
LocalTime
.
MIDNIGHT
));
return
new
Date
(
timestamp
.
getTime
());
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/utils/http/HttpClientPools.java
0 → 100644
View file @
c90453d7
/**
* kevinGD.com Inc. Copyright (c) 2015-2016-2016 All Rights Reserved.
*/
package
com
.
jqtx
.
windows
.
utils
.
http
;
import
org.apache.commons.lang3.concurrent.BasicThreadFactory
;
import
org.apache.http.HttpEntityEnclosingRequest
;
import
org.apache.http.HttpRequest
;
import
org.apache.http.NameValuePair
;
import
org.apache.http.client.HttpRequestRetryHandler
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.protocol.HttpClientContext
;
import
org.apache.http.config.Registry
;
import
org.apache.http.config.RegistryBuilder
;
import
org.apache.http.conn.ConnectTimeoutException
;
import
org.apache.http.conn.socket.ConnectionSocketFactory
;
import
org.apache.http.conn.socket.LayeredConnectionSocketFactory
;
import
org.apache.http.conn.socket.PlainConnectionSocketFactory
;
import
org.apache.http.conn.ssl.SSLConnectionSocketFactory
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.impl.conn.PoolingHttpClientConnectionManager
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.protocol.HttpContext
;
import
org.apache.http.util.EntityUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
javax.net.ssl.SSLException
;
import
javax.net.ssl.SSLHandshakeException
;
import
java.io.IOException
;
import
java.io.InterruptedIOException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URI
;
import
java.net.UnknownHostException
;
import
java.util.*
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ScheduledThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
/**
* 为对外发http请求连接可复用作处理
* 采用PoolingHttpClientConnectionManager来管理client对象
*
* 详情参见{@link http://www.yeetrack.com/?p=782}
*
* @author jiadong.yejd
* @version $Id: HttpClientPools.java, v 0.1 2016年10月27日 下午5:17:31 jiadong.yejd Exp $
*/
public
class
HttpClientPools
{
/**logger*/
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HttpClientPools
.
class
);
private
static
PoolingHttpClientConnectionManager
cm
=
null
;
/**
* ConnectTimeout服务器请求超时
* ConnectionRequestTimeout 建立请求超时
* SocketTimeout 服务器响应超时
*
*/
protected
static
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
.
setConnectTimeout
(
5000
)
.
setConnectionRequestTimeout
(
3000
)
.
setSocketTimeout
(
8000
)
.
build
();
static
{
ConnectionSocketFactory
plainsf
=
PlainConnectionSocketFactory
.
getSocketFactory
();
LayeredConnectionSocketFactory
sslsf
=
SSLConnectionSocketFactory
.
getSocketFactory
();
Registry
<
ConnectionSocketFactory
>
registry
=
RegistryBuilder
.<
ConnectionSocketFactory
>
create
().
register
(
"http"
,
plainsf
).
register
(
"https"
,
sslsf
).
build
();
cm
=
new
PoolingHttpClientConnectionManager
(
registry
);
// 将最大连接数增加到200
cm
.
setMaxTotal
(
200
);
// 将每个路由基础的连接增加到200
cm
.
setDefaultMaxPerRoute
(
200
);
//连接池管理
IdleConnMonitorTask
idleConnMonitorTask
=
new
IdleConnMonitorTask
();
//org.apache.commons.lang3.concurrent.BasicThreadFactory
ScheduledExecutorService
executorService
=
new
ScheduledThreadPoolExecutor
(
1
,
new
BasicThreadFactory
.
Builder
().
namingPattern
(
"example-schedule-pool-%d"
).
daemon
(
true
).
build
());
executorService
.
scheduleAtFixedRate
(
idleConnMonitorTask
,
30
,
60
,
TimeUnit
.
SECONDS
);
//Timer timer = new Timer(true);
////1分钟执行一次
//timer.schedule(idleConnMonitorTask, 30 * 1000, 60 * 1000);
}
/**
* 它管理着连接池,可以同时为很多线程提供http连接请求
* 如果连接池有有可用的持久连接,连接管理器就会使用其中的一个,而不是再创建一个新的连接。
* PoolingClientConnectionManager会根据它的配置来分配请求连接,如果连接池中的所有连接都被占用了,那么后续的请求就会被阻塞,直到有连接被释放回连接池中
* @return
*/
public
static
CloseableHttpClient
getHttpClient
()
{
//请求重试处理
HttpRequestRetryHandler
httpRequestRetryHandler
=
new
RetryHandler
();
CloseableHttpClient
httpClient
=
HttpClients
.
custom
().
setConnectionManager
(
cm
)
.
setRetryHandler
(
httpRequestRetryHandler
).
build
();
return
httpClient
;
}
/**
* 构建Get请求方法
*
* @param url 请求地址
* @param paramMap 参数
* @param charSet 编码
* @return
* @throws Exception
*/
public
static
HttpGet
buildGet
(
String
url
,
Map
<
String
,
String
>
paramMap
,
String
charSet
)
throws
Exception
{
HttpGet
httpGet
=
new
HttpGet
(
url
);
httpGet
.
setConfig
(
requestConfig
);
List
<
NameValuePair
>
params
=
new
ArrayList
<
NameValuePair
>();
Set
<
String
>
keySet
=
paramMap
.
keySet
();
for
(
Iterator
<
String
>
it
=
keySet
.
iterator
();
it
.
hasNext
();
)
{
String
next
=
it
.
next
();
params
.
add
(
new
BasicNameValuePair
(
next
,
paramMap
.
get
(
next
)));
}
String
str
=
EntityUtils
.
toString
(
new
UrlEncodedFormEntity
(
params
,
charSet
));
httpGet
.
setURI
(
new
URI
(
url
+
"?"
+
str
));
return
httpGet
;
}
/**
* 构建Post请求方法
*
* @param url 请求地址
* @param paramMap 参数
* @param charSet 编码
* @return
* @throws UnsupportedEncodingException
*/
public
static
HttpPost
buildPost
(
String
url
,
Map
<
String
,
String
>
paramMap
,
String
charSet
)
throws
UnsupportedEncodingException
{
HttpPost
httpPost
=
new
HttpPost
(
url
);
httpPost
.
setConfig
(
requestConfig
);
httpPost
.
addHeader
(
"Content-Type"
,
"application/x-www-form-urlencoded;"
);
httpPost
.
addHeader
(
"charset="
,
charSet
);
List
<
NameValuePair
>
valuePairs
=
new
ArrayList
<
NameValuePair
>();
for
(
String
key
:
paramMap
.
keySet
())
{
valuePairs
.
add
(
new
BasicNameValuePair
(
key
,
(
String
)
paramMap
.
get
(
key
)));
}
httpPost
.
setEntity
(
new
UrlEncodedFormEntity
(
valuePairs
,
charSet
));
return
httpPost
;
}
/**
* 对失败的处理
*
* @author jiadong.yejd
* @version $Id: HttpClientPools.java, v 0.1 2016年10月27日 下午5:53:25 jiadong.yejd Exp $
*/
static
class
RetryHandler
implements
HttpRequestRetryHandler
{
@Override
public
boolean
retryRequest
(
IOException
exception
,
int
executionCount
,
HttpContext
context
)
{
if
(
executionCount
>=
3
)
{
// 如果已经重试了3次,就放弃
return
false
;
}
if
(
exception
instanceof
SSLHandshakeException
)
{
// 不要重试SSL握手异常
return
false
;
}
if
(
exception
instanceof
InterruptedIOException
)
{
// 超时
return
false
;
}
if
(
exception
instanceof
UnknownHostException
)
{
// 目标服务器不可达
return
false
;
}
if
(
exception
instanceof
ConnectTimeoutException
)
{
// 连接被拒绝
return
false
;
}
if
(
exception
instanceof
SSLException
)
{
// ssl握手异常
return
false
;
}
HttpClientContext
clientContext
=
HttpClientContext
.
adapt
(
context
);
HttpRequest
request
=
clientContext
.
getRequest
();
// 如果请求是幂等的,就再次尝试
if
(!(
request
instanceof
HttpEntityEnclosingRequest
))
{
return
true
;
}
return
false
;
}
}
/**
*
* @author liudehong
* @version $Id: HttpClientPools.java, v 0.1 2017年7月31日 下午4:48:48 liudehong Exp $
*/
private
static
class
IdleConnMonitorTask
extends
TimerTask
{
public
IdleConnMonitorTask
()
{
System
.
err
.
println
(
"IdleConnMonitorTask..."
);
}
@Override
public
void
run
()
{
try
{
// 关闭过期的连接
//cm.closeExpiredConnections();
//关闭3分钟内不活动的连接
cm
.
closeIdleConnections
(
3
,
TimeUnit
.
MINUTES
);
logger
.
warn
(
"http pool连接池回收成功,连接池的当前状态:"
+
cm
.
getTotalStats
());
}
catch
(
Throwable
ex
)
{
logger
.
error
(
"http连接池管理异常"
,
ex
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/jqtx/windows/utils/http/HttpUtil.java
0 → 100644
View file @
c90453d7
package
com
.
jqtx
.
windows
.
utils
.
http
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.NameValuePair
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.utils.HttpClientUtils
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.util.EntityUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.util.CollectionUtils
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.nio.charset.Charset
;
import
java.util.*
;
/**
* http工具类
*
* @author shaohai.zhao
* @version : HttpUtil.java, v 0.1 2018-08-08 19:30 shaohai.zhao Exp $
*/
public
class
HttpUtil
extends
HttpClientPools
{
/**
* logger
*/
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HttpUtil
.
class
);
/**
* 发送 POST 请求(HTTP),K-V形式
*
* @param apiUrl API接口URL
* @param params 参数map
*/
public
static
String
doPost
(
String
apiUrl
,
Map
<
String
,
String
>
headers
,
Map
<
String
,
String
>
params
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
String
httpStr
=
null
;
HttpPost
httpPost
=
new
HttpPost
(
apiUrl
);
if
(!
CollectionUtils
.
isEmpty
(
headers
))
{
for
(
String
key
:
headers
.
keySet
())
{
httpPost
.
setHeader
(
key
,
headers
.
get
(
key
));
}
}
CloseableHttpResponse
response
=
null
;
try
{
httpPost
.
setConfig
(
requestConfig
);
List
<
NameValuePair
>
pairList
=
new
ArrayList
<
NameValuePair
>(
params
.
size
());
for
(
Map
.
Entry
<
String
,
String
>
entry
:
params
.
entrySet
())
{
NameValuePair
pair
=
new
BasicNameValuePair
(
entry
.
getKey
(),
entry
.
getValue
());
pairList
.
add
(
pair
);
}
httpPost
.
setEntity
(
new
UrlEncodedFormEntity
(
pairList
,
Charset
.
forName
(
"UTF-8"
)));
response
=
httpClient
.
execute
(
httpPost
);
HttpEntity
entity
=
response
.
getEntity
();
httpStr
=
EntityUtils
.
toString
(
entity
,
"UTF-8"
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"http请求失败,失败信息"
+
e
.
getMessage
());
}
finally
{
if
(
response
!=
null
)
{
try
{
EntityUtils
.
consume
(
response
.
getEntity
());
}
catch
(
IOException
e
)
{
logger
.
error
(
"http请求失败,失败信息"
+
e
.
getMessage
());
}
}
}
return
httpStr
;
}
/**
* 发送 POST 请求(HTTP),JSON形式
*
* @param json json对象
*/
public
static
String
doPost
(
String
apiUrl
,
Map
<
String
,
String
>
headers
,
String
json
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
String
httpStr
=
null
;
HttpPost
httpPost
=
new
HttpPost
(
apiUrl
);
CloseableHttpResponse
response
=
null
;
if
(!
CollectionUtils
.
isEmpty
(
headers
))
{
for
(
String
key
:
headers
.
keySet
())
{
httpPost
.
setHeader
(
key
,
headers
.
get
(
key
));
}
}
try
{
httpPost
.
setConfig
(
requestConfig
);
StringEntity
stringEntity
=
new
StringEntity
(
json
,
"UTF-8"
);
//解决中文乱码问题
stringEntity
.
setContentEncoding
(
"UTF-8"
);
stringEntity
.
setContentType
(
"application/json"
);
httpPost
.
setEntity
(
stringEntity
);
response
=
httpClient
.
execute
(
httpPost
);
HttpEntity
entity
=
response
.
getEntity
();
httpStr
=
EntityUtils
.
toString
(
entity
,
"UTF-8"
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"http请求失败,失败信息"
+
e
.
getMessage
());
}
finally
{
if
(
response
!=
null
)
{
try
{
EntityUtils
.
consume
(
response
.
getEntity
());
}
catch
(
IOException
e
)
{
logger
.
error
(
"http请求失败,失败信息"
+
e
.
getMessage
());
}
}
}
return
httpStr
;
}
/**
* 发送 SSL POST 请求(HTTPS),K-V形式
*
* @param apiUrl API接口URL
* @param params 参数map
*/
public
static
String
doPostSSL
(
String
apiUrl
,
Map
<
String
,
String
>
params
)
{
CloseableHttpClient
httpClient
=
getHttpClient
();
HttpPost
httpPost
=
new
HttpPost
(
apiUrl
);
CloseableHttpResponse
response
=
null
;
String
httpStr
=
null
;
try
{
httpPost
.
setConfig
(
requestConfig
);
List
<
NameValuePair
>
pairList
=
new
ArrayList
<
NameValuePair
>(
params
.
size
());
for
(
Map
.
Entry
<
String
,
String
>
entry
:
params
.
entrySet
())
{
NameValuePair
pair
=
new
BasicNameValuePair
(
entry
.
getKey
(),
entry
.
getValue
());
pairList
.
add
(
pair
);
}
httpPost
.
setEntity
(
new
UrlEncodedFormEntity
(
pairList
,
Charset
.
forName
(
"utf-8"
)));
response
=
httpClient
.
execute
(
httpPost
);
HttpEntity
entity
=
response
.
getEntity
();
httpStr
=
EntityUtils
.
toString
(
entity
,
"utf-8"
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"请求通联服务异常,失败信息"
+
e
.
getMessage
());
}
finally
{
HttpClientUtils
.
closeQuietly
(
response
);
}
return
httpStr
;
}
/**
* 路由提交方法
*/
public
static
String
doPostAll
(
String
apiUrl
,
Map
<
String
,
String
>
params
)
{
if
(
apiUrl
==
null
)
{
return
null
;
}
if
(
apiUrl
.
indexOf
(
"https:"
)
!=
-
1
)
{
return
doPostSSL
(
apiUrl
,
params
);
}
return
doPost
(
apiUrl
,
null
,
params
);
}
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
paramMap
)
{
CloseableHttpClient
httpClient
=
getHttpClient
();
HttpGet
httpGet
=
new
HttpGet
(
url
);
CloseableHttpResponse
response
=
null
;
String
httpStr
=
null
;
try
{
httpGet
.
setConfig
(
requestConfig
);
List
<
NameValuePair
>
params
=
new
ArrayList
<
NameValuePair
>();
if
(!
CollectionUtils
.
isEmpty
(
paramMap
))
{
Set
<
String
>
keySet
=
paramMap
.
keySet
();
for
(
Iterator
<
String
>
it
=
keySet
.
iterator
();
it
.
hasNext
();
)
{
String
next
=
it
.
next
();
params
.
add
(
new
BasicNameValuePair
(
next
,
paramMap
.
get
(
next
)));
}
}
String
str
=
EntityUtils
.
toString
(
new
UrlEncodedFormEntity
(
params
,
"utf-8"
));
httpGet
.
setURI
(
new
URI
(
url
+
"?"
+
str
));
response
=
httpClient
.
execute
(
httpGet
);
HttpEntity
entity
=
response
.
getEntity
();
httpStr
=
EntityUtils
.
toString
(
entity
,
"utf-8"
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"请求服务异常,失败信息"
+
e
.
getMessage
());
}
finally
{
HttpClientUtils
.
closeQuietly
(
response
);
}
return
httpStr
;
}
}
This diff is collapsed.
Click to expand it.
src/main/resources/mybatis/mapper/WindowsRetryMapper.xml
0 → 100644
View file @
c90453d7
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment