use of com.hedera.hashgraph.sdk.TopicMessageQuery in project hedera-sdk-java by hashgraph.
the class TopicMessageIntegrationTest method canReceiveATopicMessage.
@Test
@DisplayName("Can receive a topic message")
void canReceiveATopicMessage() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
assertThat(info.topicId).isEqualTo(topicId);
assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
assertThat(info.sequenceNumber).isEqualTo(0);
assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
Thread.sleep(3000);
var receivedMessage = new boolean[] { false };
var start = Instant.now();
var handle = new TopicMessageQuery().setTopicId(topicId).setStartTime(Instant.EPOCH).subscribe(testEnv.client, (message) -> {
receivedMessage[0] = new String(message.contents, StandardCharsets.UTF_8).equals("Hello, from HCS!");
});
Thread.sleep(3000);
new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("Hello, from HCS!").execute(testEnv.client).getReceipt(testEnv.client);
while (!receivedMessage[0]) {
if (Duration.between(start, Instant.now()).compareTo(Duration.ofSeconds(60)) > 0) {
throw new Exception("TopicMessage was not received in 60 seconds or less");
}
Thread.sleep(2000);
}
handle.unsubscribe();
new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
testEnv.close();
}
use of com.hedera.hashgraph.sdk.TopicMessageQuery in project hedera-sdk-java by hashgraph.
the class TopicMessageIntegrationTest method canReceiveALargeTopicMessage.
@Test
@DisplayName("Can receive a large topic message")
void canReceiveALargeTopicMessage() throws Exception {
var testEnv = new IntegrationTestEnv(2);
var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
Thread.sleep(5000);
var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
assertThat(info.topicId).isEqualTo(topicId);
assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
assertThat(info.sequenceNumber).isEqualTo(0);
assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
var receivedMessage = new boolean[] { false };
var start = Instant.now();
var handle = new TopicMessageQuery().setTopicId(topicId).setStartTime(Instant.EPOCH).subscribe(testEnv.client, (message) -> {
receivedMessage[0] = new String(message.contents, StandardCharsets.UTF_8).equals(Contents.BIG_CONTENTS);
});
new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(Contents.BIG_CONTENTS).execute(testEnv.client).getReceipt(testEnv.client);
while (!receivedMessage[0]) {
if (Duration.between(start, Instant.now()).compareTo(Duration.ofSeconds(60)) > 0) {
throw new Exception("TopicMessage was not received in 60 seconds or less");
}
Thread.sleep(1000);
}
handle.unsubscribe();
new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
testEnv.close();
}
use of com.hedera.hashgraph.sdk.TopicMessageQuery in project hedera-sdk-java by hashgraph.
the class ConsensusPubSubChunkedExample method main.
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException, InvalidProtocolBufferException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// generate a submit key to use with the topic
PrivateKey submitKey = PrivateKey.generateED25519();
// make a new topic ID to use
TopicId newTopicId = new TopicCreateTransaction().setTopicMemo("hedera-sdk-java/ConsensusPubSubChunkedExample").setSubmitKey(submitKey).execute(client).getReceipt(client).topicId;
assert newTopicId != null;
System.out.println("for topic " + newTopicId);
// Let's wait a bit
System.out.println("wait 10s to propagate to the mirror ...");
Thread.sleep(10000);
// setup a mirror client to print out messages as we receive them
new TopicMessageQuery().setTopicId(newTopicId).subscribe(client, topicMessage -> {
System.out.println("at " + topicMessage.consensusTimestamp + " ( seq = " + topicMessage.sequenceNumber + " ) received topic message of " + topicMessage.contents.length + " bytes");
});
// get a large file to send
String bigContents = readResources("large_message.txt");
System.out.println("about to prepare a transaction to send a message of " + bigContents.length() + " bytes");
// prepare a message send transaction that requires a submit key from "somewhere else"
@Var Transaction<?> transaction = new TopicMessageSubmitTransaction().setMaxChunks(// this is 10 by default
15).setTopicId(newTopicId).setMessage(bigContents).signWithOperator(client);
// serialize to bytes so we can be signed "somewhere else" by the submit key
byte[] transactionBytes = transaction.toBytes();
// now pretend we sent those bytes across the network
// parse them into a transaction so we can sign as the submit key
transaction = Transaction.fromBytes(transactionBytes);
// view out the message size from the parsed transaction
// this can be useful to display what we are about to sign
long transactionMessageSize = ((TopicMessageSubmitTransaction) transaction).getMessage().size();
System.out.println("about to send a transaction with a message of " + transactionMessageSize + " bytes");
// sign with that submit key
transaction.sign(submitKey);
// now actually submit the transaction
// get the receipt to ensure there were no errors
transaction.execute(client).getReceipt(client);
// noinspection InfiniteLoopStatement
while (true) {
System.out.println("waiting ...");
// noinspection BusyWait
Thread.sleep(2500);
}
}
use of com.hedera.hashgraph.sdk.TopicMessageQuery in project hedera-sdk-java by hashgraph.
the class ConsensusPubSubExample method main.
public static void main(String[] args) throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
TransactionResponse transactionResponse = new TopicCreateTransaction().execute(client);
TransactionReceipt transactionReceipt = transactionResponse.getReceipt(client);
TopicId topicId = Objects.requireNonNull(transactionReceipt.topicId);
System.out.println("New topic created: " + topicId);
Thread.sleep(5000);
new TopicMessageQuery().setTopicId(topicId).subscribe(client, resp -> {
String messageAsString = new String(resp.contents, StandardCharsets.UTF_8);
System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
});
// noinspection InfiniteLoopStatement
for (int i = 0; ; i++) {
new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("hello, HCS! " + i).execute(client).getReceipt(client);
Thread.sleep(2500);
}
}
use of com.hedera.hashgraph.sdk.TopicMessageQuery in project hedera-mirror-node by hashgraph.
the class TopicFeature method createNewOpenTopic.
@Given("I successfully create a new open topic")
public void createNewOpenTopic() {
testInstantReference = Instant.now();
NetworkTransactionResponse networkTransactionResponse = topicClient.createTopic(topicClient.getSdkClient().getExpandedOperatorAccountId(), null);
assertNotNull(networkTransactionResponse.getReceipt());
TopicId topicId = networkTransactionResponse.getReceipt().topicId;
assertNotNull(topicId);
consensusTopicId = topicId;
topicMessageQuery = new TopicMessageQuery().setTopicId(consensusTopicId).setStartTime(Instant.EPOCH);
log.debug("Set TopicMessageQuery with topic: {}, startTime: {}", consensusTopicId, Instant.EPOCH);
}
Aggregations