src/main/java/com/hedera/hashgraph/identity/hcs/MessageTransaction.java
/**
   * Builds the message and submits it to appnet's topic.
   *
   * @param client The hedera network client.
   * @return Transaction ID.
   */
Builds the message and submits it to appnet's topic.
@param client The hedera network client.
@return Transaction ID.
Builds the message and submits it to appnet ' s topic . @param client The hedera network client . @return Transaction ID .
public TransactionId execute(final Client client) {
    new Validator().checkValidationErrors("MessageTransaction execution failed: ", v -> validate(v));
    MessageEnvelope<T> envelope = message == null ? buildMessage() : message;
    if (encrypter != null) {
      envelope.encrypt(provideMessageEncrypter(encrypter));
    }
    byte[] messageContent;
    if (envelope.getSignature() == null) {
      messageContent = (this.privateKey != null) ? envelope.sign(privateKey) : envelope.sign(signer);
    } else {
      messageContent = envelope.toJson().getBytes(StandardCharsets.UTF_8);
    }
    if (receiver != null) {
      listener = provideTopicListener(topicId);
      byte[] finalMessageContent = messageContent;
      listener.setStartTime(Instant.now().minusSeconds(1))
              .setIgnoreErrors(false)
              .addFilter(r -> Arrays.equals(finalMessageContent, r.contents))
              .onError(err -> handleError(err))
              .onInvalidMessageReceived((response, reason) -> {
                if (!Arrays.equals(finalMessageContent, response.contents)) {
                  return;
                }
                handleError(new InvalidMessageException(response, reason));
                listener.unsubscribe();
              })
              .onDecrypt(decrypter)
              .subscribe(client, msg -> {
                listener.unsubscribe();
                receiver.accept(msg);
              });
    }
    TopicMessageSubmitTransaction tx = new TopicMessageSubmitTransaction()
            .setTopicId(topicId)
            .setMessage(messageContent);
    if (this.maxTransactionFee != null) {
      tx.setMaxTransactionFee(this.maxTransactionFee);
    }
    TransactionId transactionId = null;
    try {
      TransactionResponse response;
      if (buildTransactionFunction != null) {
        response = (TransactionResponse) buildTransactionFunction
                .apply(tx)
                .execute(client);
      } else {
        response = tx.execute(client);
      }
      transactionId = response.transactionId;
      executed = true;
    } catch (PrecheckStatusException | TimeoutException e) {
      handleError(e);
      if (listener != null) {
        listener.unsubscribe();
      }
    }
    return transactionId;
  }
public TransactionId execute ( final Client client ) { new Validator ( ) . checkValidationErrors ( "MessageTransaction execution failed: " , v -> validate ( v ) ) ; MessageEnvelope < T > envelope = message == null ? buildMessage ( ) : message ; if ( encrypter != null ) { envelope . encrypt ( provideMessageEncrypter ( encrypter ) ) ; } byte [ ] messageContent ; if ( envelope . getSignature ( ) == null ) { messageContent = ( this . privateKey != null ) ? envelope . sign ( privateKey ) : envelope . sign ( signer ) ; } else { messageContent = envelope . toJson ( ) . getBytes ( StandardCharsets . UTF_8 ) ; } if ( receiver != null ) { listener = provideTopicListener ( topicId ) ; byte [ ] finalMessageContent = messageContent ; listener . setStartTime ( Instant . now ( ) . minusSeconds ( 1 ) ) . setIgnoreErrors ( false ) . addFilter ( r -> Arrays . equals ( finalMessageContent , r . contents ) ) . onError ( err -> handleError ( err ) ) . onInvalidMessageReceived ( ( response , reason ) -> { if ( ! Arrays . equals ( finalMessageContent , response . contents ) ) { return ; } handleError ( new InvalidMessageException ( response , reason ) ) ; listener . unsubscribe ( ) ; } ) . onDecrypt ( decrypter ) . subscribe ( client , msg -> { listener . unsubscribe ( ) ; receiver . accept ( msg ) ; } ) ; } TopicMessageSubmitTransaction tx = new TopicMessageSubmitTransaction ( ) . setTopicId ( topicId ) . setMessage ( messageContent ) ; if ( this . maxTransactionFee != null ) { tx . setMaxTransactionFee ( this . maxTransactionFee ) ; } TransactionId transactionId = null ; try { TransactionResponse response ; if ( buildTransactionFunction != null ) { response = ( TransactionResponse ) buildTransactionFunction . apply ( tx ) . execute ( client ) ; } else { response = tx . execute ( client ) ; } transactionId = response . transactionId ; executed = true ; } catch ( PrecheckStatusException | TimeoutException e ) { handleError ( e ) ; if ( listener != null ) { listener . unsubscribe ( ) ; } } return transactionId ; }
Builds the message and submits it to appnet's topic.
Builds the message and submits it to appnet ' s topic .
// Consider only the message submitted. // Report error and stop listening