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;
}
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;
}
Aggregations