com.alipay.api.internal.util.json.JSONWriter Java Examples

The following examples show how to use com.alipay.api.internal.util.json.JSONWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AlipayMsgClient.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
public ProduceMsgAck sendMessage(AlipayRequest msgReq) throws InterruptedException {
    if (!isConnected()) {
        throw new NotYetConnectedException();
    }
    Message message = new Message();
    message.setxCmd(MsgConstants.MSG_CMD_PRODUCE);
    message.setxSignType(signType);
    message.setxCharset(charset);
    message.setAppId(appId);
    message.setMsgApi(msgReq.getApiMethodName());
    message.setxTimestamp(System.currentTimeMillis());
    message.setBizContent(new JSONWriter().write(msgReq.getBizModel(), true));
    Message.addSign(message, appPrivateKey);
    ProtocolData protocolData = new ProtocolData();
    protocolData.setMessage(message);
    CountDownLatch signal = new CountDownLatch(1);
    ProtocolDataContext protocolDataContext = new ProtocolDataContext();
    protocolDataContext.setSendData(protocolData);
    protocolDataContext.setSendSignal(signal);
    if (!sendingQueue.offer(protocolData.getStreamId(), 200, TimeUnit.MILLISECONDS)) {
        throw new RuntimeException("too many message not receive ack, refuse new send. streamId:"
                + protocolData.getStreamId());
    }
    sendingContexts.put(protocolData.getStreamId(), protocolDataContext);

    String protocolDataStr = ProtocolData.toStr(protocolData);
    if (AlipayLogger.isBizDebugEnabled()) {
        AlipayLogger.logBizDebug("send msg:" + protocolDataStr.replaceAll("[\r\n]", " "));
    }
    webSocketConnector.send(ProtocolData.toStr(protocolData));
    boolean signalNotify = signal.await(10000, TimeUnit.MILLISECONDS);

    sendingQueue.remove(protocolData.getStreamId());
    sendingContexts.remove(protocolData.getStreamId());
    if (!signalNotify) {
        AlipayLogger.logBizError("wait ack timeout(10s). streamId:" + protocolData.getStreamId());
        throw new RuntimeException("wait ack timeout(10s). streamId:" + protocolData.getStreamId());
    }

    ProtocolData ackData = protocolDataContext.getAckData();
    if (ackData == null) {
        throw new RuntimeException("ack protocol data null. streamId:" + protocolData.getStreamId());
    }
    Message ackMsg = ackData.getMessage();
    if (ackMsg == null) {
        throw new RuntimeException("ack msg null. streamId:" + protocolData.getStreamId());
    }
    ProduceMsgAck produceMsgAck = new ProduceMsgAck();
    produceMsgAck.setxStatus(MsgStatusEnum.fromStr(ackMsg.getxStatus()));
    produceMsgAck.setxCode(ackMsg.getxCode());
    produceMsgAck.setxError(ackMsg.getxError());
    produceMsgAck.setxMessageId(ackMsg.getxMessageId());
    return produceMsgAck;
}