final void acknowledge( ConnectionContext context, final TopicMessageStore destination, final String clientId, final String subscriptionName, final MessageId messageId, final MessageAck ack) throws IOException { if (ack.isInTransaction()) { if (ack.getTransactionId().isXATransaction() || theStore.isConcurrentStoreAndDispatchTransactions() == false) { destination.acknowledge(context, clientId, subscriptionName, messageId, ack); } else { Tx tx = getTx(ack.getTransactionId()); tx.add( new RemoveMessageCommand(context) { public MessageAck getMessageAck() { return ack; } public Future<Object> run(ConnectionContext ctx) throws IOException { destination.acknowledge(ctx, clientId, subscriptionName, messageId, ack); return AbstractMessageStore.FUTURE; } }); } } else { destination.acknowledge(context, clientId, subscriptionName, messageId, ack); } }
Future<Object> asyncAddTopicMessage( ConnectionContext context, final MessageStore destination, final Message message) throws IOException { if (message.getTransactionId() != null) { if (message.getTransactionId().isXATransaction() || theStore.isConcurrentStoreAndDispatchTransactions() == false) { destination.addMessage(context, message); return AbstractMessageStore.FUTURE; } else { Tx tx = getTx(message.getTransactionId()); tx.add( new AddMessageCommand(context) { @Override public Message getMessage() { return message; } @Override public Future<Object> run(ConnectionContext ctx) throws IOException { return destination.asyncAddTopicMessage(ctx, message); } }); return AbstractMessageStore.FUTURE; } } else { return destination.asyncAddTopicMessage(context, message); } }
final void removeAsyncMessage( ConnectionContext context, final MessageStore destination, final MessageAck ack) throws IOException { if (ack.isInTransaction()) { if (ack.getTransactionId().isXATransaction() || theStore.isConcurrentStoreAndDispatchTransactions() == false) { destination.removeAsyncMessage(context, ack); } else { Tx tx = getTx(ack.getTransactionId()); tx.add( new RemoveMessageCommand(context) { @Override public MessageAck getMessageAck() { return ack; } @Override public Future<Object> run(ConnectionContext ctx) throws IOException { destination.removeMessage(ctx, ack); return AbstractMessageStore.FUTURE; } }); } } else { destination.removeAsyncMessage(context, ack); } }
/** 添加消息,为了保证添加顺序,这里不得不加锁 */ @Override public void addMessage( final MessageStore store, final long msgId, final PutCommand putCmd, JournalLocation location) throws IOException { if (location == null) { // 非重放,添加put日志 final AppendMessageCommand appendCmd = AppendMessageCommand.newBuilder() .setMessageId(msgId) .setPutCommand(ByteString.copyFrom(putCmd.encode().array())) .build(); final TxCommand txCommand = TxCommand.newBuilder() .setCmdType(TxCommandType.APPEND_MSG) .setCmdContent(appendCmd.toByteString()) .build(); final Tx tx = this.getInflyTx(putCmd.getTransactionId()); if (tx != null) { location = this.journalStore.write(txCommand, null, tx.location, false); } else { location = this.journalStore.write(txCommand, null, null, false); } } final Tx tx = this.getTx(putCmd.getTransactionId(), location); tx.add(store, msgId, putCmd); }