Search in sources :

Example 1 with DefaultMessageTransmitter

use of com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4MQ method createMQClient.

private DefaultMessageTransmitter createMQClient(ParticipantContext context) throws Exception {
    MsgQueueConsensusSettings consensusSettings = (MsgQueueConsensusSettings) getConsensusSetting(context);
    String server = consensusSettings.getNetworkSettings().getServer();
    String txTopic = consensusSettings.getNetworkSettings().getTxTopic();
    String txResultTopic = consensusSettings.getNetworkSettings().getTxResultTopic();
    String msgTopic = consensusSettings.getNetworkSettings().getMsgTopic();
    String msgResultTopic = consensusSettings.getNetworkSettings().getMsgResultTopic();
    MsgQueueProducer txProducer = MsgQueueFactory.newProducer(server, txTopic, false);
    MsgQueueConsumer txResultConsumer = MsgQueueFactory.newConsumer(server, txResultTopic, false);
    MsgQueueProducer msgProducer = MsgQueueFactory.newProducer(server, msgTopic, false);
    MsgQueueConsumer msgResultConsumer = MsgQueueFactory.newConsumer(server, msgResultTopic, false);
    DefaultMessageTransmitter transmitter = new DefaultMessageTransmitter().setTxProducer(txProducer).setTxResultConsumer(txResultConsumer).setMsgProducer(msgProducer).setMsgResultConsumer(msgResultConsumer);
    transmitter.connect();
    return transmitter;
}
Also used : MsgQueueProducer(com.jd.blockchain.consensus.mq.producer.MsgQueueProducer) MsgQueueConsensusSettings(com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings) MsgQueueConsumer(com.jd.blockchain.consensus.mq.consumer.MsgQueueConsumer) DefaultMessageTransmitter(com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter)

Example 2 with DefaultMessageTransmitter

use of com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter in project jdchain-core by blockchain-jd-com.

the class ParticipantManagerService4MQ method submitNodeStateChangeTx.

@Override
public TransactionResponse submitNodeStateChangeTx(ParticipantContext context, int activeID, TransactionRequest txRequest, List<NodeSettings> origConsensusNodes) {
    TxResponseMessage responseMessage = new TxResponseMessage();
    DefaultMessageTransmitter mqClient = null;
    try {
        // 初始化区块队列
        initBlockQueue(context, activeID);
        // 发送拓扑变更消息,通知领导者推送落块消息
        mqClient = createMQClient(context);
        ExtendMessage extendMessage = new ExtendMessage(MessageType.RECONFIGURE, null);
        AsyncFuture<byte[]> msgFuture = mqClient.sendUnordered(MessageConvertor.serializeExtendMessage(extendMessage));
        byte[] result = msgFuture.get(MQ_INVOKE_TIMEOUT, TimeUnit.MILLISECONDS);
        if (result == null) {
            responseMessage.setExecutionState(TransactionState.TIMEOUT);
        } else {
            ExtendMessageResult messageResult = MessageConvertor.convertBytesExtendMessageResult(result);
            if (!messageResult.isSuccess()) {
                LOGGER.error("extend message execute error: {}", messageResult.getError());
                responseMessage.setExecutionState(TransactionState.CONSENSUS_ERROR);
            } else {
                // 发送激活交易
                AsyncFuture<byte[]> txFuture = mqClient.sendOrdered(BinaryProtocol.encode(txRequest, TransactionRequest.class));
                result = txFuture.get(MQ_INVOKE_TIMEOUT, TimeUnit.MILLISECONDS);
                if (result == null) {
                    responseMessage.setExecutionState(TransactionState.TIMEOUT);
                    return responseMessage;
                }
                return BinaryProtocol.decode(result);
            }
        }
    } catch (Exception e) {
        LOGGER.error("message or tx execute error", e);
        responseMessage.setExecutionState(TransactionState.CONSENSUS_ERROR);
    } finally {
        if (null != mqClient) {
            mqClient.close();
        }
    }
    return responseMessage;
}
Also used : DefaultMessageTransmitter(com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter) TxResponseMessage(com.jd.blockchain.transaction.TxResponseMessage) ExtendMessage(com.jd.blockchain.consensus.mq.event.ExtendMessage) ExtendMessageResult(com.jd.blockchain.consensus.mq.event.ExtendMessageResult)

Aggregations

DefaultMessageTransmitter (com.jd.blockchain.consensus.mq.client.DefaultMessageTransmitter)2 MsgQueueConsumer (com.jd.blockchain.consensus.mq.consumer.MsgQueueConsumer)1 ExtendMessage (com.jd.blockchain.consensus.mq.event.ExtendMessage)1 ExtendMessageResult (com.jd.blockchain.consensus.mq.event.ExtendMessageResult)1 MsgQueueProducer (com.jd.blockchain.consensus.mq.producer.MsgQueueProducer)1 MsgQueueConsensusSettings (com.jd.blockchain.consensus.mq.settings.MsgQueueConsensusSettings)1 TxResponseMessage (com.jd.blockchain.transaction.TxResponseMessage)1