4-1、ActiveMQ 配置項
阿新 • • 發佈:2018-11-08
我們去掉了維金的版本,和trade交易保持同步,改用spring boot 自帶的ActiveMQ。配置如下:
1.application配置
ActiveMqMessageProducer生產者傳送程式碼
package
com.netfinworks.pps.message;
import com.meidusa.fastjson.JSONObject;
import
com.netfinworks.pps.ext.integration.self.PpsNotifyFrontendClientImpl;
import
org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import
org.apache.activemq.protobuf.BaseMessage;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory; import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.jms.core.JmsMessagingTemplate;
import
org.springframework.stereotype.Service;
import
javax.jms.Destination;
import
javax.jms.JMSException;
@Service
public
class
ActiveMqMessageProducer
implements
MessageProducer<MessageType,String,Object> {
private
static
Logger logger = LoggerFactory.getLogger(ActiveMqMessageProducer.
class
);
@Autowired
private
JmsMessagingTemplate jmsTemplate;
@Override
public
boolean
send(String queue,Object body) {
return
send(
null
,queue,body);
}
private
boolean
send(MessageType type,String address,String body){
Destination destination = getDestination(type,address);
logger.info(
"send ActiveMqMessageProducer address = {},content= {}"
,address,body);
jmsTemplate.convertAndSend(destination,body);
return
true
;
}
@Override
public
boolean
send(MessageType type, String address, Object body) {
return
send(type,address,body);
}
private
Destination getDestination(MessageType type, String address){
if
(type ==
null
){
type = MessageType.QUEUE;
}
switch
(type){
case
QUEUE:
return
new
ActiveMQQueue(address);
case
TOPIC:
return
new
ActiveMQTopic(address);
default
:
return
new
ActiveMQQueue(address);
}
}
/*public static void main(String[] args) {
System.out.println(senJSONObject.toJSONString("{\"extension\":\"{\\\"apiResultMsg\\\":\\\"[BCSS001000001]:Invalid Institute Parameter[BCSS001000001]:Invalid Institute Parameter\\\",\\\"apiResultcode\\\":\\\"11\\\"}\",\"notifyTpye\":\"PAYMENT\",\"paymentOrderList\":\"{\\\"memo\\\":\\\"支付階段支付\\\",\\\"paymentState\\\":\\\"F\\\",\\\"paymentVoucherNo\\\":\\\"20180918113337315500000001695\\\"}\",\"paymentVoucherNo\":\"20180918113337315500000001695\",\"state\":\"F\",\"tradeOrderNo\":\"20180918113337254100000001694\"}"));
}*/
}
|
/**
*
*/
package
com.netfinworks.pps.ext.service.jms;
import
com.meidusa.fastjson.JSON;
import
com.meidusa.fastjson.JSONObject;
import
com.netfinworks.common.util.LogUtil;
import
com.netfinworks.common.util.MDCUtil;
import
com.netfinworks.mq.handler.impl.AbstractMessageHandler;
import
com.netfinworks.mq.handler.notify.AbstractNotifyMessageHandler;
import
com.netfinworks.mq.request.MQRequest;
import
com.netfinworks.mq.request.notify.DefaultNotifyRequest;
import
com.netfinworks.payment.service.facade.enums.PaymentState;
import
com.netfinworks.pfs.service.payment.PaymentQueryFacade;
import
com.netfinworks.pfs.service.payment.domain.message.PaymentMessageBody;
import
com.netfinworks.pfs.service.payment.domain.message.PaymentUniMessage;
import
com.netfinworks.pfs.service.payment.response.QueryByBPSNResponse;
import
com.netfinworks.pps.core.common.Constants;
import
com.netfinworks.pps.core.domain.vo.PaymentResult;
import
com.netfinworks.pps.core.domain.vo.enums.PaymentSceneType;
import
com.netfinworks.pps.core.domainservice.PaymentService;
import
com.netfinworks.pps.core.domainservice.RefundPaymentService;
import
com.netfinworks.pps.core.domainservice.SettlementService;
import
com.netfinworks.pps.core.domainservice.clearing.ClearingService;
import
com.netfinworks.pps.core.domainservice.exchange.ExchangeService;
import
com.netfinworks.pps.core.util.MapUtils;
import
org.apache.commons.lang.StringUtils;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.slf4j.MDC;
import
org.springframework.jms.annotation.JmsListener;
import
org.springframework.stereotype.Component;
import
org.springframework.stereotype.Service;
import
javax.annotation.Resource;
import
java.util.HashMap;
import
java.util.Map;
/**
* <p>支付結果非同步通知</p>
* @author Fengxueyong
* @version $Id: PaymentResultMessageHandler.java, v 0.1 2018年1月1日 上午11:29:36 fengxueyong Exp $
*/
@Component
public
class
PaymentResultMessageHandler{
// 日誌列印
private
static
final
Logger logger = LoggerFactory.getLogger(PaymentResultMessageHandler.
class
);
@Resource
(name =
"paymentService"
)
private
PaymentService paymentService;
@Resource
(name =
"settleService"
)
private
SettlementService settlementService;
@Resource
(name =
"clearingService"
)
private
ClearingService clearingService;
@Resource
(name =
"exchangeService"
)
private
ExchangeService exchangeService;
@Resource
(name =
"paymentQueryFacade"
)
private
PaymentQueryFacade paymentQueryFacade;
@Resource
(name =
"refundPaymentService"
)
private
RefundPaymentService refundPaymentService;
/**
* Payment支付結果通知處理
* @param request
* @throws Exception
*/
@JmsListener
(destination =
"${payment.asyn.result.queue.name}"
)
public
void
handleMessage(DefaultNotifyRequest<PaymentUniMessage> request)
throws
Exception {
try
{
logger.info(
"payment.asyn.result.queue 收到 payment 非同步通知,msg="
+JSONObject.toJSONString(request.getContent()));
DefaultNotifyRequest<PaymentUniMessage> message = (DefaultNotifyRequest<PaymentUniMessage>)request;
if
(request==
null
){
logger.warn(
"payment.asyn.result.queue is null"
);
}
//設定MDC
MDCUtil.init(message);
logger.info(
"[{}->Spring boot PPS][JMS]接收payment支付結果通知訊息:{}"
, message.getDestination(), message.getContent());
//獲取訊息體
PaymentMessageBody messageBody = JSON.parseObject(message.getContent().getMessageBodyString(), PaymentMessageBody.
class
);
// 處理非同步通知
handlerPaymentResult(messageBody);
}
catch
(IllegalArgumentException ex) {
logger.error(
"Payment支付結果處理引數異常"
, ex);
}
catch
(Exception ex) {
logger.error(
"Payment支付結果處理失敗"
, ex);
throw
ex;
}
finally
{
MDC.clear();
}
}
private
void
handlerPaymentResult(PaymentMessageBody messageBody){
logger.info(
"Spring boot PPS 收到 payment 非同步通知,msg="
+ JSON.toJSONString(messageBody));
// 根據業務支付號查詢支付資訊
QueryByBPSNResponse response = paymentQueryFacade.queryByBizPaymentSeqNo(messageBody.getBizPaymentSeqNo(), LogUtil.buildOEWithClientId(Constants.CLIENT_ID));
// 獲取支付場景型別
PaymentSceneType psType = parsePsType(response.getBizPaymentOrderInfo().getExtension());
if
(psType ==
null
){
return
;
}
PaymentResult paymentResult = createPaymentResult(messageBody,response);
switch
(psType){
case
PAYMENT:
paymentService.handlePaymentResult(paymentResult);
break
;
// case PAYMENT_PROFIT_LOSE:
// clearingService.handleProfitOrLosePaymentResult(paymentResult);
// break;
case
EXCHANGE:
exchangeService.handleExchangePaymentResult(paymentResult);
break
;
// case EXCHANGE_PROFIT_LOSE:
// exchangeService.handleExchangeProfitOrLosePaymentResult(paymentResult);
// break;
case
SETTLE:
settlementService.handlePaymentResult(paymentResult);
break
;
case
REFUND_PAYMENT:
case
REFUND_SETTLEMENT:
case
REFUND_EXCHANGE:
// case REFUND_EXCHANGE_PROFIT_LOSE:
// case REFUND_PAYMENT_PROFIT_LOSE:
refundPaymentService.handleRefundPaymentResult(paymentResult, messageBody.getOrigPaymentVoucherNo());
break
;
default
:
break
;
}
return
;
}
private
PaymentSceneType parsePsType(String bpoExtension) {
Map<String,String> bpoExtMap =
new
HashMap<String,String>();
if
(StringUtils.isNotBlank(bpoExtension)){
bpoExtMap = MapUtils.parseString(bpoExtension);
}
if
(bpoExtMap.containsKey(Constants.PAYMENT_SCENE_TYPE_KEY)){
return
PaymentSceneType.getByCode(bpoExtMap.get(Constants.PAYMENT_SCENE_TYPE_KEY));
}
return
null
;
}
private
PaymentResult createPaymentResult(PaymentMessageBody messageBody, QueryByBPSNResponse response) {
PaymentResult paymentResult =
new
PaymentResult();
paymentResult.setPaymentSeqNo(response.getBizPaymentOrderInfo().getPaymentOrderInfos().get(
0
).getPaymentSeqNo());
paymentResult.setPaymentState(PaymentState.getByCode(messageBody.getBizPaymentState()));
Map<String, String> ext = messageBody.getExtensions();
Map<String, String> extensions =
new
HashMap<>();
extensions.put(
"apiResultMsg"
, ext.get(
"apiResultMsg"
));
extensions.put(
"apiResultcode"
, ext.get(
"apiResultcode"
));
extensions.put(
"fundChannelShippingAddress"
, ext.get(
"fundChannelShippingAddress"
));
String paymentCode = ext.get(
"paymentCode"
);
if
(paymentResult.getPaymentState() == PaymentState.PROCESS && paymentCode !=
null
) {
extensions.put(
"paymentCode"
, paymentCode);
}
paymentResult.setExtension(JSON.toJSONString(extensions));
return
paymentResult;
}
protected
Object getMessageContent(MQRequest request) {
return
request;
}
public
static
void
main(String[] args) {
PaymentMessageBody messageBody = JSON.parseObject(
"{\"notifyTpye\":\"REFUND\",\"origPaymentVoucherNo\":\"P20180402FO1428002\",\"paymentOrderList\":\"{\\\"memo\\\":\\\"退支付\\\",\\\"paymentSta te\\\":\\\"S\\\",\\\"paymentVoucherNo\\\":\\\"P20180402FO1428002\\\"}\",\"paymentVoucherNo\":\"P20180402FO1428002\",\"state\":\"S\",\"tradeOrderNo\":\"T20180402FO1428002\"}"
, PaymentMessageBody.
class
);
System.out.println(messageBody);
}
}
|
3.測試 (寫個定時訊息傳送測試,記得測試完遮蔽掉程式碼)